Listview with radio buttons in flutter slows performance - flutter

I would like to update when the value of radio button changes in flutter But its slowing down the app.
I have created the following listview with the radio buttons yes and no
final List<ItemModel> _questions =
List.generate(100, (index) => ItemModel(name: 'nm_$index', id: index + 1, checked: ''));
ListTile(
title: Text((index + 1).toString() + ". " + question.name),
subtitle: Column(
children: [
ListTile(
onTap: () {
setState(() {
_questions[index].checked = 'yes';
});
},
title: const Text("Yes"),
leading: Radio(
value: 'yes',
groupValue: question.checked,
onChanged: (val) {
setState(() {
_questions[index].checked = 'yes';
});
}),
),
ListTile(
title: const Text("No"),
onTap: () {
setState(() {
_questions[index].checked = 'no';
});
},
leading: Radio(
value: 'no',
groupValue: question.checked,
onChanged: (val) {
setState(() {
_questions[index].checked = 'no';
});
}),
)
],
));
The above listview has two radio buttons where each row is either marked as yes or no. It works well for a small list but when the list gets larger like the above with 100 items the app starts to slow down. I believe its caused by the calling of setState on each radio button click which then causes the ui to be rebuilt.
Is there a way i can update the status of the selected radio without causing the ui to rebuild or is there a way i can improve the above to make the app stop lagging and slowing down when there is alot of items.

Related

create dependent radio buttons in different rows

Currently, I can only use independent radio buttons to realize my desired layout.
But, I need to have the shown radio buttons with different options (option 1 - 6) and only allow single select across the page and have this selection accessible when clicking the button.
How can I realize this in flutter(flow)? Do I need a custom function?
Thanks!
I also tried check boxes but there I have basically the same problem to realize single select.
You can enforce a single selection mode on your radio buttons by setting the groupValue property. This here gives you an example for two radio buttons provided you store the currently selected value in some property int? _selectedValue.
You can change the type of course, I used an integer for this example. More info can be found in the documentation of the Radio class.
return Column(
children: <Widget>[
ListTile(
title: const Text('Value 1'),
leading: Radio<int>(
value: 1,
groupValue: _selectedValue,
onChanged: (int? value) {
setState(() {
_selectedValue = value;
});
},
),
),
ListTile(
title: const Text('Value 2'),
leading: Radio<int>(
value: 2,
groupValue: _selectedValue,
onChanged: (int? value) {
setState(() {
_selectedValue = value;
});
},
),
),
],
);

Flutter: disable ExpansionTile

I have an ExpansionTile that checks if the user is allowed to enable it:
ExpansionTile(
tilePadding: widget.rent ? EdgeInsets.all(0) : null,
onExpansionChanged: (bool value) {
if (_canCreateBill) {
setState(() {
_billable = value;
});
} else {
return Navigator.of(context)
.pushNamed('/invoice-details')
.then((val) async {
await _getBillingInfo();
setState(() {
_billable =
_canCreateBill;
});
});
}
widget.onSwitchChange(value);
},
initiallyExpanded: _billable,
title: Text(translate('expense.create_invoice')),
subtitle: Text(translate('expense.create_invoice_info')),
trailing: IgnorePointer(
child: Switch(
value: _billable,
onChanged: (_) {},
),
),
children: [
Visibility(
visible: _canCreateBill,
child: _billingInfo(),
),
],
);
Happy path:
Here we have _canCreateBill which controls if the user can enable the switch. If the user clicks and he's not allowed, I moved him to a page to add some info. After adding that info, he comes back and the switch is enable.
Problem:
The user can go back without adding any info. The switch will be disable but the ExpansionTile will be active because we already clicked on it:
How can check for this also? Can we disable the ExpansionTile or force a click somehow?
This option is not a solution:
IgnorePointer(
ignoring: true,
child: ExpansionTile()
)

How to handle list - 5000 DropdownMenuItem?

I have list with strings - about 5k positions. I need to convert this list to List< DropdownMenuItem> and I use this in SearchableDropdown.
How to do this most effectively? Or how to do this once when I start the app and save somewhere?
Now I have lag about 3-5 sec when it's loading every time when I open one screen. And I can't use others SearchableDropdown because the have lags to when this big list is loading :/
#override
void initState() {
convert(BIG_LIST).then((value){
list.addAll(value);
});
}
Future<List<DropdownMenuItem>> convert(List<String> BIG_LIST) async {
List<DropdownMenuItem> results = [];
await Future.forEach(BIG_LIST, (string){
results(DropdownMenuItem(
child: Text(string),
value: string,
));
});
return results;
}
Container(
width: MediaQuery.of(context).size.width*0.6,
child: SearchableDropdown(
items: list,
isExpanded: true,
value: value,
hint: new Text('Select'),
searchHint: new Text(
'Select',
style: new TextStyle(fontSize: 24),
),
onChanged: (value) {
print(value);
setState(() {
text = value;
});
},
),
),

How to get Radio to work on flutter when using dynamic object?

I'm new to flutter and I have issues with Radio.
I got it to work perfectly when using the "regular" method:
int _groupValue=-1;
...
...
child: Align(
alignment: Alignment.center,
child: Column(
children: <Widget>[
RadioListTile(activeColor: Colors.black,
groupValue: _groupValue,
value: 1,
onChanged: (value) { setState(() {
_groupValue=value;
}); },
title: Text("a"),
),
RadioListTile(activeColor: Colors.black,
groupValue: _groupValue,
value: 2,
onChanged: (value) { setState(() {
_groupValue=value;
}); },
title: Text("b"),
),
],
),
),
Since I'm using data from API to create the radio buttons I've changed this code to this:
child: Align(
alignment: Alignment.center,
child: children: radioListView
),
),
and on click of a button i call async method to download the data from the API and like this :
void getApiData() async {
...
...
...
setState() {
var radioListView = List<RadioListTile>();
for (Map<String, dynamic> c in apidata)) {
radioListView.add(new RadioListTile(
groupValue: _groupValue,
value: c['option_value'],
title: c['title'],
onChanged: (value) { setState(() {
_groupValue=value;
});
),
));
}
}
}
using the first code it works but using the second code I just get to see the items but nothing happens when I click on the radio buttons (although the onchanged does trigger because I tried to print the value and it's fine)
what am I doing wrong?
I found the solution here:
https://www.didierboelens.com/2018/05/hint-5-how-to-refresh-the-content-of-a-dialog-via-setstate/
The problem was that I had to seperate the widgets and create another stateful widget for the radio

control & disable a dropdown button in flutter?

I wanted to control a drop-down button and make it unclickable using a button.
Is there any way to make it disable. Basically not allowing it able to change.
new DropdownButton(
value: animalName,
items: animals.map(
(String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text('$value'),
);
},
).toList(),
onChanged: (value) {
setState(() {
animalName = value;
});
},
),
So this is the code I currently use on the drop-down button, but i cant disabled it.
Found this in the DropdownButton docs:
If items or onChanged is null, the button will be disabled, the down arrow will be grayed out, and the disabledHint will be shown (if provided)
DropdownButton(
onChanged: null,
items: [...],
)
This isn't what you want to hear, but I don't think there's currently an easy way. I experimented with simply removing all the items and that causes a nice little crash. Maybe worth raising an issue with the flutter people on github...
There is an alternative that may be good enough for you for now. If you wrap your DropdownButton in an IgnorePointer, when you want it to be disabled you can change IgnorePointer's ignoring property to true.
That way if the user taps on it, it won't do anything.
But you'll probably want to indicate to the user somehow that it's disabled as well, something like setting the hint text (as it's grey).
child: new IgnorePointer(
ignoring: true,
child: new DropdownButton(
hint: new Text("disabled"),
items: ["asdf", "wehee", "asdf2", "qwer"].map(
(String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text('$value'),
);
},
).toList(),
onChanged: (value) {},
),
You can make DropdownButtonFormField or DropdownButton disabled if set onChanged to null, and if you want that dropdown still shows selected value you must set disabledHint. For example:
DropdownButtonFormField<String>(
disabledHint: Text(_selectedItem),
value: _selectedItem,
onChanged: enabled ? (value) => setState(() => _selectedItem = value) : null,
items: items.map<DropdownMenuItem<String>>((item) {
return DropdownMenuItem(
value: item,
child: Text(item),
);
}).toList(),
)
Just wrap it with IgnorePointer widget to make DropdownButton disable
IgnorePointer(
ignoring: enabled,
child: new DropdownButton(
value: animalName,
items: animals.map(
(String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text('$value'),
);
},
).toList(),
onChanged: (value) {
setState(() {
animalName = value;
});
},
),
);
If items or onChanged is null, the button will be disabled, the down
arrow will be grayed out, and the disabledHint will be shown (if
provided)
So something like this should work:
DropdownButton<String>(
...
onChanged: this.enabled ? (id) => setState(() => this.id = id) : null,
)
okay, i found a trick that satisfied me
i wanted it hide/show the DropdownButton depending on CheckboxListTile
in StatefulWidget Class
first create a function ex:
_buildDropDown(bool enable) {
if (enable) {
return DropdownButton<String>(
hint: Text("Hint"),
items: <String>[
'item 1',
'item 2',
'item 3',
].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (value) {},
);
} else { // Just Divider with zero Height xD
return Divider(color: Colors.white, height: 0.0);
}
}
and now in build
bool enable = true;
#override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
CheckboxListTile(
title: const Text('Switcher'),
selected: true,
value: enable,
onChanged: (bool value) {
setState(() {
enable = value;
});
},
),
_buildDropDown(enable),
],
);
}
now every time you change enable it will display and hide the DropdownButton
DropdownButtonFormField(
onChange: isDisable ? null : (str){
},
disabledHint: isDisable ? null : Text('Your hint text'),
...
)
For disable
onChange: null
For disable Caption
disabledHint: Text('Your hint text')
//add widget'AbsorbPointer' true-disable,false-enable
// isEditable = ture
AbsorbPointer(
absorbing: isEditable
DropdownButton(
onChanged: null,
items: [...],
)
)
Simple:
decoration:InputDecoration(enabled: false),