gridview - Flutter GridView 导致 Navigator.push 错误
问题描述
我的颤振应用程序有一个按钮可以转到联系页面。当 GridView 只有一个浮动按钮时,它运行良好。但是当GridView有多个floatingButton时会报错。请帮助解决这个问题。
错误:
- 颤振:在调度程序回调期间抛出以下断言:
- flutter:子树中有多个英雄共享同一个标签。
- flutter:在每个要为其设置英雄动画的子树(通常是 PageRoute 子树)中,每个英雄
- flutter:必须有一个唯一的非空标签。
- flutter:在这种情况下,多个英雄具有以下标签:
- 颤动:这是其中一位违规英雄的子树:
- 颤振:#英雄(标签:,状态:_HeroState#232b6)
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,
),
],
),
)
],
);
}
}
解决方案
FloatingActionButton
有一个heroTag
具有默认值的属性const _DefaultHeroTag(),
。
由于您有两个FloatingActionButton
s,您应该为它们中的每一个分配一个不同的标签:
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
将小部件添加到树中。
推荐阅读
- c++ - 导致奇怪错误的优化,将变量置于不应处于的状态
- python - 如何在 Json 请求为空时捕获它并阻止它在 python 中崩溃代码执行?
- linux - 获取大量输出并将其转换为 linux 中的 var
- html - Bootstrap 4 导航栏切换图标不起作用
- r - 如何在ggplot2中获取x轴的正数和负数并重新调整R中的y轴?
- java - ActiveMQ 批量消费者
- c# - 如何在不更改原始数组的情况下更改函数中的数组?
- python - HTTP 服务器接收文件并在 Python 中发回新文件
- javascript - 如何添加 firebase 规则以仅允许使用实时数据库访问一封用户电子邮件?
- c# - 如何在 C# 中获得与 windows Display 相同的屏幕排列?