【问题标题】:Expand one Expansion tile at a time一次展开一个扩展图块
【发布时间】:2020-01-17 05:28:52
【问题描述】:

我有一个扩展图块列表,其中每个都包含一个 ListTile 列表。现在我要做的是在单击一个扩展磁贴时折叠所有扩展。我只想一次展开并显示 一个 展开图块。 或者有什么办法可以将它迁移到扩展面板

                      child: Card(
                            child: ExpansionTile(
                              key: _expansionKey,
                              backgroundColor: Colors.black12,
                              onExpansionChanged: (val) {
                                setState(() {
                                  preId = prescriptionProvider
                                      .doctorPrescriptions[index]
                                      .prescriptionId;
                                  fetchDoctorPrescriptionDrugsByDoctor(
                                      preId);
                                });
                                Toast.show("Changed", context);
                              },
                              title: Text("Prescription ID : " +
                                  prescriptionProvider
                                      .doctorPrescriptions[index]
                                      .prescriptionId),
                              children: <Widget>[
                                InkWell(
                                  onTap: () {

                                  },
                                  child: ListView.builder(
                                    itemCount: presciptionDrugList.length,
                                    shrinkWrap: true,
                                    itemBuilder:
                                        (BuildContext context, int i) {
                                      return ListTile(
                                        title: Text(
                                          presciptionDrugList.isNotEmpty
                                              ? presciptionDrugList[i]
                                                  .drugsName
                                              : "loading",
                                        ),
                                        trailing: Icon(FontAwesomeIcons.pills),
                                      );
                                    },
                                  ),
                                )
                              ],
                            ),
                          ),

由于我有一个列表数组变量,我在其中获取 ListTile 的数据打开的图块将显示与我正在展开的图块相同的数据。所以,我想通过折叠它们来隐藏它。

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    此示例是一种解决方法,它只会保持一个磁贴展开。它的问题是每次扩展其中一个图块时都会重建整个列表。

    class _MyHomePageState extends State<MyHomePage> {
        final int max = 10;
        int selected = -1;
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Builder(
            key: Key('builder ${selected.toString()}'),
            builder: (context){
              return ListView(
                children: <Widget>[
                  for(int i = 0; i < max; i++)
                    ExpansionTile(
                      key: Key(i.toString()),
                      title: Text('Tile ${i+1}'),
                      initiallyExpanded: i==selected,
                      onExpansionChanged: (expanded){
                        if(expanded)
                        setState(() {
                          selected = i; 
                        });
                        else setState(() {
                          selected = -1; 
                        });
                      },
                    )
                ],
              );
            },
          )
        );
      }
    }
    

    【讨论】:

    • 该示例似乎正在运行,但我无法相应地配置我的应用程序。你能用我上传的sn-p做吗?
    • 该示例似乎正在运行,但我无法相应地配置我的应用程序。你能用我上传的sn-p做吗?
    猜你喜欢
    • 2010-10-28
    • 2021-08-10
    • 2021-07-17
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2012-12-16
    • 2019-10-22
    相关资源
    最近更新 更多