Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IconButton throws an exception

Tags:

flutter

dart

I'm trying to make a simple AppBar widget with some icons in Flutter, but I keep getting this assertion:

    The following assertion was thrown building IconButton(Icon(IconData(U+0E5D2)); disabled; tooltip:

I pretty much just mimiced the documentation, but here is my code:

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
  title: "Stateless Widget Example",
  home: new AppBar(title: new Text("App Bar"))
 ));
}

class AppBar extends StatelessWidget {
  AppBar({this.title});

  final Widget title;

  @override
  Widget build(BuildContext context) {
    return new Container(
      height: 56.0,
      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      decoration: new BoxDecoration(
        color: Colors.cyan,
        border: new Border(
          bottom: new BorderSide(
            width: 1.0,
            color: Colors.black
          )
        )
      ),
      child: new Row (
        children: <Widget> [
          new IconButton(
            icon: new Icon(Icons.menu),
            tooltip: 'Navigation menu',
            onPressed: null, // null disables the button
          ),
          new Expanded(child: title)
        ]
      )
    );
  }
}

I feel like I'm missing an import or something. But I'm not completely sure. Perhaps my computer was just acting up, because Flutter run has been buggy for me. I'm new to Dart and Flutter, so perhaps I'm just not seeing an obvious error.

like image 332
Bram Vanbilsen Avatar asked May 17 '17 17:05

Bram Vanbilsen


2 Answers

IconButton is a Material Widget, so you need to use it inside a Material parent, for example something like this:

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text(widget.title), actions: <Widget>[
        new IconButton(
          icon: new Icon(Icons.favorite),
          tooltip: 'Favorite',
          onPressed: () {},
        ),
        new IconButton(
          icon: new Icon(Icons.more_vert),
          tooltip: 'Navigation menu',
          onPressed: () {},
        ),
      ]),
      body: new Center(
        child: new Text('This is the body of the page.'),
      ),
    );
  }
like image 178
Michael Thomsen Avatar answered Nov 13 '22 19:11

Michael Thomsen


Check to ensure that your pubspec.yaml contains the following:

flutter:
  uses-material-design: true

That will ensure that the Material Icons font is included with your application, so that you can use the icons in the Icons class.

like image 43
Collin Jackson Avatar answered Nov 13 '22 17:11

Collin Jackson