固定密码正确时颤动显示小部件
Flutter show widget when fixed password is correct
我想给用户显示一个Image.asset()
,但是在显示之前,他必须知道一个固定的密码,比如Password1234,他应该写在一个TextField()
之类的,如果他在文本字段中输入的内容等于固定密码,他应该能够看到。有什么想法吗?
这可以使用 Visibility widget and setState() 方法轻松实现。请看看这些工具。
看看这个,让我知道它是否适合你
class _MyHomePageState extends State<MyHomePage> {
//For entering password
TextEditingController _passcontroller = TextEditingController();
//Default password set as 1234
String defaultPassword = "1234";
//For visibility widget it is set to false
bool _isVisible = false;
@override
void initState() {
super.initState();
_passcontroller = TextEditingController();
}
@override
void dispose() {
_passcontroller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Center(
child: TextField(
controller: _passcontroller,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Password',
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
showWidget(_passcontroller.text);
},
child: const Text('Submit'),
),
),
Visibility(
visible: _isVisible,
child: Image.network('https://picsum.photos/250?image=9'),
),
],
),
),
);
}
Future<void> showWidget(String password_text) async{
//Checking if the enterd password is equal to default
// password if both matches change the _isVisible to true so
//your widget will show
if (password_text == defaultPassword) {
setState(() {
_isVisible = !_isVisible;
});
}else {
final snackBar = SnackBar(content:Text('Incorrect Password'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
}
我想给用户显示一个Image.asset()
,但是在显示之前,他必须知道一个固定的密码,比如Password1234,他应该写在一个TextField()
之类的,如果他在文本字段中输入的内容等于固定密码,他应该能够看到。有什么想法吗?
这可以使用 Visibility widget and setState() 方法轻松实现。请看看这些工具。
看看这个,让我知道它是否适合你
class _MyHomePageState extends State<MyHomePage> {
//For entering password
TextEditingController _passcontroller = TextEditingController();
//Default password set as 1234
String defaultPassword = "1234";
//For visibility widget it is set to false
bool _isVisible = false;
@override
void initState() {
super.initState();
_passcontroller = TextEditingController();
}
@override
void dispose() {
_passcontroller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Center(
child: TextField(
controller: _passcontroller,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Password',
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
showWidget(_passcontroller.text);
},
child: const Text('Submit'),
),
),
Visibility(
visible: _isVisible,
child: Image.network('https://picsum.photos/250?image=9'),
),
],
),
),
);
}
Future<void> showWidget(String password_text) async{
//Checking if the enterd password is equal to default
// password if both matches change the _isVisible to true so
//your widget will show
if (password_text == defaultPassword) {
setState(() {
_isVisible = !_isVisible;
});
}else {
final snackBar = SnackBar(content:Text('Incorrect Password'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
}