Mobile 颤振-如何在颤振中保存状态选择单选按钮列表?

Mobile 颤振-如何在颤振中保存状态选择单选按钮列表?,mobile,dart,flutter,state,radio,Mobile,Dart,Flutter,State,Radio,根据我掌握的数据,我设法显示了一个radioistratile列表。 请参见下面的我的代码: class ShipFromItemList extends StatefulWidget { @override _ShipFromItemListState createState() => _ShipFromItemListState(); } class _ShipFromItemListState extends State<ShipFromItemList> {

根据我掌握的数据,我设法显示了一个
radioistratile
列表。 请参见下面的我的代码:

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;
    });
  }
}
类ShipFromItemList扩展StatefulWidget{
@凌驾
_ShipFromItemListState createState()=>\u ShipFromItemListState();
}
类_ShipFromItemListState扩展状态{
ShippingAddress\u radioValue;
@凌驾
小部件构建(构建上下文){
返回scopedModelSecondant(生成器:(上下文、子对象、模型){
返回ListView(包覆面提取:true,子项:createRadioListUsers(model));
});
}
列出createRadioListUsers(主模型){
列表小部件=[];
for(ShippingAddress模型中的ShippingAddress ShippingAddress.shippaddrsList){
widgets.add(列)(
儿童:[
尺寸箱(高度:10),
容器(
孩子:放射科医生(
值:shippingAddress,
groupValue:_radioValue,
标题:正文(
发货地址?名称??“,
样式:Theme.of(context).textTheme.title.copyWith(fontSize:15),
),
字幕:文本(
发货地址?地址??“,
风格:
Theme.of(context).textTheme.caption.copyWith(fontSize:13),
),
中学:课文(“14.728英里”,
风格:主题(上下文)
.文本主题
说明文字
.copyWith(字体大小:13)),
onChanged:(当前用户){
打印(“当前用户${currentUser?.name}”);
setSelectedUser(当前用户);
},
所选:_radioValue==发货地址,
activeColor:Colors.orange,
),
),
尺寸箱(高度:10),
分隔器(
高度:0.0,
),
],
));
}
返回窗口小部件;
}
setSelectedUser(发货地址发货地址){
设置状态(){
_radioValue=发货地址;
});
}
}

但是,我想当有一天返回到
选择无线电
页面时,初始化所选无线电是最后一个数据(稍后我将保存firebase)。。我必须保存哪些数据才能将最新数据保存为初始化所选收音机?

您应该将
\u radioValue
的值存储在
sharedPrefences
中。使用此flift软件包,您可以在关闭并重新打开应用程序后再次调用该值
SharedReferences
只存储原始值,例如
int
String
,所以要考虑到这一点。

你应该将
\u radioValue
的值存储在
SharedReferences
中,使用这个颤振包,你可以在关闭并重新打开应用程序后再次调用该值
SharedReferences
只存储诸如
int
String
之类的基本值,所以要考虑到这一点。

非常抱歉,我来晚了。

 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});
}
导入“包装:颤振/材料.省道”;
类ShowSelectRadio扩展StatefulWidget{
@凌驾
ShowSelectRadioState createState(){
返回新的ShowSelectRadioState();
}
}
类ShowSelectRadioState扩展状态{
int _currVal=1;
字符串_currText='';
列表_组=[
群模型(
文本:“flatter.dev”,
索引:1,
),
群模型(
文字:“诱导微笑网”,
索引:2,
),
群模型(
文字:“Google.com”,
索引:3,
),
群模型(
文字:“Yahoo.com”,
索引:4,
),
];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“显示所选收音机示例”),
),
正文:专栏(
儿童:[
扩大(
儿童:中心(
子:文本(_currText,
样式:TextStyle(
字体大小:20.0,
fontWeight:fontWeight.bold,
)),
),
),
扩大(
子:容器(
高度:350.0,
子:列(
儿童:_组
.map((t)=>放射科医生(
标题:文本(“${t.Text}”),
groupValue:_currVal,
值:t.index,
一旦更改:(val){
设置状态(){
_currVal=val.index;
_currText=t.text;
});
},
))
.toList(),
),
)),
],
),
);
}
}
类组模型{
字符串文本;
整数指数;
GroupModel({this.text,this.index});
}

非常抱歉,我来晚了。

 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});
}
导入“包装:颤振/材料.省道”;
类ShowSelectRadio扩展StatefulWidget{
@凌驾
ShowSelectRadioState createState(){
返回新的ShowSelectRadioState();
}
}
类ShowSelectRadioState扩展状态{
int _currVal=1;
字符串_currText='';
列表_组=[
群模型(
文本:“flatter.dev”,
索引:1,
),
群模型(
文字:“诱导微笑网”,
索引:2,
),
群模型(
文字:“Google.com”,
索引:3,
),
群模型(
文字:“Yahoo.com”,
索引:4,
),
];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“显示所选收音机示例”),
),
正文:专栏(
儿童:[
扩大(
儿童:中心(
子:文本(_currText,
样式:TextStyle(
字体大小:20.0,
fontWeight:fontWeight.bold,
)),
),
),
扩大(
子:容器(