我想在显示 page1 时显示 SnackBar。当用户从 page2 导航到 page1 时。
但我只能从第 1 页到第 2 页
这是我的代码
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(
' Homepage',
),
),
body: Center(
child: RaisedButton(
***onPressed: () {
Navigator.push(context,
BouncyPageRoute3(widget: Page2()));
}***
child: Text('go to Page2'),
),
)));
}
}
class BouncyPageRoute3 extends PageRouteBuilder {
final Widget widget;
BouncyPageRoute3({this.widget})
: super(
transitionDuration: Duration(milliseconds: 700),
transitionsBuilder: (BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
animation =
CurvedAnimation(parent: animation, curve: Curves.ease);
return ScaleTransition(
scale: animation,
alignment: Alignment.center,
child: child,
);
},
pageBuilder: (BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
return widget;
});
}
当您使用 Page2 上的 RaisedButton 返回 HomePage 时,我希望 HomePage 上的 SnackBar 显示
我还使用了 BouncyPageRoute3,用于动画。
答案 0 :(得分:1)
试试这个:
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
' Homepage',
),
),
body: Center(
child: RaisedButton(
onPressed: () async {
await Navigator.of(context).push(
PageRouteBuilder(
pageBuilder: (c, a1, a2) => Page2(),
transitionsBuilder: (c, animation, a2, child) {
var begin = Offset(0.0, 1.0);
var end = Offset.zero;
var curve = Curves.ease;
var tween = Tween(begin: begin, end: end)
.chain(CurveTween(curve: curve));
return SlideTransition(
position: animation.drive(tween),
child: child,
);
},
transitionDuration: Duration(milliseconds: 2000),
),
);
showSnackBar();
},
child: Text('go to Page2'),
),
));
}
showSnackBar() {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("page 1")));
}
}
在第 2 页:
RaisedButton(
child: Text('go to homepage '),
onPressed: () {
Navigator.of(context).pop();
},
),