动态列表的复选框状态不会改变

时间:2021-06-16 18:43:27

标签: flutter flutter-layout

我正在尝试在多个部分创建多个复选框,并且数据来自 API。使用下面的方法,标题是正确的,但这里的复选框状态没有改变。

import 'package:flutter/material.dart';

class CheckBoxState {
  final String title;
  bool value;
  CheckBoxState({
    this.title,
    this.value = false,
  });
}


Widget chkboxFilters(BuildContext context) {
    return Container(
        child: Padding(
      padding: const EdgeInsets.all(10.0),
      child: ListView.builder(
          shrinkWrap: true,
          itemCount:
              (userSearchResult[_filterPageIndex.toString()]['values']).length,
          itemBuilder: (context, int i) {
            final filterList = [
              CheckBoxState(
                  title: (userSearchResult[_filterPageIndex.toString()]
                          ['values'][i])
                      .toString()),
            ];
            return ListView(
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              children: [
                ...filterList.map(buildSingleCheckBox).toList(),
              ],
            );
          }),
    ));
  }

Widget buildSingleCheckBox(CheckBoxState checkBox) => CheckboxListTile(
        controlAffinity: ListTileControlAffinity.leading,
        activeColor: Colors.blueAccent,
        value: checkBox.value,
        title: Text(checkBox.title,
            style: TextStyle(
              fontWeight: FontWeight.w300,
            )),
        onChanged: (bool newValue) => setState(() => checkBox.value = newValue),
      );

0 个答案:

没有答案