【问题标题】:Flutter show widget when fixed password is correct固定密码正确时颤振显示小部件
【发布时间】:2021-10-17 08:45:14
【问题描述】:

我想给用户显示一个Image.asset(),但是在显示之前,他必须知道一个固定密码,比如Password1234,他应该把它写成TextField()什么的,并且如果他在文本字段中输入的密码等于固定密码,他应该能够看到它。有什么想法吗?

【问题讨论】:

    标签: flutter dart password-protection


    【解决方案1】:

    看看这个,让我知道它是否适合你

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

    【讨论】:

    • 不提兄弟,很乐意帮忙,我也做了一些小改动,我刚刚将比较密码泄露给了函数@Cubii
    【解决方案2】:

    这可以使用Visibility widgetsetState() 方法轻松实现。请看看这些工具。

    【讨论】:

      猜你喜欢
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 2021-01-12
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多