【问题标题】:flutter - how can save state selected radio button list in flutter?颤动 - 如何在颤动中保存状态选择的单选按钮列表?
【发布时间】:2019-07-01 07:11:09
【问题描述】:

我设法根据我拥有的数据显示了一个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页面时,初始化选择的收音机是最后一个数据(我稍后会保存firebase)..我必须保存哪些数据才能保持最新数据初始化选定的收音机?

【问题讨论】:

    标签: mobile dart flutter state radio


    【解决方案1】:

    为时已晚深感抱歉。

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

    【讨论】:

      【解决方案2】:

      你应该使用这个flutter包https://pub.dartlang.org/packages/shared_preferences_radioValue的值存储在sharedPrefences中,它可以让你在应用程序关闭并重新打开后再次调用该值。 sharedPrefences 仅存储原始值,例如 intString,因此请考虑到这一点。

      【讨论】:

      • 嗯好吧,但我想在这种情况下使用 Firebase。好的,那我如何检查初始化选择的单选按钮是存储的数据?
      猜你喜欢
      • 2021-05-13
      • 2019-12-18
      • 2021-04-29
      • 1970-01-01
      • 2020-08-05
      • 2020-06-09
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      相关资源
      最近更新 更多