Mandatory checkbox in flutter

Asked

Viewed 62 times

0

Good morning, I am developing an application and with the implementation of LGPD has become mandatory in the registration menus a checkbox with the famous "read and accept the terms of use and privacy policy". I am having difficulty performing the validation if the user clicked on the Checkbox. How could I make the field of checkbox mandatory? Similar to what the Validator of Textformfield ago.

In short, I would like the user to be obliged to click on the checkbox in order to be able to register. If anyone can help me, thank you!

import 'package:flutter/material.dart';

class checkbox extends StatefulWidget {
  @override
  _checkboxState createState() => _checkboxState();
}

class _checkboxState extends State<checkbox> {

  bool _checked = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: CheckboxListTile(
        contentPadding: EdgeInsets.only(bottom: 3, left: 3, right: 3, top: 0),
        title: Text("Ao se cadastrar você está concordando com os Termos de Uso e Política de Privacidade",
        style:  TextStyle(
          fontSize: 13,
          height: 1.2,
          color: Colors.white
        ),
          textAlign: TextAlign.justify,
        ),
        controlAffinity: ListTileControlAffinity.leading,
        activeColor: Colors.blue,
        value: _checked,
        onChanged: (bool valor) {
          setState(() {
            _checked = valor;
            }
          );
        },
      ),
    );
  }
}

All the other validations worked perfectly, but I got in trouble here. Grateful!

1 answer

0

The problem has been solved as follows:

 return FormField<bool>(
      builder: (state) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                Checkbox(
                    value: checkboxValue,
                    onChanged: (value) {
                      setState(() {
                        checkboxValue = value;
                        state.didChange(value);
                      });
                    }),
                Text('Concordo com os Termos de Uso\n\ne Política de Privacidade',
                style: TextStyle(
                  fontSize: 13,
                  height: 0.6,
                  color: Colors.white
                ),
                  textAlign: TextAlign.justify,
                ),
              ],
            ),
            Text(
              state.errorText ?? '',
              style: TextStyle(
                color: Theme.of(context).errorColor,
              ),
            )
          ],
        );
      },
      validator: (value) {
        if (!checkboxValue) {
          return 'Você precisa aceitar os termos';
        } else {
          return null;
        }
      },
    );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.