复选框无法与匹配的数据列表一起正常工作

Checkboxes are not working properly with matching list of data

我为 ui 使用了预定义数据,但它不起作用。这是我的尝试:

List? authId=[1,3];
  List<CheckBoxModel> checkBoxListTileModel =
  CheckBoxModel.getUsersAuthorization1();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      
      body: ListView.builder(
          itemCount: checkBoxListTileModel.length,
          itemBuilder: (BuildContext context, int index) {
            return Card(
              child: Container(
                padding: EdgeInsets.all(10.0),
                child: Column(
                  children: <Widget>[
                    CheckboxListTile(
dense: true,
title: Text('${checkBoxListTileModel[index].name}',),
                        value: authId==checkBoxListTileModel[index].id ? true : false,
                        onChanged: (bool? val) {
                          checkBoxListTileModel[index].isCheck = val;
                        })
                  ],
                ),
              ),
            );
          }),
    );
  }
}

我可以处理读取数据。我可以得到它们,但是当我尝试更改它们或使用列表时会出现问题。这是行不通的。当我将 authId 作为 int 时,它可以工作但仍然无法正常工作,我无法更改它的值。

这是我的列表实现。

class CheckBoxModel {
  int? id;
  String? name;
  bool? isCheck;

  CheckBoxModel({this.id, this.name, this.isCheck});

  static List<CheckBoxModel> getUsersAuthorization1() {
    return <CheckBoxModel> [
          CheckBoxModel(
            id: 1,
            name: 'Get',
            isCheck: false,
          ),
          CheckBoxModel(
            id: 2,
            name: 'Insert',
            isCheck: false,
          ),
          CheckBoxModel(
            id: 3,
            name: 'Update',
            isCheck: false,
          ),
      ];
  }
}

我的解决方案:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<CheckBoxModel> checkBoxListTileModel =
      CheckBoxModel.getUsersAuthorization1();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
          itemCount: checkBoxListTileModel.length,
          itemBuilder: (BuildContext context, int index) {
            return Card(
              child: Container(
                padding: const EdgeInsets.all(10.0),
                child: Column(
                  children: <Widget>[
                    CheckboxListTile(
                        dense: true,
                        title: Text(
                          '${checkBoxListTileModel[index].name}',
                        ),
                        value: checkBoxListTileModel[index].isCheck,
                        onChanged: (bool? val) {
                          setState(() {
                            checkBoxListTileModel[index].isCheck = val;
                          });
                        })
                  ],
                ),
              ),
            );
          }),
    );
  }
}

class CheckBoxModel {
  int? id;
  String? name;
  bool? isCheck;

  CheckBoxModel({this.id, this.name, this.isCheck});

  static List<CheckBoxModel> getUsersAuthorization1() {
    return <CheckBoxModel>[
      CheckBoxModel(
        id: 1,
        name: 'Get',
        isCheck: false,
      ),
      CheckBoxModel(
        id: 2,
        name: 'Insert',
        isCheck: false,
      ),
      CheckBoxModel(
        id: 3,
        name: 'Update',
        isCheck: false,
      ),
    ];
  }
}

我认为使用 List 不是一个很好的解决方案。但是如果你真的需要它:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<CheckBoxModel> checkBoxListTileModel =
      CheckBoxModel.getUsersAuthorization1();
  List? authId = [1, 3];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
          itemCount: checkBoxListTileModel.length,
          itemBuilder: (BuildContext context, int index) {
            return Card(
              child: Container(
                padding: const EdgeInsets.all(10.0),
                child: Column(
                  children: <Widget>[
                    CheckboxListTile(
                        dense: true,
                        title: Text(
                          '${checkBoxListTileModel[index].name}',
                        ),
                        value: authId!
                            .where((e) => e == checkBoxListTileModel[index].id)
                            .iterator
                            .moveNext(),
                        onChanged: (bool? val) {
                          setState(() {
                            if (val!) {
                              authId!.add(checkBoxListTileModel[index].id);
                            } else {
                              authId!.removeWhere(
                                  (e) => e == checkBoxListTileModel[index].id);
                            }
                          });
                        })
                  ],
                ),
              ),
            );
          }),
    );
  }
}

class CheckBoxModel {
  int? id;
  String? name;

  CheckBoxModel({this.id, this.name});

  static List<CheckBoxModel> getUsersAuthorization1() {
    return <CheckBoxModel>[
      CheckBoxModel(
        id: 1,
        name: 'Get',
      ),
      CheckBoxModel(
        id: 2,
        name: 'Insert',
      ),
      CheckBoxModel(
        id: 3,
        name: 'Update',
      ),
    ];
  }
}

您需要更改此部分:

value: authId==checkBoxListTileModel[index].id ? true : false,
                    onChanged: (bool? val) {
                      checkBoxListTileModel[index].isCheck = val;
                    }

对此=>

value: authId.contains(checkBoxListTileModel[index].id ? true : false,
                    onChanged: (bool? val) {
                      checkBoxListTileModel[index].isCheck = val;
                    }