Checkbox form validation

前端 未结 4 2391
栀梦
栀梦 2021-02-20 11:27

How can I validate a checkbox in a Flutter Form? Every other validation works fine, but the checkbox doesn\'t show an Error. Here is my code:

FormFi         


        
4条回答
  •  [愿得一人]
    2021-02-20 12:26

    The above answer is correct, however, if you want to display an error message that is more consistent with the default layout of a TextFormField widget error message, then wrap the Text widget in a Padding widget, and give it the hex colour #e53935.

    Note: You may need to adjust the left padding to fit the CheckboxListTile widget is also wrapped in a Padding widget.

    Check the code below:

        bool _termsChecked = false;
        CheckboxListTile(
                  activeColor: Theme.of(context).accentColor,
                  title: Text('I agree to'),
                  value: _termsChecked,
                  onChanged: (bool value) => setState(() => _termsChecked = value),
                  subtitle: !_termsChecked
                    ? Padding(
                        padding: EdgeInsets.fromLTRB(12.0, 0, 0, 0), 
                        child: Text('Required field', style: TextStyle(color: Color(0xFFe53935), fontSize: 12),),)
                    : null,
                ),
    

提交回复
热议问题