【问题标题】:SliverList setState to Affect Only Selected Item FlutterSliverList setState 仅影响选定的项目颤振
【发布时间】:2021-08-26 10:29:02
【问题描述】:

我的 SliverList 上的每个项目都有一个按钮。当我单击特定的列表项按钮时,我希望通过使用 setState 将其更改为不同的小部件。只有该特定项目按钮应更改为不同的小部件,而列表中的其余部分保留其自己的项目按钮。

示例如下。问题当然是当我单击列表上的任何按钮时,列表上每个项目上的所有按钮都会更改。需要什么才能使其仅影响列表中其按钮被按下的特定项目?

SliverList(
   delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
     final item = gd[index];
     return Container(
       child: Column(
         children: [
           item.mypicwidget(context,item.pid),
           _shownewwidget
              ? item.newwidget(context)
              : RaisedButton(
                   onPressed: () {
                      setState(() {
                        _shownewwidget=true;
                      });
                   },
                  child: const Text('Press Me'),
                ),
]
),
);

【问题讨论】:

    标签: flutter flutter-sliver


    【解决方案1】:

    想通了。

    我相信这种方法是在现有列表中添加一个对象键来表示是要呈现小部件1 还是小部件2,例如。标签:“正常”或标签:“点击”。根据 index.tag,将显示 widget1 或 widget2。因此,对于带有标签正常的按钮的小部件,单击时该按钮将具有功能,将项目的 index.tag 从正常更改为单击。当 Sliverlist 看到这个新值时,它会自动呈现相应的小部件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多