我正在用Flutter做一个使用TabBar的代码,我想将路由放入代码主体内。我的应用程序的主要内容不在main.dart类中,而是在firstPage.dart类中,但是我在main.dart中实现了“ SecondPage”类,我想通过我的IconButton访问该“ SecondPage” firstpage.dart,有办法吗?
我尝试使用“ ./main.dart/SecondPage”,但我认为这是不可能的
// main.dart类中的路线
void main(){
runApp(new MaterialApp(
home: new MyTab(),
routes: <String, WidgetBuilder>{
"/SecondPage": (BuildContext context) => new SecondPage()
}
));
}
// main.dart中的SecondPage类
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context){
return new Scaffold(
appBar: new AppBar(title: new Text("Segunda Página"), backgroundColor: Colors.blue,),
body: new Container(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new IconButton(
icon: new Icon(Icons.home, color: Colors.red),
iconSize: 70.0,
onPressed: (){
Navigator.of(context).pushNamed("/HomePage");
},
),
new Text("Second Page")
],
),
),
),
);
}
}
// firstPage.dart类
class First extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Container(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new IconButton(
icon: new Icon(Icons.accessibility_new),
iconSize: 150.0,
color: Colors.brown,
onPressed: () {
Navigator.of(context).pushNamed("/SecondPage");
},
),
new Text("Acessibilidade")
],
),
),
),
);
}
}
当我按下IconButton时出现的错误是:
I / flutter(21313):引发了另一个异常:在_WidgetsAppState中找不到路由RouteSettings(“ ./ main.dart / SecondPage”,null)的生成器。