【问题标题】:Listview checkboxes with dynamic titles flutter [closed]具有动态标题的列表视图复选框颤动[关闭]
【发布时间】:2020-07-19 08:57:23
【问题描述】:

所以我想构建一个带有复选框的列表视图,并且数据应该是使用模型动态的。任何帮助将不胜感激。

【问题讨论】:

标签: android ios flutter


【解决方案1】:

您可以像这样使用StatefulWidgetFutureBuilderListView.builderCheckboxListTile 来做到这一点..

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  
  List<String> listOfItems;
  List<bool> itemCheckedState = [];
  
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: fetchListOfItems(),
      builder: (context, snapshot){
        if(!snapshot.hasData){
          return const CircularProgressIndicator();
        }
        else {
          listOfItems = snapshot.data;
          for(int i = 0; i < listOfItems.length; i++){
            itemCheckedState.add(false);
          }
          return ListView.builder(
            itemCount: listOfItems.length,
            itemBuilder: (context, index){
              return CheckboxListTile(
                title: Text(listOfItems[index]),
                value: itemCheckedState[index],
                onChanged: (newValue){
                  setState((){
                    itemCheckedState[index] = newValue;
                  });
                },
              );
            }
          );
        }
      }
    );
  }
  
  
  Future<List<String>> fetchListOfItems() async {
    
    //for demo purpose lets just return a list after 2 seconds
    //here you can code for fetching your dynamic data instead
    
    List<String> list = ['abc', 'def', 'xyz'];
    await Future.delayed(Duration(seconds: 2), (){});
    return list;
  }
  
}

【讨论】:

  • 感谢您的回复,我实际上想将数据存储在模型中以用于发布请求,例如:{“apple”:true,“banana”:false}。所以我想要一个模型类,它有一个构造函数来初始化变量
猜你喜欢
  • 2018-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 2020-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
相关资源
最近更新 更多