Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List ExpansionPanelRadio的唯一headerValue和ExpansedValue_List_Flutter_Class_Expansion - Fatal编程技术网

List ExpansionPanelRadio的唯一headerValue和ExpansedValue

List ExpansionPanelRadio的唯一headerValue和ExpansedValue,list,flutter,class,expansion,List,Flutter,Class,Expansion,如何为每个可扩展的互动程序添加唯一的值,使常见问题解答看起来像这样 从此链接获取我的代码: 我想添加我自己独特的headerValue和expandedValue,而不是让它占用像这样的标准数字 headerValue: 'Book $index', expandedValue: 'Details for Book $index goes here' 我可能会使用这个类,而不是他们的: class FAQ { FAQ({ this.id, this.expa

如何为每个可扩展的互动程序添加唯一的值,使常见问题解答看起来像这样

从此链接获取我的代码:

我想添加我自己独特的headerValue和expandedValue,而不是让它占用像这样的标准数字

 headerValue: 'Book $index',
      expandedValue: 'Details for Book $index goes here'
我可能会使用这个类,而不是他们的:

class FAQ {
  FAQ({
    this.id,
    this.expandedValue,
    this.headerValue,
  });

  int id;
  String expandedValue;
  String headerValue;
}

您可以创建自定义类,该类可以返回问题列表。在
Item
类中,您需要定义函数
Item.fromJson(动态json)
,该函数将返回
Map
或您的项目。然后,您可以再创建一个函数,将实际数据传递给类
,它将返回
映射
,以便在小部件中使用

这是完整的代码和工作原理

  class Item {
  final String id;
  final String expandedValue;
  final String headerValue;
  Item(
    this.id,
    this.expandedValue,
    this.headerValue,
  );

  factory Item.fromJson(dynamic json) {
    return Item(
        "${json['id']}", "${json['expandedValue']}", "${json['headerValue']}");
  }
}

var mylist = [
  {"id": "1", "headerValue": "question 1", "expandedValue": "answer 1"},
  {"id": "2", "headerValue": "question 2", "expandedValue": "answer 2"}
];

getFeedbackList() {
  return mylist.map((json) => Item.fromJson(json)).toList();
}

class ExpansionItems extends StatefulWidget {
  const ExpansionItems({Key? key}) : super(key: key);

  @override
  State<ExpansionItems> createState() => _ExpansionItemsState();
}

class _ExpansionItemsState extends State<ExpansionItems> {
  final List<Item> _data = getFeedbackList();
  @override
  Widget build(BuildContext context) {
    print(_data);
    return SingleChildScrollView(
      child: Container(
        child: _buildPanel(),
      ),
    );
  }

  Widget _buildPanel() {
    return ExpansionPanelList.radio(
      initialOpenPanelValue: 2,
      children: _data.map<ExpansionPanelRadio>(
        (Item item) {
          return ExpansionPanelRadio(
            value: item.id,
            headerBuilder: (BuildContext context, bool isExpanded) {
              return ListTile(
                title: Text(item.headerValue),
              );
            },
            body: ListTile(
              title: Text(item.expandedValue),
            ),
          );
        },
      ).toList(),
    );
  }
}
类项目{
最终字符串id;
最终字符串扩展值;
最终字符串头值;
项目(
这个身份证,
这个。扩展值,
这个头值,
);
factory Item.fromJson(动态json){
退货项目(
“${json['id']}”、“${json['expandedValue']}”、“${json['headerValue']}”);
}
}
var mylist=[
{“id”:“1”,“headerValue”:“问题1”,“expandedValue”:“答案1”},
{“id”:“2”,“headerValue”:“问题2”,“扩展值”:“答案2”}
];
getFeedbackList(){
返回mylist.map((json)=>Item.fromJson(json)).toList();
}
类ExpansionItems扩展StatefulWidget{
常量扩展项({Key?Key}):super(Key:Key);
@凌驾
状态createState()=>_expansionItemState();
}
类_expansionItemState扩展状态{
最终列表_data=getFeedbackList();
@凌驾
小部件构建(构建上下文){
打印(_数据);
返回SingleChildScrollView(
子:容器(
子项:_buildPanel(),
),
);
}
小部件_buildPanel(){
return ExpansionPanelList.radio(
initialOpenPanelValue:2,
子项:_data.map(
(项目){
返回扩展面板收音机(
值:item.id,
headerBuilder:(BuildContext上下文,bool isExpanded){
返回列表块(
标题:文本(项目标题值),
);
},
正文:ListTile(
标题:文本(项扩展值),
),
);
},
).toList(),
);
}
}

您可以尝试上面的代码

谢谢Chirag。这正是我想要的。你知道怎么解决计时器倒计时吗?