我试图在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的新手,所以也许我只是没有看到明显的错误。

最佳答案

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

关于dart - IconButton引发异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44031376/

10-09 03:23