Reputation: 1207
I managed to display a RadioListTile
List based on the data I have.
see my code below:
class ShipFromItemList extends StatefulWidget {
@override
_ShipFromItemListState createState() => _ShipFromItemListState();
}
class _ShipFromItemListState extends State<ShipFromItemList> {
ShippingAddress _radioValue;
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<MainModel>(builder: (context, child, model) {
return ListView(shrinkWrap: true, children: createRadioListUsers(model));
});
}
List<Widget> createRadioListUsers(MainModel model) {
List<Widget> widgets = [];
for (ShippingAddress shippingAddress in model.shipaddrsList) {
widgets.add(Column(
children: <Widget>[
SizedBox(height: 10),
Container(
child: RadioListTile(
value: shippingAddress,
groupValue: _radioValue,
title: Text(
shippingAddress?.name ?? "",
style: Theme.of(context).textTheme.title.copyWith(fontSize: 15),
),
subtitle: Text(
shippingAddress?.address ?? "",
style:
Theme.of(context).textTheme.caption.copyWith(fontSize: 13),
),
secondary: Text("14.728 mi",
style: Theme.of(context)
.textTheme
.caption
.copyWith(fontSize: 13)),
onChanged: (currentUser) {
print("Current User ${currentUser?.name}");
setSelectedUser(currentUser);
},
selected: _radioValue == shippingAddress,
activeColor: Colors.orange,
),
),
SizedBox(height: 10),
Divider(
height: 0.0,
),
],
));
}
return widgets;
}
setSelectedUser(ShippingAddress shippingAddress) {
setState(() {
_radioValue = shippingAddress;
});
}
}
But, I want when one day returns to the Select Radio
page, the initialize selected radio is the last data (which I will save firebase later).. What data do I have to save in order to maintain the latest data as initialize selected radio?
Upvotes: 0
Views: 13573
Reputation: 11
in some cases this will work so change your code like this setState(() { _currVal = t.index; _currText = t.text; });
Upvotes: 1
Reputation: 3164
Am deeply sorry that am too late.
import 'package:flutter/material.dart';
class ShowSelectRadio extends StatefulWidget {
@override
ShowSelectRadioState createState() {
return new ShowSelectRadioState();
}
}
class ShowSelectRadioState extends State<ShowSelectRadio> {
int _currVal = 1;
String _currText = '';
List<GroupModel> _group = [
GroupModel(
text: "Flutter.dev",
index: 1,
),
GroupModel(
text: "Inducesmile.com",
index: 2,
),
GroupModel(
text: "Google.com",
index: 3,
),
GroupModel(
text: "Yahoo.com",
index: 4,
),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Show Selected Radio Example"),
),
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: Text(_currText,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
)),
),
),
Expanded(
child: Container(
height: 350.0,
child: Column(
children: _group
.map((t) => RadioListTile(
title: Text("${t.text}"),
groupValue: _currVal,
value: t.index,
onChanged: (val) {
setState(() {
_currVal = val.index;
_currText = t.text;
});
},
))
.toList(),
),
)),
],
),
);
}
}
class GroupModel {
String text;
int index;
GroupModel({this.text, this.index});
}
Upvotes: 2
Reputation: 7869
You Should store the value of _radioValue
in sharedPrefences
using this flutter package https://pub.dartlang.org/packages/shared_preferences, it will enable you to call the value again after the app is closed and reopened again. sharedPrefences
store primitive values only such as int
and String
so take that into consideration.
Upvotes: 0