How do I set text field and show and hide text based on radio button. I want to set when select yes text and text field will be visible and when deselect yes text and text field will be hidden. I tried to find info in this regard but was unsuccessful.
My code:
bool select = false;
String _medicalhistory = "n/a";
Container(
width: MediaQuery.of(context).size.width > 800
? 700
: MediaQuery.of(context).size.width,
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Past medical history: ",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
height: 1.5),
),
RadioListTile(
title: const Text("Yes"),
value: "yes",
groupValue: _medicalhistory,
//onChanged: handleSelection
onChanged: (value) {
setState(() {
_medicalhistory = value!;
});
},
),
const Text(
"If yes, please share and describe the previous medical illness: "),
const SizedBox(
height: 5,
),
TextField(
controller: myController,
maxLength: 1000,
decoration: const InputDecoration(
border: OutlineInputBorder(),
//hintText: 'Enter a search term',
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextButton(
onPressed: () {
_medicalhistory = myController.text;
},
child: const Text("Save")),
],
),
RadioListTile(
title: const Text("No"),
value: "no",
groupValue: _medicalhistory,
onChanged: (value) {
setState(() {
_medicalhistory = value.toString();
});
},
),
],
)
],
),
),
I am attaching the code if anyone is willing to help me. Sorry, I’m new to this. Please guide me and I hope someone is willing to share the code on how to make the design I want.
3
Answers
It’s quite simple, check for your condition and use an
if
.In this example, were using the spread operator (
...
) to add multiple items to show in the list:For example:
Your complete code would look like:
See also
Use turnery operator or if to see if
medicalhistory == "Yes"
then display textboxYou can use
ValueNotifier
which willrebuild
only specific widget