mardi 15 février 2022

Can't access checkbox value in flutter_picker's footer

I'm new to flutter and I'm trying to get a checkbox value that's in a flutter_picker's footer. I'm using the flutter_picker for picking a date value.

void showYearMonthPicker(BuildContext context) {
    Map japaneseMap =  getJapaneseYearMap();
    Picker picker = Picker(
        adapter: NumberPickerAdapter(data: [
          NumberPickerColumn(
              begin: 1,
              end: 5,
              initValue: 1,
              onFormatValue: (v) {
                return "$v年(" + japaneseMap["$v"] + ")";
              }
          ),
          NumberPickerColumn(
              begin: 1,
              end: 12,
              initValue: 1,
              onFormatValue: (v) {
                return "$v月";
              }
          ),
        ]),
        cancelText: "cancel",
        confirmText: "confirm",
        columnFlex: [2, 1],
        footer: CheckboxWithState(),
        selectedTextStyle: TextStyle(color: Colors.deepOrangeAccent),
        onConfirm: (Picker picker, List value) {
          // use checkbox _value when confirm button is pressed. 
          // for example, use default dates instead of picker selection if checkbox is selected. 
        }
    );
      picker.show(_scaffoldKey.currentState!);
    }

At first the checkbox wasn't displaying the updated value so I put the checkbox with its value in another statefulwidget class and put it in the footer of my Picker.

class CheckboxWithState extends StatefulWidget {
  @override
  _CheckboxWithState createState()=> new _CheckboxWithState();
}


class _CheckboxWithState extends State<CheckboxWithState>{
  bool _value = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 30.0,
        alignment: Alignment.center,
        child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Checkbox(
                value: _value,
                onChanged:(isCheck){
                  setState(() {
                    _value=isCheck!;
                  });
                } ,
              ),
              Text("not determined",
                  style: TextStyle(fontSize: 18)),
            ]
        )
    );
  }

  bool getDetermined(){
    return _value;
  }
}

I want to use the checkbox value in the onConfirm of my Picker class but I don't know how to access it. The solution could be very simple as I don't know the basics of flutter very well.




Aucun commentaire:

Enregistrer un commentaire