If user clicks on any of the checkbox then a textformfield should be displayed below the checkbox to enter the unit configuration.
For example when user clicks on a checkbox 1BHK then a textformfield should be visible under it and if he clicks on 2 BHK then a textformfield should be visible under it and so on.
Here is my code:
import 'package:flutter/material.dart';
class CheckBoxes extends StatefulWidget {
const CheckBoxes({Key? key}) : super(key: key);
@override
State<CheckBoxes> createState() => _CheckBoxesState();
}
class _CheckBoxesState extends State<CheckBoxes> {
static List<String> selectedOptions = [];
List<CheckboxListTile>? bhkOptions;
@override
void initState() {
super.initState();
selectedOptions = [];
}
@override
Widget build(BuildContext context) {
bhkOptions = [
CheckboxListTile(
title: Text('1 BHK'),
value: selectedOptions.contains('1 BHK'),
onChanged: (bool? value) {
setState(() {
if (value!) {
selectedOptions.add('1 BHK');
} else {
selectedOptions.remove('1 BHK');
}
});
},
),
CheckboxListTile(
title: Text('2 BHK'),
value: selectedOptions.contains('2 BHK'),
onChanged: (bool? value) {
setState(() {
if (value!) {
selectedOptions.add('2 BHK');
} else {
selectedOptions.remove('2 BHK');
}
});
},
),
CheckboxListTile(
title: Text('3 BHK'),
value: selectedOptions.contains('3 BHK'),
onChanged: (bool? value) {
setState(() {
if (value!) {
selectedOptions.add('3 BHK');
} else {
selectedOptions.remove('3 BHK');
}
});
},
),
CheckboxListTile(
title: Text('4 BHK'),
value: selectedOptions.contains('4 BHK'),
onChanged: (bool? value) {
setState(() {
if (value!) {
selectedOptions.add('4 BHK');
} else {
selectedOptions.remove('4 BHK');
}
});
},
),
CheckboxListTile(
title: Text('5+ BHK'),
value: selectedOptions.contains('5+ BHK'),
onChanged: (bool? value) {
setState(() {
if (value!) {
selectedOptions.add('5+ BHK');
} else {
selectedOptions.remove('5+ BHK');
}
});
},
),
];
return Column(
children: bhkOptions!,
);
}
}
2
Answers
I think you should use a custom class that implement by yourself instead of CheckboxListTile
As suggested in https://api.flutter.dev/flutter/material/CheckboxListTile-class.html (below the headline "CheckboxListTile isn’t exactly what I want") you can create a custom checkbox widget. The class
CheckBoxTile
doesn’t contain properties to set a child widget below the checkbox.The following code snippets implements a custom checkbox class
MyCustomCheckbox
. TheTextFormField
is the second item in a column and is only shown if thevalue
property ofMyCustomCheckbox
isn’t null. Which exactly means that the checkbox is selected. The first item of the column includes the checkbox label and the checkbox itself.EDIT
You’ve asked in your comment how to access the value set by the checkboxes in another page.
Maybe this guide from the official flutter docs https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple
is worthwhile reading for you. At least it was for me quite helpful.
Your widgets are organized in a widget tree. In order to edit one variable in widget A and access this variable in a different widget B you should declare this variable in a widget that is a common ancestor of A and B.
Then the question arises: How to pass the info/the set handler down the widget tree to A and B. One solution passing it down using constructor parameters, another is using
InheritedWidget
s.In the following approach I’ve used the provider package, which basically simplifies use of the
InheritedWidget
s.Here, the common ancestor is the
MyApp
widget which manages its (state) variable inMyAppState
.