Flutter 如何在flatter中使用滑块生成密码?

Flutter 如何在flatter中使用滑块生成密码?,flutter,Flutter,我正在创建一个密码管理器应用程序,当用户输入他的详细信息时,我不会向他们显示一个滑块,该滑块会在上面的密码字段中生成密码。我已经创建了一些代码,但当用户单击“生成密码”按钮时,我没有得到预期的结果,它显示以下输出 预期产出:- 这是我的密码: import 'package:flutter/cupertino.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; impor

我正在创建一个密码管理器应用程序,当用户输入他的详细信息时,我不会向他们显示一个滑块,该滑块会在上面的密码字段中生成密码。我已经创建了一些代码,但当用户单击“生成密码”按钮时,我没有得到预期的结果,它显示以下输出

预期产出:-

这是我的密码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class PasswordInput extends StatefulWidget {
  @override
  _PasswordInputState createState() => _PasswordInputState();
}

class _PasswordInputState extends State<PasswordInput> {
  bool _obscureText = true;
  int generatePasswordHelper = 0;
  String _passwordStrength = "Hello";
  int _currentRangeValues = 6;

  void _toggle() {
    setState(() {
      _obscureText = !_obscureText;
    });
  }

  Widget WebsiteName() {
    return TextFormField(
      textCapitalization: TextCapitalization.words,
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
        labelText: "Name of website",
        labelStyle: TextStyle(fontSize: 14, color: Colors.grey.shade400),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.grey.shade300,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.red,
          ),
        ),
      ),
    );
  }

  Widget WebsiteAddress() {
    return TextFormField(
      keyboardType: TextInputType.url,
      decoration: InputDecoration(
        labelText: "Website address",
        labelStyle: TextStyle(fontSize: 14, color: Colors.grey.shade400),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.grey.shade300,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.red,
          ),
        ),
      ),
    );
  }

  Widget UserName() {
    return TextFormField(
      keyboardType: TextInputType.emailAddress,
      decoration: InputDecoration(
        labelText: "Username / Email",
        labelStyle: TextStyle(fontSize: 14, color: Colors.grey.shade400),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.grey.shade300,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.red,
          ),
        ),
      ),
    );
  }

  Widget Password() {
    return TextFormField(
      keyboardType: TextInputType.text,
      obscureText: _obscureText,
      decoration: InputDecoration(
        labelText: "Password",
        labelStyle: TextStyle(fontSize: 14, color: Colors.grey.shade400),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.grey.shade300,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.red,
          ),
        ),
        suffixIcon: IconButton(
          icon: Icon(
            _obscureText ? Icons.visibility : Icons.visibility_off,
            color: Colors.grey.shade600,
          ),
          onPressed: _toggle,
        ),
      ),
    );
  }

  Widget Note() {
    return TextFormField(
      keyboardType: TextInputType.text,
      textCapitalization: TextCapitalization.sentences,
      maxLines: null,
      decoration: InputDecoration(
        labelText: "Note",
        labelStyle: TextStyle(fontSize: 14, color: Colors.grey.shade400),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.grey.shade300,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(
            color: Colors.red,
          ),
        ),
      ),
    );
  }

  Widget GeneratePassword() {
    this.generatePasswordHelper = 0;
    return Container(
      padding: EdgeInsets.only(left: 10),
      child: RichText(
        text: TextSpan(
            style: TextStyle(
              color: Colors.deepOrange,
            ),
            children: <TextSpan>[
              TextSpan(
                  text: "GENERATE PASSWORD",
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      setState(() {
                        generatePasswordHelper = 1;
                      });
                    })
            ]),
      ),
    );
  }

  Widget PasswordGenerator() {
    return Container(
      color: Colors.grey.shade200,
      //height:  MediaQuery.of(context).size.width / 2,
      width: MediaQuery.of(context).size.width / 1.1,
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                padding: EdgeInsets.only(left: 10),
                child: Text(
                  _passwordStrength,
                ),
              ),
              IconButton(
                icon: Icon(Icons.close),
                onPressed: () {
                  setState(() {
                    generatePasswordHelper = 0;
                  });
                },
              ),
            ],
          ),
          Slider(
            value: _currentRangeValues.toDouble(),
            min: 0,
            max: 100,
            divisions: 27,
            onChanged: (double newValue) {
              setState(() {
                _currentRangeValues = newValue.round();
              });
            },
            semanticFormatterCallback: (double newValue) {
              return '${newValue.round()} dollars';
            }
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        centerTitle: true,
        title: Text("Add Password"),
      ),
      body: SingleChildScrollView(
        child: ConstrainedBox(
          constraints: BoxConstraints(),
          child: Container(
            padding: EdgeInsets.only(left: 16, right: 16),
            child: Column(
              //mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: 20,
                ),
                WebsiteName(),
                SizedBox(
                  height: 20,
                ),
                WebsiteAddress(),
                SizedBox(
                  height: 20,
                ),
                UserName(),
                SizedBox(
                  height: 20,
                ),
                Password(),
                SizedBox(
                  height: 20,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    if (generatePasswordHelper == 0)
                      GeneratePassword()
                    else
                      PasswordGenerator()
                  ],
                ),
                SizedBox(
                  height: 20,
                ),
                Note(),
                SizedBox(
                  height: 20,
                ),
                Container(
                  height: 50,
                  width: double.infinity,
                  child: FlatButton(
                    onPressed: () {},
                    padding: EdgeInsets.all(0),
                    child: Ink(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(6),
                        gradient: LinearGradient(
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                          colors: [
                            Color(0xffff5f6d),
                            Color(0xffff5f6d),
                            Color(0xffffc371),
                          ],
                        ),
                      ),
                      child: Container(
                        alignment: Alignment.center,
                        constraints: BoxConstraints(
                            maxWidth: double.infinity, minHeight: 50),
                        child: Text(
                          "Submit",
                          style: TextStyle(
                              color: Colors.white, fontWeight: FontWeight.bold),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(6),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
import'包装:flift/cupertino.dart';
导入“package:flatter/signatures.dart”;
进口“包装:颤振/材料.省道”;
进口“包装:fluttoast/fluttoast.dart”;
类PasswordInput扩展StatefulWidget{
@凌驾
_PasswordInputState createState()=>\u PasswordInputState();
}
类_PasswordInputState扩展状态{
bool _obsolizeText=true;
int generatePasswordHelper=0;
字符串_passwordStrength=“Hello”;
int _currentRangeValues=6;
void _toggle(){
设置状态(){
_蒙蔽文本=!\u蒙蔽文本;
});
}
小部件网站名(){
返回TextFormField(
textcapitalize:textcapitalize.words,
键盘类型:TextInputType.text,
装饰:输入装饰(
labelText:“网站名称”,
labelStyle:TextStyle(字体大小:14,颜色:Colors.grey.shade400),
enabledBorder:OutlineInputBorder(
边界半径:边界半径。圆形(10),
边界边(
颜色:Colors.grey.shade300,
),
),
聚焦顺序:大纲输入边框(
边界半径:边界半径。圆形(10),
边界边(
颜色:颜色,红色,
),
),
),
);
}
小部件网站地址(){
返回TextFormField(
键盘类型:TextInputType.url,
装饰:输入装饰(
labelText:“网站地址”,
labelStyle:TextStyle(字体大小:14,颜色:Colors.grey.shade400),
enabledBorder:OutlineInputBorder(
边界半径:边界半径。圆形(10),
边界边(
颜色:Colors.grey.shade300,
),
),
聚焦顺序:大纲输入边框(
边界半径:边界半径。圆形(10),
边界边(
颜色:颜色,红色,
),
),
),
);
}
小部件用户名(){
返回TextFormField(
键盘类型:TextInputType.emailAddress,
装饰:输入装饰(
labelText:“用户名/电子邮件”,
labelStyle:TextStyle(字体大小:14,颜色:Colors.grey.shade400),
enabledBorder:OutlineInputBorder(
边界半径:边界半径。圆形(10),
边界边(
颜色:Colors.grey.shade300,
),
),
聚焦顺序:大纲输入边框(
边界半径:边界半径。圆形(10),
边界边(
颜色:颜色,红色,
),
),
),
);
}
小部件密码(){
返回TextFormField(
键盘类型:TextInputType.text,
蒙蔽文本:_蒙蔽文本,
装饰:输入装饰(
labelText:“密码”,
labelStyle:TextStyle(字体大小:14,颜色:Colors.grey.shade400),
enabledBorder:OutlineInputBorder(
边界半径:边界半径。圆形(10),
边界边(
颜色:Colors.grey.shade300,
),
),
聚焦顺序:大纲输入边框(
边界半径:边界半径。圆形(10),
边界边(
颜色:颜色,红色,
),
),
后缀:图标按钮(
图标:图标(
_遮挡文字?图标.可见性:图标.可见性关闭,
颜色:Colors.grey.shade600,
),
按下按钮:_切换,
),
),
);
}
小部件注释(){
返回TextFormField(
键盘类型:TextInputType.text,
text大写:text大写。句子,
maxLines:null,
装饰:输入装饰(
标签文字:“注意”,
labelStyle:TextStyle(字体大小:14,颜色:Colors.grey.shade400),
enabledBorder:OutlineInputBorder(
边界半径:边界半径。圆形(10),
边界边(
颜色:Colors.grey.shade300,
),
),
聚焦顺序:大纲输入边框(
边界半径:边界半径。圆形(10),
边界边(
颜色:颜色,红色,
),
),
),
);
}
Widget GeneratePassword(){
this.generatePasswordHelper=0;
返回容器(
填充:仅限边缘设置(左:10),
孩子:RichText(
text:TextSpan(
样式:TextStyle(
颜色:颜色。深橙色,
),
儿童:[
TextSpan(
文本:“生成密码”,
识别器:TapGestureRecognizer()
…onTap=(){
设置状态(){
generatePasswordHelper=1;
});
})
]),
),
);
}
小部件密码生成器(){
返回容器(
颜色:Colors.grey.shade200,
//高度:MediaQuery.of(context).size.width/2,
宽度:MediaQuery.of(context).size.width/1.1,
子:列(
儿童:[
划船(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
容器(
填充:仅限边缘设置(左:10),
子:文本(
_密码强度,
),
),
图标按钮(
图标:图标(Icons.close),
已按下:(){
设置状态(){
generatePasswordHelper=0;
});
},
),
],
),
滑块(
值:_currentRangeValues.toDou
class _PasswordInputState extends State<PasswordInput> {
  bool _obscureText = true;
  int generatePasswordHelper = 0;
  String _passwordStrength = "Hello";
  int _currentRangeValues = 6;
  String currentPassword = ""
  ...

  Widget Password() {
    return TextFormField(
      keyboardType: TextInputType.text,
      obscureText: _obscureText,
      initialValue: currentPassword
      ...