【问题标题】:Unable to validate input using TextFormField and FlatButton无法使用 TextFormField 和 FlatButton 验证输入
【发布时间】:2020-11-22 03:46:23
【问题描述】:

我目前正在开发一个颤振应用程序。我正在尝试使用 TextFormField 获取用户输入,并使用 FlatButton 内的导航器将其传递到下一页。但由于某种原因,它并没有像我预期的那样工作。

代码如下:

TextFormField(
                decoration: InputDecoration(
                  enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(8)),
                      borderSide: BorderSide(color: Colors.grey[200])),
                  focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(8)),
                      borderSide: BorderSide(color: Colors.grey[300])),
                  filled: true,
                  fillColor: Colors.grey[100],
                  errorBorder: InputBorder.none,
                  focusedErrorBorder: InputBorder.none,
                  errorStyle: Theme.of(context).textTheme.caption,
                  hintText: 'Phone Number',
                ),
                keyboardType: TextInputType.phone,
                controller: _phoneController,
                maxLength: 10,
                validator: (value) {
                  print(value);
                  return isPhoneValid(value)
                      ? 'Continue'
                      : "Input : 98XXXXXXXX";
                },
              ),
              FlatButton(
                child: Text('Enter'),
                textColor: Colors.white,
                disabledColor: Colors.grey,
                padding: EdgeInsets.all(16),
                onPressed: (isPhoneValid(_phoneController.text))
                    ? () {
                        Navigator.push(context, MaterialPageRoute(builder,(context)=>HomeScreen));
                      }
                    : null,
                color: Colors.blue,
              ),

这些都在构建函数之外。

final _phoneController = TextEditingController();

  bool isPhoneValid(String value) {
    print('Function');
    print(value);
    return value.trim().length == 10;
  }

由于某种原因,FlatButton 中的 onPressed 总是映射为 null。请在这方面指导我。

提前致谢。

PS:TextFormField 中的验证器似乎也不起作用。

【问题讨论】:

    标签: android-studio flutter dart flutter-layout


    【解决方案1】:

    尚不清楚您以这种方式构建onPressed 的原因,它不会按您期望的方式工作。将您的 onPressed 方法更改为此

    onPressed: () {
        if (isPhoneValid(_phoneController.text)) {
            Navigator.push(context, MaterialPageRoute(builder,(context)=>HomeScreen));
        }
    } 
    

    编辑

    为了在文本字段有效之前禁用按钮,您必须监听文本更改

    bool _canPressButton = false;
    
    @override
    void initState() {
      _phoneController.addListener(_checkIfCanPressButton);
    
      super.initState();
    }
    
    void _checkIfCanPressButton() {
      var canPressButton = isPhoneValid(_phoneController.text);
      if (canPressButton != _canPressButton) {
        setState(() => _canPressButton = canPressButton);
      }
    }
    

    那你就可以了

    onPressed: _canPressButton ?
        ? () => Navigator.push(context, MaterialPageRoute(builder: (context) => HomeScreen))
        : null,
    

    【讨论】:

    • 谢谢。这可行,但我希望在条件不成立时禁用该按钮。
    • 感谢工作。你能帮我做验证器部分吗?
    • @MANOJKUMAR:什么不起作用?对我来说似乎没问题。
    • 我看不到任何验证器消息。
    猜你喜欢
    • 2022-10-21
    • 2022-01-22
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多