首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IconButton抛出异常

IconButton抛出异常
EN

Stack Overflow用户
提问于 2017-05-18 01:28:59
回答 4查看 5.6K关注 0票数 6

我试图创建一个简单的带有Flutter图标的AppBar小部件,但我总是得到这样的断言:

代码语言:javascript
复制
    The following assertion was thrown building IconButton(Icon(IconData(U+0E5D2)); disabled; tooltip:

我基本上只是模仿了文档,但下面是我的代码:

代码语言:javascript
复制
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)
        ]
      )
    );
  }
}

我觉得我错过了一个入口或者别的什么。但我不能完全确定。也许我的电脑只是出问题了,因为颤动运行对我来说是错误的。我是Dart和Flutter的新手,所以可能我只是看不到明显的错误。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-05-18 02:57:37

IconButton是一个Material小部件,因此您需要在Material父对象中使用它,例如:

代码语言:javascript
复制
  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.'),
      ),
    );
  }
票数 11
EN

Stack Overflow用户

发布于 2017-05-18 01:45:30

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

代码语言:javascript
复制
flutter:
  uses-material-design: true

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

票数 1
EN

Stack Overflow用户

发布于 2021-07-03 01:56:04

代码语言:javascript
复制
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"),
            ),
          ),
        ),
      ],
    );
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44031376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档