【问题标题】:Flutter- pass MaterialPageRoute as parameter in another widgetFlutter-在另一个小部件中将 MaterialPageRoute 作为参数传递
【发布时间】:2022-01-12 13:21:23
【问题描述】:
我想将 MaterialPageRoute 作为参数传递到另一个页面。就像如果想将 onPressed((){}) 传递到我们声明为的其他页面
FirstPage({
this.onPressed,
});
final GestureTapCallback onPressed;
如何将MaterialPageRoute(builder: (context) => SecondPage()) 作为参数传递给其他页面?
【问题讨论】:
标签:
android
ios
flutter
dart
【解决方案1】:
这是一种方法:
class MyApp extends StatelessWidget {
const MyApp({Key? key}): super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: FirstPage(
materialPageRoute: MaterialPageRoute(builder: (context) => const SecondPage()),
)
);
}
}
class FirstPage extends StatelessWidget {
const FirstPage({
Key? key,
required this.materialPageRoute,
}): super(key: key);
final MaterialPageRoute materialPageRoute;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
onPressed: () => Navigator.of(context).push(materialPageRoute),
child: const Text('Navigate to SecondPage'),
),
)
);
}
}
class SecondPage extends StatelessWidget {
const SecondPage({Key? key}): super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: const Center(
child: Text('Second Page'),
),
);
}
}