首页 > 解决方案 > 如何在验证器中引用表单字段的值或颤振中的另一个表单字段

问题描述

在下面的代码中,我试图根据新密码字段的保存值测试重复密码的值。该值保存到变量_newPW。但在重复密码文本表单字段中,_newPW 变量始终为空。

我该怎么做?我只想验证两个新密码是否匹配。谢谢。

          TextFormField(
            obscureText: true,
            validator: (value) {
              if (value.isEmpty) {
                return 'please enter the new password';
              }
              return null;
            },
            onSaved: (value) {
              _newPW = value;
            },
            decoration: _textFormFieldDecoration(
              hintText: 'new password',
              padding: 8.0,
            ),
          ),

          TextFormField(
            obscureText: true,
            validator: (value) {
              if (value.isEmpty) {
                return 'please enter the new password';
              } else if (value != _newPW) {
                print('value = $value');
                print('newPW = $_newPW');           // <-- _newPW variable is blank
                return 'new passwords must match';
              }
              return null;
            },
            decoration: _textFormFieldDecoration(
              hintText: 'repeat new password',
              padding: 8.0,
            ),
          ),

标签: flutter

解决方案


您可以在下面复制粘贴运行完整代码
您可以使用onChanged

onChanged: (value) {
              _newPW = value;
            },

工作演示

在此处输入图像描述

完整代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _newPW;
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Form(
          key: _formKey,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextFormField(
                obscureText: true,
                validator: (value) {
                  if (value.isEmpty) {
                    return 'please enter the new password';
                  }
                  return null;
                },
                onChanged: (value) {
                  _newPW = value;
                },
                onSaved: (value) {
                  _newPW = value;
                },
                decoration: InputDecoration(
                  hintText: 'new password',
                  //padding: 8.0,
                ),
              ),
              TextFormField(
                obscureText: true,
                validator: (value) {
                  if (value.isEmpty) {
                    return 'please enter the new password';
                  } else if (value != _newPW) {
                    print('value = $value');
                    print('newPW = $_newPW'); // <-- _newPW variable is blank
                    return 'new passwords must match';
                  }
                  return null;
                },
                decoration: InputDecoration(
                  hintText: 'repeat new password',
                  //padding: 8.0,
                ),
              ),
              SubmitWidget(formKey: _formKey)
            ],
          ),
        ),
      ),
    );
  }
}

class SubmitWidget extends StatelessWidget {
  const SubmitWidget({
    Key key,
    @required GlobalKey<FormState> formKey,
  })  : _formKey = formKey,
        super(key: key);

  final GlobalKey<FormState> _formKey;

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        if (_formKey.currentState.validate()) {
          Scaffold.of(context)
              .showSnackBar(SnackBar(content: Text('Processing Data')));
        }
      },
      child: Text('Submit'),
    );
  }
}

推荐阅读