IconButton引发异常

时间:2017-05-17 17:28:59

标签: dart flutter

我正在尝试使用Flutter中的一些图标制作一个简单的AppBar小部件,但我不断得到这个断言:

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

我几乎只是模仿了文档,但这是我的代码:

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)
        ]
      )
    );
  }
}

我觉得我错过了一个导入或其他东西。但我不完全确定。也许我的电脑只是表现得很好,因为Flutter跑步对我来说是错误的。我是Dart和Flutter的新手,所以也许我只是没有看到明显的错误。

4 个答案:

答案 0 :(得分:6)

IconButton是一个Material Widget,因此您需要在Material父级中使用它,例如:

  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.'),
      ),
    );
  }

答案 1 :(得分:1)

检查以确保pubspec.yaml包含以下内容:

flutter:
  uses-material-design: true

这将确保您的应用程序包含Material Icons字体,以便您可以使用Icons类中的图标。

答案 2 :(得分:0)

addButton() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 10.0),
          child: SizedBox(
            height: 45,
            width: 200,
            child: ElevatedButton.icon(
              onPressed: () async {},
              style: ButtonStyle(
                shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                    RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(30.0),
                      )),
                elevation: MaterialStateProperty.all(1),
                backgroundColor: MaterialStateProperty.all(Colors.blue),
              ),
              icon: Icon(Icons.add, size: 18),
              label: Text("Add question"),
            ),
          ),
        ),
      ],
    );
  }

答案 3 :(得分:-1)

您丢失了材料小部件 IconButton需要材质

return new Material(
      child: 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)
        ]
      )
    );