Search code examples
flutterdartflutter-form-builder

FormBuilderCheckboxList internal value is reset but the checkbox still in checked condition (not cleared) if using INITIAL VALUE


I am using FLUTTER_FORM_BUILDER package for my custom form. I build checkbox list using FormBuilderCheckbox, i give it initial value using initialValue construtor. The problem occur when I'm trying to clear the checkbox. I use globalkey.currentState.reset() to reset the value. It does reset the internal value of the checkbox, but it seems the checkboxes still in Checked Condition.

How can i clear it? I can't use .clear() since i can't assign controller to the FormBuilderCheckbox.

Any insight would be appreciated, thank you.

EDIT: This is a simplified code to reproduce.

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Map _initialData = {
    'checkbox': ['1'],
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyCustomForm(data: _initialData),
    );
  }
}

class MyCustomForm extends StatefulWidget {
  final Map data;

  const MyCustomForm({Key key, @required this.data}) : super(key: key);

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

class _MyCustomFormState extends State<MyCustomForm> {
  List _checkboxInitial;
  final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();

  @override
  void initState() {
    setState(() {
      _checkboxInitial = List.from(widget.data['checkbox']);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Container(
        height: 200,
        width: 200,
        child: FormBuilder(
          key: _fbKey,
          child: Column(
            children: <Widget>[
              FormBuilderCheckboxList(
                initialValue: _checkboxInitial,
                decoration: InputDecoration(border: InputBorder.none),
                attribute: 'checkbox',
                options: [
                  '1',
                  '2',
                  '3',
                ]
                    .map(
                      (data) => FormBuilderFieldOption(
                        child: Text(data),
                        value: data,
                      ),
                    )
                    .toList(growable: false),
              ),
              RaisedButton(
                onPressed: () {
                  setState(() {
                    _fbKey.currentState.reset();
                  });
                },
                child: Text('Clear'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Solution

  • Apparently, i solved my own problem using UniqueKey(). To reset the value, just reset it inside setState().

    class _MyCustomFormState extends State<MyCustomForm> {
      List _checkboxInitial;
      final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
    
      @override
      void initState() {
        setState(() {
          _checkboxInitial = widget.data['checkbox'];
        });
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Material(
          child: Container(
            height: 200,
            width: 200,
            child: FormBuilder(
              key: _fbKey,
              child: Column(
                children: <Widget>[
                  FormBuilderCheckboxList(
                    key: UniqueKey(),
                    initialValue: _checkboxInitial,
                    decoration: InputDecoration(border: InputBorder.none),
                    attribute: 'checkbox',
                    options: [
                      '1',
                      '2',
                      '3',
                    ]
                        .map(
                          (data) => FormBuilderFieldOption(
                            child: Text(data),
                            value: data,
                          ),
                        )
                        .toList(growable: false),
                  ),
                  RaisedButton(
                    onPressed: () {
                      setState(() {
                        _checkboxInitial = [];
                      });
                    },
                    child: Text('Clear'),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    

    I hope it can help anyone who needs it.