【问题标题】:how can i have an logout option in my flutter application?如何在我的颤振应用程序中有注销选项?
【发布时间】:2020-04-06 16:39:05
【问题描述】:

我开发了一个管理应用程序,其中我有登录页面,该页面指向我的仪表板,我可以选择注销,但我不能这样做。

这是我的代码:

  @override
  Widget build(BuildContext context) {

    return Scaffold(

        appBar: AppBar(
          title: Row(
            children: <Widget>[
              Expanded(
                  child: FlatButton.icon(
                      onPressed: () {
                        setState(() => _selectedPage = Page.dashboard);
                      },
                      icon: Icon(
                        Icons.dashboard,
                        color: _selectedPage == Page.dashboard
                            ? active
                            : notActive,
                      ),
                      label: Text('Dashboard'))),
              Expanded(
                  child: FlatButton.icon(
                      onPressed: () {
                        setState(() => _selectedPage = Page.manage);
                      },
                      icon: Icon(
                        Icons.sort,
                        color:
                        _selectedPage == Page.manage ? active : notActive,
                      ),
                      label: Text('Manage'))),
            ],
          ),
          elevation: 0.0,
          backgroundColor: Colors.white,
        ),
        body: _loadScreen());
  }



  Widget _loadScreen() {
    switch (_selectedPage) {
      case Page.dashboard:

        return FutureBuilder(
            future: getUsersCount(),
            builder: (BuildContext context, AsyncSnapshot<String> text) {
              print(text);
              if(text== "-1"){
              return CircularProgressIndicator();
              } else {
                return Column(
                  children: <Widget>[
                    ListTile(
                      subtitle: Text('Admin View', textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 29.0,
                            color: Colors.indigo,
                            fontWeight: FontWeight.bold),),
                    ),
                    Expanded(child: GridView(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,),
                      children: <Widget>[
                        Padding(padding: const EdgeInsets.all(18.0),
                          child: Card(child: ListTile(
                              title: FlatButton.icon(
                                  onPressed: null,
                                  icon: Icon(
                                    Icons.people, color: Colors.black,),
                                  label: Text("Users", style: TextStyle(
                                      fontSize: 9, color: Colors.indigo),)),
                              subtitle: Text(text.data != null ? text.data : '',
                                textAlign: TextAlign.center,
                                style: TextStyle(color: active, fontSize: 50.0),
                              )),
                          ),
                        ),
                      ],

                    ),

                    ),
                  ],
                );
              }
            });
        break;
      case Page.manage:
        return ListView(
          children: <Widget>[
            ListTile(
              leading: Icon(Icons.lock,color: Colors.black,),
              title: Text("Logout",style: TextStyle(color: Colors.indigo),),
              onTap: () {
                Navigator.push(context, MaterialPageRoute(builder: (context)=> AdminLogin()));

// _brandAlert();
              },
            ),

            Divider(),

          ],
        );
        break;
      default:
        return Container();
    }
  }

  void _categoryAlert() {
    var alert = new AlertDialog(
      content: Form(
        key: _categoryFormKey,
        child: TextFormField(
          controller: categoryController,
          validator: (value){
            if(value.isEmpty){
              return 'category cannot be empty';
            }
            return null;
          },
          decoration: InputDecoration(
            hintText: "add Promocode",
          ),
        ),
      ),
      actions: <Widget>[
        FlatButton(onPressed: (){
          if(categoryController.text != null){
            _categoryService.createCategory(categoryController.text);
          }
          Fluttertoast.showToast(msg: 'Promocode changed');
          Navigator.pop(context);
        }, child: Text('ADD')),
        FlatButton(onPressed: (){
          Navigator.pop(context);
        }, child: Text('CANCEL')),

      ],
    );

    showDialog(context: context, builder: (_) => alert);
  }
}

我希望退出管理仪表板,然后再次切换到我的登录页面。

【问题讨论】:

  • 与其推路由,不如pushAndRemoveUntil。但无论如何,从应用程序注销还不够,您要处理的方式取决于您如何管理数据和身份验证以及如何跟踪经过身份验证的用户。

标签: firebase flutter dart firebase-authentication


【解决方案1】:

你需要pushReplacementNamed 而不是只需要push

    Navigator.pushReplacementNamed(context, '/Login');

    navigator.pushReplacement(
      MaterialPageRoute(builder: (BuildContext context) => Login()));

用例:pushReplacementNamed

当用户成功登录,并且现在可能在 DashboardScreen 上时,无论如何您都不希望用户返回 LoginScreen。所以登录路由应该完全被仪表板路由代替。另一个示例是从 SplashScreen 转到 HomeScreen。它应该只显示一次,并且用户应该不能再次从 HomeScreen 回到它。在这种情况下,由于我们要进入一个全新的屏幕,我们可能希望将这个方法用于其输入动画属性。阅读更多关于this

【讨论】:

    【解决方案2】:

    替换这一行

    Navigator.push(context, MaterialPageRoute(builder: (context)=> AdminLogin()));
    

    final route = MaterialPageRoute(builder: (BuildContext context) => AdminLogin());
    Navigator.of(this.context).pushAndRemoveUntil(route, (Route<dynamic> route) => false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-05
      • 2019-11-20
      • 2021-07-03
      • 2020-06-20
      • 1970-01-01
      • 2020-08-05
      • 2022-08-20
      • 2021-08-13
      相关资源
      最近更新 更多