繁体   English   中英

ListView Flutter 中的 CheckboxListTile

[英]CheckboxListTile in ListView Flutter

如何在 ListView 小部件中放置多个 CheckboxListTile? 我已经尝试过这样做,但它遇到了一些异常。

首先创建类 CheckBoxListTileModel (你可以在这个类中放任何东西,你可以根据需要定义一个列表)例如:

class CheckBoxListTileModel {
  String title;
  bool isCheck;

  CheckBoxListTileModel({
    required this.title,
    required this.isCheck,
  });

  static List<CheckBoxListTileModel> getList() {
    return <CheckBoxListTileModel>[
      CheckBoxListTileModel(
        title: "Android",
        isCheck: true,
      ),
      CheckBoxListTileModel(
        title: "Flutter",
        isCheck: false,
      ),
      CheckBoxListTileModel(
        title: "IOS",
        isCheck: false,
      ),
      CheckBoxListTileModel(
        title: "PHP",
        isCheck: false,
      ),
      CheckBoxListTileModel(
        title: "Node",
        isCheck: false,
      ),
    ];
  }
}

创建一个列表是你的页面类

import 'package:flutter/material.dart';

class TestPage extends StatefulWidget {
  const TestPage({Key? key}) : super(key: key);

  @override
  State<TestPage> createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  List<CheckBoxListTileModel> checkBoxListTileModel =
      CheckBoxListTileModel.getList();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.symmetric(
          horizontal: 10,
          vertical: 10,
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              'Choose',
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
            ListView.builder(
                shrinkWrap: true,
                itemCount: checkBoxListTileModel.length,
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    child: Column(
                      children: <Widget>[
                        CheckboxListTile(
                          title: Text(
                            checkBoxListTileModel[index].title,
                            style: const TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                          value: checkBoxListTileModel[index].isCheck,
                          onChanged: (bool? val) {
                            setState(() {
                              checkBoxListTileModel[index].isCheck = val!;
                            });
                          },
                        ),
                      ],
                    ),
                  );
                }),
            const SizedBox(
              height: 20,
            ),
            ElevatedButton(
              onPressed: () {
                int i = 0;
                checkBoxListTileModel.forEach((element) {
                  i++;
                  debugPrint('choice$i: ${element.isCheck}');
                  debugPrint('choice$i: ${element.title}\n');
                });
              },
              child: const Text('Get Result'),
            ),
          ],
        ),
      ),
    );
  }
}

按钮“获取结果”在控制台中查看结果(不一定放)

查看结果在此处输入图像描述

请参阅控制台在此处输入图像描述

multi_select_flutter这个包就是这样做的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM