我不熟悉Flutter和Dart语言,这是我的第一个实际项目,基本上有很多屏幕,我想知道是否可以使用ListTile的onTap转到新屏幕?如果不是,我希望有人指导我。谢谢
到目前为止,这是我的代码:
import 'package:flutter/material.dart';
import 'package:rate_your_professor/screens/firstScreen.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Rate ',
home: Scaffold(
appBar: AppBar(
title: Text("XXXXXXXXXX", textDirection: TextDirection.rtl),
),
body: RaisedBookButton(),
),
));
}
Widget getListView(){
var listView = ListView(
children: <Widget>[
Text("XXXXXXXXXXXXXXX", textDirection: TextDirection.rtl,textAlign: TextAlign.center,),
ListTile(
leading: Icon(Icons.location_city),
title: Text("XXXXX ", textDirection: TextDirection.rtl),
subtitle: Text("XXXXXXXXXX", textDirection: TextDirection.rtl,),
onTap: (){
//Here where I would like to go to new screen
},
),
],
);
return listView;
}
答案 0 :(得分:2)
为了使用导航器转到不同的页面,您需要应用程序的BuildContext。这是如何获取它的示例:
import 'package:flutter/material.dart';
import 'package:rate_your_professor/screens/firstScreen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Some App',
home: SomeApp(),
);
}
}
class SomeApp extends StatelessWidget {
Widget getListView(BuildContext context) {
var listView = ListView(
children: <Widget>[
Text(
"XXXXXXXXXXXXXXX",
textDirection: TextDirection.rtl,
textAlign: TextAlign.center,
),
ListTile(
leading: Icon(Icons.location_city),
title: Text("XXXXX ", textDirection: TextDirection.rtl),
subtitle: Text(
"XXXXXXXXXX",
textDirection: TextDirection.rtl,
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => YourNewPage(),
),
);
},
),
],
);
return listView;
}
@override
Widget build(BuildContext context) {
return Scaffold(body: getListView(context));
}
}
答案 1 :(得分:0)
您可以使用导航器导航到下一个屏幕
如果要转到新屏幕并返回,请在此处使用Navigator.push
。
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
与此同时,请查看官方documentation以获得有关导航的更多信息。