如何处理在flutter中动态创建的复选框列表? [英] How can I handle a list of checkboxes dynamically created in flutter?

查看:527
本文介绍了如何处理在flutter中动态创建的复选框列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用flutter,尝试建立带有一些文本和旁边的自定义复选框的值列表.轻击文本或复选框中的任何位置应显示启用状态,再次轻击则应将其禁用.我不确定如何分别处理每个复选框的状态.我也尝试使用CheckBoxListTile,但不确定如何实现所需的功能.有人可以提供任何示例吗?

Using flutter, I am trying to build a list of values with some text and a customized checkbox next to it. Tapping anywhere on the text or checkbox should show the enabled state and tapping again should disable it. I am unsure how to handle the state of each checkbox separately. I tried using CheckBoxListTile too but I am not sure how I can achieve what I want. Can someone provide any examples?

推荐答案

以下是CheckboxListTile的一些示例代码.您可以在 gallery <中找到更多示例/a>.

Here's some sample code for CheckboxListTile. You can find more examples in the gallery.

import 'package:flutter/material.dart';

class Demo extends StatefulWidget {
  @override
  DemoState createState() => new DemoState();
}

class DemoState extends State<Demo> {
  Map<String, bool> values = {
    'foo': true,
    'bar': false,
  };

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('CheckboxListTile demo')),
      body: new ListView(
        children: values.keys.map((String key) {
          return new CheckboxListTile(
            title: new Text(key),
            value: values[key],
            onChanged: (bool value) {
              setState(() {
                values[key] = value;
              });
            },
          );
        }).toList(),
      ),
    );
  }
}

void main() {
  runApp(new MaterialApp(home: new Demo(), debugShowCheckedModeBanner: false));
}

这篇关于如何处理在flutter中动态创建的复选框列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆