问题
I am trying to create a project where a user can click on a tab in the navigation drawer and it will take them to a page where they can play a quiz. I am having trouble having the user click on the tab and it redirecting them to another .dart file that I have that contains all the code necessary for the quiz. I am very new to Flutter so any information and advice helps. If you have any questions or need information from me please just ask.
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter App',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new HomePage(title: 'HomePage'),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Knowledge Burst"),
backgroundColor: Colors.redAccent,),
drawer: new Drawer(
child: new ListView(
children: <Widget>[
new UserAccountsDrawerHeader(
accountName: new Text("Knowledge Burst"),
accountEmail: null,
decoration: new BoxDecoration(
image: new DecorationImage(
fit: BoxFit.fill,
image: new NetworkImage("")
)
),
),
new ListTile(
title: new Text("History of FBLA"),
trailing: new Icon(Icons.arrow_upward),
),
new ListTile(
title: new Text("FBLA Sponsered"),
trailing: new Icon(Icons.arrow_upward),
),
new ListTile(
title: new Text("FBLA National Office Holders"),
trailing: new Icon(Icons.arrow_upward),
),
new Divider(),
new ListTile(
title: new Text("Economics"),
trailing: new Icon(Icons.arrow_upward),
),
new ListTile(
title: new Text("Entrepreneurship"),
trailing: new Icon(Icons.arrow_upward),
),
new Divider(),
new ListTile(
title: new Text("License and Terms of Use"),
trailing: new Icon(Icons.arrow_upward),
),
new Divider(),
new ListTile(
title: new Text("Close"),
trailing: new Icon(Icons.cancel),
onTap: () => Navigator.of(context).pop(),
),
],
),
),
body: new Center(
child: new Text("HomePage", style: new TextStyle(fontSize:
35.0),),
),
);
}
}
回答1:
You can utilize the onTap method here on ListTile to navigate
new ListTile(
title: new Text("Economics"),
trailing: new Icon(Icons.arrow_upward),
onTap: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => NewScreen()),
);
},
),
来源:https://stackoverflow.com/questions/53576925/flutter-routing-to-a-different-dart-file