flutter 如何弹出动态页面?
flutter how to popuntil to a dynamic page?
我有一个从父页面推送的 post 页面。并且此页面中有 3 个步骤需要再推送 2 次(每个页面使用 CupertinoPageRoute 进行推送)。输入完所有的textfield后,需要popuntil到起始页(一次弹出3页),这是一个topic_id.
的动态页面
Homepage
┗ TopicPage (specified with topic_id)
┗ CreatePage (input some text)
┗ OptionPage (select some options to finish creation)
然后完成创建并返回到具有相同 topic_id 的 TopicPage。
如何实现这种效果?
据我了解,您希望返回某条路线。例如,要返回名为 "login" 的路线,您可以这样做:
Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
这是帮助您到达路线树中任何先前路线的片段。
Navigator.popUntil(context, (Route<dynamic> route){
bool shouldPop = false;
if(route.settings.name == HomePage.routeName){
shouldPop = true;
}
return shouldPop;
});
如果您需要此代码的完整示例,请查找此附加演示。
import 'package:flutter/material.dart';
void main(){
runApp(MaterialApp(
title: "Demo App",
routes: {
HomePage.routeName : (_) => HomePage(),
Step1Page.routeName : (_) => Step1Page(),
Step2Page.routeName : (_) => Step2Page(),
Step3Page.routeName : (_) => Step3Page(),
},
home: SplashPage(),
initialRoute: HomePage.routeName,
));
}
class SplashPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
class HomePage extends StatelessWidget {
static const routeName = "/home";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Home Page"),
RaisedButton(onPressed: (){
Navigator.pushNamed(context, Step1Page.routeName);
}, child: Text("Start Steps"),)
],
),
),
);
}
}
class Step1Page extends StatelessWidget {
static const routeName = "/step1";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Step1 Page"),
RaisedButton(onPressed: (){
Navigator.pushNamed(context, Step2Page.routeName);
}, child: Text("Go Step2"),)
],
),
),
);
}
}
class Step2Page extends StatelessWidget {
static const routeName = "/step2";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Step2 Page"),
RaisedButton(onPressed: (){
Navigator.pushNamed(context, Step3Page.routeName);
}, child: Text("Go Step3"),)
],
),
),
);
}
}
class Step3Page extends StatelessWidget {
static const routeName = "/step3";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Step3 Page"),
RaisedButton(onPressed: (){
Navigator.popUntil(context, (Route<dynamic> route){
bool shouldPop = false;
if(route.settings.name == HomePage.routeName){
shouldPop = true;
}
return shouldPop;
});
}, child: Text("Go Home"),)
],
),
),
);
}
}
如果您有任何疑问,请告诉我。
我有一个从父页面推送的 post 页面。并且此页面中有 3 个步骤需要再推送 2 次(每个页面使用 CupertinoPageRoute 进行推送)。输入完所有的textfield后,需要popuntil到起始页(一次弹出3页),这是一个topic_id.
的动态页面Homepage
┗ TopicPage (specified with topic_id)
┗ CreatePage (input some text)
┗ OptionPage (select some options to finish creation)
然后完成创建并返回到具有相同 topic_id 的 TopicPage。
如何实现这种效果?
据我了解,您希望返回某条路线。例如,要返回名为 "login" 的路线,您可以这样做:
Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
这是帮助您到达路线树中任何先前路线的片段。
Navigator.popUntil(context, (Route<dynamic> route){
bool shouldPop = false;
if(route.settings.name == HomePage.routeName){
shouldPop = true;
}
return shouldPop;
});
如果您需要此代码的完整示例,请查找此附加演示。
import 'package:flutter/material.dart';
void main(){
runApp(MaterialApp(
title: "Demo App",
routes: {
HomePage.routeName : (_) => HomePage(),
Step1Page.routeName : (_) => Step1Page(),
Step2Page.routeName : (_) => Step2Page(),
Step3Page.routeName : (_) => Step3Page(),
},
home: SplashPage(),
initialRoute: HomePage.routeName,
));
}
class SplashPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
class HomePage extends StatelessWidget {
static const routeName = "/home";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Home Page"),
RaisedButton(onPressed: (){
Navigator.pushNamed(context, Step1Page.routeName);
}, child: Text("Start Steps"),)
],
),
),
);
}
}
class Step1Page extends StatelessWidget {
static const routeName = "/step1";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Step1 Page"),
RaisedButton(onPressed: (){
Navigator.pushNamed(context, Step2Page.routeName);
}, child: Text("Go Step2"),)
],
),
),
);
}
}
class Step2Page extends StatelessWidget {
static const routeName = "/step2";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Step2 Page"),
RaisedButton(onPressed: (){
Navigator.pushNamed(context, Step3Page.routeName);
}, child: Text("Go Step3"),)
],
),
),
);
}
}
class Step3Page extends StatelessWidget {
static const routeName = "/step3";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Step3 Page"),
RaisedButton(onPressed: (){
Navigator.popUntil(context, (Route<dynamic> route){
bool shouldPop = false;
if(route.settings.name == HomePage.routeName){
shouldPop = true;
}
return shouldPop;
});
}, child: Text("Go Home"),)
],
),
),
);
}
}
如果您有任何疑问,请告诉我。