【发布时间】:2021-07-24 23:58:26
【问题描述】:
当我尝试将我的颤振页面从 neighbourhoodList 推送到各个社区(例如 neighbourhoodlist_admirality)时收到一些错误。
在我的社区列表中,当用户点击相关社区时,我想导航到各个社区页面。由于我还没有建立单独的社区页面,我已经将它们链接到一个示例页面,即 NeighbourhoodAdmirality。
这是我的 neighbourhoodlist 页面代码:
import 'package:flutter/material.dart';
import 'indv_neighbourhoods/neighbourhoodlist_admirality.dart';
class NeighbourhoodList extends StatefulWidget {
NeighbourhoodList({ this.name = "name"});
final String name;
@override
_NeighbourhoodListState createState() => _NeighbourhoodListState();
}
class _NeighbourhoodListState extends State<NeighbourhoodList> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Neighbourhoods',
),
),
body: _buildListView(context),
);
}
ListView _buildListView(BuildContext context){
return ListView.builder(
itemCount: allNeighbourhoods.length,
itemBuilder: (BuildContext content, int index) {
NeighbourhoodList neighbourhoodlist = allNeighbourhoods[index];
return NeighbourhoodListTile(neighbourhoodlist);
});
}
}
class NeighbourhoodListTile extends ListTile {
NeighbourhoodListTile(NeighbourhoodList neighbourhoodlist)
: super(
title: Text(neighbourhoodlist.name),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => NeighbourhoodAdmiralty(neigh1)),
);
}
);
}
List<NeighbourhoodList> allNeighbourhoods = [
NeighbourhoodList(name: 'Admiralty'),
NeighbourhoodList(name: 'Aljunied'),
NeighbourhoodList(name: 'Ang Mo Kio'),
];
这是一个示例页面的代码,我想在每个单独的社区被引导时将我的邻居列表指向该页面。
import 'package:flutter/material.dart';
class NeighbourhoodAdmiralty extends StatefulWidget {
final String neigh1;
NeighbourhoodAdmiralty(this.neigh1);
@override
_NeighbourhoodAdmiraltyState createState() => _NeighbourhoodAdmiraltyState();
}
class _NeighbourhoodAdmiraltyState extends State<NeighbourhoodAdmiralty> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Admiralty"),
),
body: Center(child: Text('This is the individual neighbourhood page'),
),
);
}
}
由于我还是一个初学者,我遇到了一些错误,并且有几个问题:
neighbourhoodlist.dart 上的错误 1:Navigator.push 下的“未定义名称 'context'” --> 不知道为什么会发生这种情况,因为我已经在上面的方法中传递了 BuildContext
neighbourhoodlist.dart 上的错误 2:Navigator.push 下的“未定义名称 'neigh1'” --> 我想将 neighbourhoodlist.dart 页面重定向到各个社区工作表,但我不确定我在这里传递了什么,我已经尝试过 'neigh1'(我在 neighbourhoodlist_admirality 中的变量)、'name' - NeighbourhoodList 中的变量和 'index' 0 中的变量 inNeighbourhoodListState 但到目前为止它们似乎都不起作用。
感谢您为解决此问题提供的所有帮助,并提前致谢!
【问题讨论】:
标签: flutter