Navigator.push 在 ListView 中,ListTile 不工作
Navigator.push in a ListView, ListTile not working
我正在编写一个带有导航抽屉、tabcontroller 和选项卡中的列表视图的 flutter 应用程序。现在,我想从列表视图中的项目导航到一个新屏幕,但我无法让它工作。这是我的代码:
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 并按需创建列表,例如:
class ChooseDiff extends StatelessWidget {
// code
List<Widget> _list1(BuildContext context) => <Widget>[
// your list here
];
// your TabView code
children: [
ListView(children: _list1(context),
],
我正在编写一个带有导航抽屉、tabcontroller 和选项卡中的列表视图的 flutter 应用程序。现在,我想从列表视图中的项目导航到一个新屏幕,但我无法让它工作。这是我的代码:
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 并按需创建列表,例如:
class ChooseDiff extends StatelessWidget {
// code
List<Widget> _list1(BuildContext context) => <Widget>[
// your list here
];
// your TabView code
children: [
ListView(children: _list1(context),
],