mobile - flutter-如何在flutter中保存状态选择的单选按钮列表?

标签 mobile dart flutter state radio

我设法根据我的数据显示了一个RadioListTile列表。
请参阅下面的代码:

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;
    });
  }
}

但是,我想当一天返回Select Radio页面时,初始化选定的 radio 是最后一个数据(我将在以后保存Firebase)。为了保持最新数据作为初始化选定的 radio ,我必须保存哪些数据?

最佳答案

非常抱歉,为时已晚。

 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});
}

关于mobile - flutter-如何在flutter中保存状态选择的单选按钮列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54568757/

相关文章:

flutter - 抖动问题中的HTTP调用类型强制转换错误

colors - StageXL 和颜色值

dart - 如何在 flutter 中制作视频缩略图?

dart - Flutter URL 启动器 Google map

javascript - 在右下角显示一个没有位置 : absolute 的 <div>

php - 用 PHP 检测移动设备的最简单方法

mobile - AdMob 可以用于桌面应用程序吗?

javascript - HTML/JS 地理定位和比较坐标

flutter - Flutter中有类似AlarmManager的东西吗?

flutter - 使用Flutter向下滚动时如何隐藏带有动画的小部件