CheckboxListTile 如何激活/停用?

时间:2021-04-09 23:27:34

标签: flutter flutter-layout flutter-dependencies flutter-test

我正在使用 CheckboxListTile 实现复选框列表,但遇到了问题。

我正在尝试使用 setState 激活/停用复选框,但它似乎没有更新。

这是我的代码。

这是我调用返回 CheckboxListTitle 的函数的部分。

Column(
          children: [
            RefreshIndicator(
              onRefresh: refetch,
              child: ListView.builder(
                itemCount: newVehiclesType.length,
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  return _itemVehicle(
                      newVehiclesType[index]['imagenActivo']['url'].toString(),
                      newVehiclesType[index]['nombre'].toString(),
                      newVehiclesType[index]['isSelected'],
                      (newVehiclesType[index]['id']),
                      index);
                },
              ),
            ),
            SizedBox(
              height: 20,
            ),

这是我重复使用的 Widget 功能。

  Widget _itemVehicle(String assetImage, String vehicleName, bool isSelected,
      dynamic index, int indexList) {
    var currentIndex = indexList;
    return CheckboxListTile(
      secondary: Image.network(
        'http://192.168.1.3:1337$assetImage',
        width: 30,
        height: 30,
      ),
      title: Text(vehicleName),
      value: newVehiclesType[currentIndex]['isSelected'],
      controlAffinity: ListTileControlAffinity.leading,
      onChanged: (bool newValue) {
        print(newValue);
        setState(() {
          currentIndex =
              newVehiclesType.indexWhere((element) => element['id'] == index);
          newVehiclesType[currentIndex]['isSelected'] = newValue;
        });
      },
    );
  }


0 个答案:

没有答案