我已经为此工作了几个小时,但我放弃了。我需要帮助。我选择了一个 BottomNavBarItem onTap 并启动了一个名为 _onItemTapped 的 void 函数,然后该函数确定 BNBI 的索引,然后启动一个新类。我在代码中有几个测试打印,因此我可以看到它已下降到第二个“print 'testing1'”,但没有加载任何内容,也没有任何反应。有人可以解释一下我做错了什么吗?
以下代码来自 BNBI 代码
BottomNavigationBarItem(
backgroundColor: Colors.blueGrey,
icon: Icon(Icons.login),
label: ' Search \n Ingredients Db',
tooltip:
'Login or Create an Account to Search Ingredients Database',
),
],
onTap: _onItemTapped,
//loads
void _onItemTapped(final int index) {
setState(() {
Future.sync(() => _CocPageState());
_selectedIndex = index;
});
if (_selectedIndex == 0) {
_CocPageState()
.build(context); // takes you to the Chemicals of Concern page.
} else if (_selectedIndex == 1) {
_CocPageState().build(context); // //todo go to the dirty list
} else if (_selectedIndex == 2) {
_CocPageState().build(context); //todo go to the safer products list
}
_CocPageState().build(context);
}
// to the Class
class CocPage extends StatefulWidget {
@override
_CocPageState createState() => _CocPageState();
}
class _CocPageState extends State<CocPage> {
@override
Widget build(BuildContext context) {
print('test');
const title =
'Chemicals of Concern';
print('testing1'); //nothing happens beyond this point that I can tell.
return Scaffold(
body: ListView.builder(
itemCount: chemBrain.chemsBank.length,
// itemBuilder: (context, index),
prototypeItem: ListTile(
title: Text('Index 0: chemBrain.chemsBank[index].chemName')),
itemBuilder: (BuildContext context, index) {
return ListTile(
title: Text(chemBrain.chemsBank[index].chemName),
trailing: Column(
children: [
InkWell(
child: Text(chemBrain.getChemsName()),
最佳答案
我建议您在点击 BottomNavigationBarItem 时调用 Navigator.of(context).push()。然后您只需推送到新屏幕,而不必担心状态。
关于Flutter 不会加载新的列表生成器页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73653885/