【问题标题】:Navigate back and show snackbar返回并显示小吃店
【发布时间】:2021-07-10 13:24:04
【问题描述】:

我想在 page1 显示时显示 SnackBar。当用户从 page2 导航到 page1 时。

但我只能从 page1 工作到 page2

这是我的代码

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 返回主页时,我希望主页上的 SnackBar 显示

我还使用了 BouncyPageRoute3,它是用于动画的。

【问题讨论】:

  • 从page1到page2的代码可以分享一下吗?
  • 我将代码添加到问题@BabC

标签: flutter dart snackbar


【解决方案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();
        },
      ),

【讨论】:

  • 很遗憾,它没有工作,问题中上面的代码会如何?
  • 使用 Navigator.pop,仅在 MyApp 类上保留 MaterialApp
  • 我将此添加到我的动画 onPressed 代码中:() { Navigator.push(context, BouncyPageRoute3(widget: Page2())); } 现在应该怎么样?
  • 你想要什么样的动画?
  • 我有动画路线过渡
猜你喜欢
  • 2018-09-14
  • 2016-04-01
  • 2018-05-04
  • 2020-05-18
  • 2016-03-13
  • 2020-05-22
  • 2020-04-07
  • 2022-10-08
  • 2022-12-17
相关资源
最近更新 更多