这是我的第一篇文章,非常抱歉。我有一个问题,我无法创建一个按钮来在我的apk中打开随机页面。该项目与冠状病毒连接。
答案 0 :(得分:0)
您可以轻松使用RouteGenerator
并列出此任务所需的随机页面。
main.dart:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Random pages',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: 'start_page',
onGenerateRoute: RouteGenerator.generateRoute,
);
}
}
route_generator.dart:
class RouteGenerator {
static List<String> myRandomPages = [
'first_page',
'second_page'
];
static String getRandomNameOfRoute(){
return myRandomPages[Random().nextInt(myRandomPages.length)];
}
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case 'start_page':
return MaterialPageRoute(builder: (_) => StartPage());
case 'first_page':
return MaterialPageRoute(builder: (_) => FirstPage()); // FirstPage - is just a Widget with your content
case 'second_page':
return MaterialPageRoute(builder: (_) => SecondPage());// Also custom Widget
//... other random or not pages
}
}
}
start_page.dart:
class StartPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Start page'),
),
body: Center(
child: RaisedButton(
child: Text('Go to random page'),
onPressed: () => Navigator.of(context).pushNamed(RouteGenerator.getRandomNameOfRoute()),
),
)
);
}
}