如果用户是管理员 flutter,则显示一个小部件

Display a widget if the user is an admin flutter

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

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),
    );
  }
}

您可以以静态方式或使用 ChangeNotifier(类似于 observable)存储 isAdmin 布尔值。

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

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

然后将此值用于 show/hide 您的带有可见性小部件的小部件

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

 Visibility(
              visible: // isAdmin variable,
              child: ...
            ),

编辑: 对于您的问题,请检查密码:

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'),
                ),
         ...
}