首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果用户是管理员颤振器,则显示小部件。

如果用户是管理员颤振器,则显示小部件。
EN

Stack Overflow用户
提问于 2021-02-17 09:22:56
回答 1查看 617关注 0票数 0

我有一个“设置”屏幕,用户可以点击“管理模式”,然后要求一个密码。如果这个密码与我将提供给应用程序管理员的已定义密码相对应,他就会让应用程序在其他屏幕上显示所有管理部件。我想设置一个默认的管理密码,当用户点击“验证”按钮,程序检查是正确的密码。我有一个按钮在屏幕上,就像“管理面板”,我希望这个按钮只有当用户是管理员时才显示。下面是我创建的对话框,我想我必须创建一些东西来检查密码在onPressed函数中是否正确。

代码语言:javascript
复制
class DialogModeAdmin extends StatelessWidget {
  final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
  String _passAdmin;
  dialogContent(BuildContext context) {
    return Container(
      decoration: new BoxDecoration(
        color: Colors.white,
        shape: BoxShape.rectangle,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: Colors.black26,
            blurRadius: 10.0,
            offset: const Offset(0.0, 10.0),
          ),
        ],
      ),
      child: SizedBox(
        height: 120,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: Text("Mot de passe"),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                decoration: new BoxDecoration(
                  color: Colors.white,
                  border: Border.all(
                    color: Colors.black,
                    width: 1,
                  ),
                  boxShadow: [BoxShadow(
                    color: Color.fromRGBO(10, 10, 10, 0.25),
                    blurRadius: 5.0,
                    spreadRadius: -2.0,
                    offset: Offset(3.0, 5.0),
                  )],
                  borderRadius: BorderRadius.all(Radius.circular(5.0)),
                ),
                child: SizedBox(
                  height: 30,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 5.0, right: 5.0, top: 1.0, bottom: 4.0),
                    child: FormBuilderTextField(
                      // validator: FormBuilderValidators.required(context),
                      decoration: InputDecoration(
                        border: InputBorder.none,
                      ),
                      name: 'passadmin',
                      onChanged: (String value) {
                        _passAdmin = value;
                      },
                    ),
                  )
                )
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: SizedBox(
                height: 30,
                width: 90,
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    primary: Color.fromRGBO(255, 64, 0, 1.0),
                    onPrimary: Colors.white,
                    shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.0))),     
                  ),
                  onPressed: () async{
                    if(_fbKey.currentState.validate()){
                      // check if the password is correct and then set a local variable to true to set the privileges of an admin
                    }
                  },
                  child: Text('Valider'),
                ),
              ),
            ),
          ],
        )
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10),
      ),
      elevation: 0.0,
      backgroundColor: Colors.transparent,
      child: dialogContent(context),
    );
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-17 09:43:24

您可以以静态方式存储isAdmin布尔值,也可以使用ChangeNotifier存储布尔值(类似于可观察的)。

https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple

https://api.flutter.dev/flutter/foundation/ChangeNotifier-class.html

然后使用此值显示/隐藏使用可见性Widget的小部件

https://api.flutter.dev/flutter/widgets/Visibility-class.html

代码语言:javascript
复制
 Visibility(
              visible: // isAdmin variable,
              child: ...
            ),

编辑:用于检查密码:

代码语言:javascript
复制
class DialogModeAdmin extends StatelessWidget {
  final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
  String _passAdmin;
  // This is your password
  final String _password = "MyPassword";

  dialogContent(BuildContext context) {
    ...
                      
                      name: 'passadmin',
                      // Here you store correctly what the user typed
                      onChanged: (String value) {
                        _passAdmin = value;
                      },
                    ),
    ...
                child: ElevatedButton(
                  ...
                  onPressed: () async{
                    if(_fbKey.currentState.validate()){
                      // check if the password is correct and then set a local variable to true to set the privileges of an admin
                      // As the comment says : 
                     if(_passAdmin == _password){
                       // set the static/valueNotifier to true
                     } else {
                       // display an error 'wrong password'
                     }
                    }
                  },
                  child: Text('Valider'),
                ),
         ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66239035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档