Flutter中通过数据槽部件

cld4siwp  于 2022-12-27  发布在  Flutter
关注(0)|答案(1)|浏览(117)

我想将passcontroller数据从密码小工具传输到passwordrep小工具,以便在passwordrep小工具中进行比较,但它不传输我输入的文本。我不知道是变量还是小工具类型的问题。请帮助。

密码小工具:

TextFormField passwordrep(TextEditingController passrepcontroller, bool isObscure) {
  TextEditingController passcontroller = new TextEditingController();
  password(passcontroller, true);
  return TextFormField(
      controller: passrepcontroller,
      obscureText: isObscure,
      onSaved: (value) {
        passrepcontroller.text = value!;
      },
      validator: (value) {
        RegExp regex = RegExp(r'^.{8,}$');
        if (value!.isEmpty) {
          return "Please repeat the password";
        }

        if (!regex.hasMatch(value) || value != passcontroller.text) {
          return 'Passwords don\'t match';
        }

        return null;
      },
      decoration: const InputDecoration(
        fillColor: Colors.white70,
        filled: true,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(15.0),
          ),
          borderSide: BorderSide(color: Colors.black),
        ),
        contentPadding: EdgeInsets.symmetric(vertical: 15.0),
        hintText: "Repeat Password",
        prefixIcon: Icon(
          Icons.lock_outline,
          color: Colors.black,
        ),
      ));
}

密码报告小工具:

TextFormField passwordrep(TextEditingController passrepcontroller, bool isObscure) {
  TextEditingController passcontroller = new TextEditingController();
  password(passcontroller, true);
  return TextFormField(
      controller: passrepcontroller,
      obscureText: isObscure,
      onSaved: (value) {
        passrepcontroller.text = value!;
      },
      validator: (value) {
        RegExp regex = RegExp(r'^.{8,}$');
        if (value!.isEmpty) {
          return "Please repeat the password";
        }

        if (!regex.hasMatch(value) || value != passcontroller.text) {
          return 'Passwords don\'t match';
        }

        return null;
      },
      decoration: const InputDecoration(
        fillColor: Colors.white70,
        filled: true,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(15.0),
          ),
          borderSide: BorderSide(color: Colors.black),
        ),
        contentPadding: EdgeInsets.symmetric(vertical: 15.0),
        hintText: "Repeat Password",
        prefixIcon: Icon(
          Icons.lock_outline,
          color: Colors.black,
        ),
      ));
}

我试过将小部件转换为有状态的小部件,但也不起作用。

ua4mk5z4

ua4mk5z41#

为密码和repeatPassword制作2个文本控制器

final _passwordController = TextEditingController();
final _repeatPasswordContrller = TextEditingController();

将每个TextEditingController传递到所需的TextFormField

TextFormField(
            controller: _passwordController,

使用text属性_passwordController.text,该属性包含用户输入的文本
完整示例

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});


  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  final _formKey = GlobalKey<FormState>();

  final _passwordController = TextEditingController();
  final _repeatPasswordContrller = TextEditingController();

  @override
  Widget build(BuildContext context) {

    return Scaffold(
    body:    Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextFormField(
            controller: _passwordController,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Please enter password';
              }
              return null;
            },
          ), TextFormField(
            controller: _repeatPasswordContrller,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Please repeat password';
              }
              return null;
            },
          ),
          const SizedBox(height: 25),
          ElevatedButton(
            onPressed: () {
              if (_formKey.currentState!.validate()) {
                

                //compare text here
                if(_passwordController.text == _repeatPasswordContrller.text){
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('the same')),
                  );
                }else{
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('different strings')),
                  );
                }

              }
            },
            child: const Text('Compare passwords'),
          ),
        ],
      ),
    )

    );
  }
}

相关问题