Ras*_*ana 2 android-studio flutter flutter-test flutter-dependencies flutter-layout
我是颤振的初学者,几天前才刚刚开始。我想从一页转到另一页。但当我使用导航器时,它显示错误。
我尝试使用堆栈溢出上类似问题的一些答案来解决它,但我无法解决它。另外,我无法正确理解这些。
以下是其中一些:
未定义的名称“上下文”
颤动导航中未定义的名称“上下文”
import 'package:flutter/material.dart';
import 'package:origitive_app/main.dart';
import 'package:origitive_app/Productpage.dart';
void main() {
runApp(chooseCategory());
}
class chooseCategory extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: new DefaultTabController(
length: 3,
child: new Scaffold(
appBar: new AppBar(
title: const Text('Collections'),
backgroundColor: Colors.grey[400],
actions: [
//list if widget in appbar actions
PopupMenuButton(
icon: Icon(Icons.menu),
//don't specify icon if you want 3 dot menu
color: Colors.blue,
itemBuilder: (context) =>
[
PopupMenuItem<int>(
value: 0,
child: Text(
"Home", style: TextStyle(color: Colors.white),),
),
PopupMenuItem<int>(
value: 0,
child: Text(
"About", style: TextStyle(color: Colors.white),),
),
PopupMenuItem<int>(
value: 0,
child: Text(
"Settings", style: TextStyle(color: Colors.white),),
),
],
onSelected: (item) => {print(item)},
),
],
bottom: new TabBar(isScrollable: true, tabs: [
new Tab(text: 'MEN',),
new Tab(text: 'WOMEN',),
new Tab(text: 'KIDS',),
]),
),
body: new TabBarView(
children: [
new ListView(
children: list1,
),
new ListView(
children: list2,
),
new ListView(
children: list3,
),
],
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.computer),
title: Text('Technology'),
),
BottomNavigationBarItem(
icon: Icon(Icons.book),
title: Text('Education'),
),
],
),
),
));
}
List<Widget> list1 = <Widget>[
new ListTile(
title: new Text('Shoes',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/men1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Clothing',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
subtitle: new Text('429 Castro St'),
leading: new Image.asset("assets/Images/men2.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Accessories',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/men3.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
];
List<Widget> list2 = <Widget>[
new ListTile(
title: new Text('Shoes',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
subtitle: new Text('85 W Portal Ave'),
leading: new Image.asset("assets/Images/women1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Clothing',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/women2.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Accessories',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/women3.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
];
List<Widget> list3 = <Widget>[
new ListTile(
title: new Text('Boys Shoes',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/boy1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage());
);
},
),
new ListTile(
title: new Text('Girls Shoes',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/girl1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Boys Clothing',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/boy1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: () {
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
);
},
),
new ListTile(
title: new Text('Girls Clothing',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/girl2.png"),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},trailing: Icon(Icons.arrow_forward),
),
];
}
}
Run Code Online (Sandbox Code Playgroud)
尝试添加静态构建上下文但失败。它给出了空错误。
小智 6
这是因为您试图访问其范围之外的上下文。将 list1 和 list2 移动到方法中,build如下所示:-
import 'package:flutter/material.dart';
import 'package:origitive_app/main.dart';
import 'package:origitive_app/Productpage.dart';
void main() {
runApp(chooseCategory());
}
class chooseCategory extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: new DefaultTabController(
length: 3,
child: new Scaffold(
appBar: new AppBar(
title: const Text('Collections'),
backgroundColor: Colors.grey[400],
actions: [
//list if widget in appbar actions
PopupMenuButton(
icon: Icon(Icons.menu),
//don't specify icon if you want 3 dot menu
color: Colors.blue,
itemBuilder: (context) =>
[
PopupMenuItem<int>(
value: 0,
child: Text(
"Home", style: TextStyle(color: Colors.white),),
),
PopupMenuItem<int>(
value: 0,
child: Text(
"About", style: TextStyle(color: Colors.white),),
),
PopupMenuItem<int>(
value: 0,
child: Text(
"Settings", style: TextStyle(color: Colors.white),),
),
],
onSelected: (item) => {print(item)},
),
],
bottom: new TabBar(isScrollable: true, tabs: [
new Tab(text: 'MEN',),
new Tab(text: 'WOMEN',),
new Tab(text: 'KIDS',),
]),
),
body: new TabBarView(
children: [
new ListView(
children: list1,
),
new ListView(
children: list2,
),
new ListView(
children: list3,
),
],
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.computer),
title: Text('Technology'),
),
BottomNavigationBarItem(
icon: Icon(Icons.book),
title: Text('Education'),
),
],
),
),
));
List<Widget> list1 = <Widget>[
new ListTile(
title: new Text('Shoes',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/men1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Clothing',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
subtitle: new Text('429 Castro St'),
leading: new Image.asset("assets/Images/men2.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Accessories',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/men3.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
];
List<Widget> list2 = <Widget>[
new ListTile(
title: new Text('Shoes',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
subtitle: new Text('85 W Portal Ave'),
leading: new Image.asset("assets/Images/women1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Clothing',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/women2.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Accessories',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/women3.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
];
List<Widget> list3 = <Widget>[
new ListTile(
title: new Text('Boys Shoes',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/boy1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage());
);
},
),
new ListTile(
title: new Text('Girls Shoes',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/girl1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},
),
new ListTile(
title: new Text('Boys Clothing',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/boy1.png"),
trailing: Icon(Icons.arrow_forward),
onTap: () {
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
);
},
),
new ListTile(
title: new Text('Girls Clothing',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
leading: new Image.asset("assets/Images/girl2.png"),
onTap: (){
Navigator.push(
context,//error
MaterialPageRoute(builder: (context) => Productpage()));
},trailing: Icon(Icons.arrow_forward),
),
];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19310 次 |
| 最近记录: |