首页 > 解决方案 > Flutter GridView 导致 Navigator.push 错误

问题描述

我的颤振应用程序有一个按钮可以转到联系页面。当 GridView 只有一个浮动按钮时,它运行良好。但是当GridView有多个floatingButton时会报错。请帮助解决这个问题。

错误:

在此处输入图像描述

class _DatabasePageState extends State<DatabasePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.black,
          appBar: AppBar(
            title: Text('DB'),
            centerTitle: true,
            backgroundColor: Colors.black,
          ),
          body: _showOrderdatabase(),

        );
      }

      Widget _showOrderdatabase() {

        return Column(
          children: <Widget>[
            Container(
              height: 220.0,
              color: Colors.transparent,
              child: new Container(
                  padding: EdgeInsets.all(40.0),
                  decoration: new BoxDecoration(
                    color: Colors.green,
                    borderRadius: BorderRadius.all(const Radius.circular(40.0)),
                  ),
                  child: new Column(
                    children: <Widget>[
                      Text(
                        "vds",
                        style: TextStyle(fontSize: 16.0),
                      ),
                      IconButton(
                        icon: Icon(
                          Icons.email,
                          color: Colors.blueAccent,
                        ),
                        iconSize: 50.0,
                        onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (c) => new ContactUs())),
                      ),
                    ],
                  )),
            ),
            Expanded(
              child: GridView.count(
                primary: false,
                padding: const EdgeInsets.all(20.0),
                crossAxisSpacing: 30.0,
                mainAxisSpacing: 30.0,
                crossAxisCount: 4,
                children: <Widget>[
                  FloatingActionButton(
                    onPressed: null,
                    backgroundColor: Colors.orange,
                    child: Text('今天'),
                    foregroundColor: Colors.black,
                  ),
                  FloatingActionButton(
                    onPressed: null,
                    backgroundColor: Colors.yellow,
                    child: Text('年'),
                    foregroundColor: Colors.black,
                  ),

                ],
              ),
            )
          ],
        );
      }
    }

标签: gridviewnavigationflutter

解决方案


FloatingActionButton有一个heroTag具有默认值的属性const _DefaultHeroTag(),

由于您有两个FloatingActionButtons,您应该为它们中的每一个分配一个不同的标签:

   children: <Widget>[
      FloatingActionButton(
        onPressed: null,
        heroTag: 'Tag1',
        backgroundColor: Colors.orange,
        child: Text('今天'),
        foregroundColor: Colors.black,
      ),
      FloatingActionButton(
        onPressed: null,
        heroTag: 'Tag2',
        backgroundColor: Colors.yellow,
        child: Text('年'),
        foregroundColor: Colors.black,
      ),
    ],

或者您可以将两者都设置为,null因此不会Hero将小部件添加到树中。


推荐阅读