我正在编写一个带有导航抽屉,tabcontroller和选项卡中的列表 View 的flutter应用程序。现在,我要从列表 View 中的项目导航到一个新屏幕,而我无法使其正常工作。这是我的代码:
class ChooseDiff extends StatelessWidget {
static const String routeName = "ChooseDiff";
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new DefaultTabController(
length: 3,
child: new Scaffold(
appBar: new AppBar(
title: const Text('Tabbed AppBar'),
bottom: new TabBar(isScrollable: true, tabs: [
new Tab(text: 'Enkla',),
new Tab(text: 'Svåra',),
new Tab(text: 'Top 10',),
]),
),
body: new TabBarView(
children: [
new ListView(
children: list,
),
new ListView(
children: list1,
),
new ListView(
children: list3,
)
],
),
),
),
);
}
}
List<Widget> list = <Widget>[
new ListTile(
title: new Text('H&M',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Icon(
Icons.add_to_home_screen,
color: Colors.blue[500],
),
),
new ListTile(
title: new Text('Adidas',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Icon(
Icons.add_to_home_screen,
color: Colors.blue[500],
),
)
];
List<Widget> list1 = <Widget>[
new ListTile(
title: new Text('H&M',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Icon(
Icons.add_to_home_screen,
color: Colors.blue[500],
),
onTap: () {
@override
BuildContext context;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HM()),
);
},
),
new ListTile(
title: new Text('Adidas',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Icon(
Icons.add_to_home_screen,
color: Colors.blue[500],
),
onTap: () {
@override
BuildContext context;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Adisas()),
);
}
)
];
List<Widget> list3 = <Widget>[
new ListTile(
title: new Text('H&M',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Icon(
Icons.add_to_home_screen,
color: Colors.blue[500],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HM()),
);
},
),
new ListTile(
title: new Text('Adidas',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Icon(
Icons.add_to_home_screen,
color: Colors.blue[500],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Adisas()),
);
}
)
];
//Lägg till alla Svåra Texter här
TextStyle defaultStyle = TextStyle(fontSize: 30, color: Colors.black);
class HM extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("H&M"),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(5, 50, 0, 0),
child: RichText(
text: (TextSpan(
style: defaultStyle,
text: 'Vi är Truth About Business!'
)
),
)
),
],
),
),
);
}
}
class Adisas extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Adidas"),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(5, 50, 0, 0),
child: RichText(
text: (TextSpan(
style: defaultStyle,
text: 'Vi är Truth About Business!'
)
),
)
),
],
),
),
);
}
}
当尝试构建此代码时,出现此错误:
Initializing gradle...
Resolving dependencies...
Running Gradle task 'assembleDebug'...
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
Compiler message:
lib/Screens/Texter/TabController.dart:109:9: Error: Getter not found: 'context'.
context,
^^^^^^^
lib/Screens/Texter/TabController.dart:123:11: Error: Getter not found: 'context'.
context,
^^^^^^^
Compiler failed on C:\Users\tbsvst18tedbom\AndroidStudioProjects\tab_truth_true\lib\main.dart
FAILURE: Build failed with an exception.
* Where:
Script 'C:\Users\tbsvst18tedbom\Desktop\Appen\Flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 765
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebugArm64'.
> Process 'command 'C:\Users\tbsvst18tedbom\Desktop\Appen\Flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 29s
Finished with error: Gradle task assembleDebug failed with exit code 1
我不知道还能尝试什么,所以我希望有人可以通过替代代码或其他有用的方法来帮助我。
我非常感谢您的帮助!非常感谢!
最佳答案
您的列表是全局定义的,因此没有上下文。为了使导航器正常工作,它需要在轻按时知道您的最上层上下文。您需要做的是将当前上下文列表。
一种方法是将列表定义放在ChooseDiff类中,并按需创建列表,例如:
class ChooseDiff extends StatelessWidget {
// code
List<Widget> _list1(BuildContext context) => <Widget>[
// your list here
];
// your TabView code
children: [
ListView(children: _list1(context),
],
关于android - Navigator.push在ListView中,ListTile不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58821419/