首页 > 解决方案 > 在颤动中从标签栏推送和弹出导航?

问题描述

在此处输入图像描述

您好,我是 Flutter 的新手,我在main.dart点击按钮后在页面中创建了一个登录表单,logout它将打开带有 2 个孩子的 TabbarTab1.dartTab2.dart. 在Tab2.dart,我有注销按钮,在注销时单击我只是导航到我的登录页面,但我尝试过,它向我显示黑屏。

//main.dart
 child: RaisedButton(
                  child: Text(
                    "Login",
                    style: TextStyle(fontSize: 20),
                  ),
                  onPressed: () {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) => TabBarPage()));
                  },

                )
//Tab2.dart

class Tab2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("PROFILE"),
      ),
      body: Center(
        child: Container(
          padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
          child: Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Container(
                  child: RaisedButton(
                    color: Theme.of(context).accentColor,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          'Logout',
                          style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.w700,
                            color: Colors.white,
                          ),
                        ),
                        Icon(
                          Icons.settings_power,
                          color: Colors.white,
                        ),
                      ],
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

标签: flutter

解决方案


只需main.dart用以下代码替换您的代码

child: RaisedButton(
              child: Text(
                "Login",
                style: TextStyle(fontSize: 20),
              ),
              onPressed: () {
              Navigator.of(context).pushAndRemoveUntil(
                MaterialPageRoute(builder: (context) => TabBarPage()))
              },

            )

推荐阅读