我有一个“设置”屏幕,用户可以点击“管理模式”,然后要求一个密码。如果这个密码与我将提供给应用程序管理员的已定义密码相对应,他就会让应用程序在其他屏幕上显示所有管理部件。我想设置一个默认的管理密码,当用户点击“验证”按钮,程序检查是正确的密码。我有一个按钮在屏幕上,就像“管理面板”,我希望这个按钮只有当用户是管理员时才显示。下面是我创建的对话框,我想我必须创建一些东西来检查密码在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),
);
}
}发布于 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
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'),
),
...
}https://stackoverflow.com/questions/66239035
复制相似问题