使用checkboxlisttile在flutter中使用CheckboxListtile时是否可以知道触发复选框或项目文本

发布于 2025-02-13 20:51:13 字数 1103 浏览 0 评论 0原文

我正在使用CheckBoxListtile在Flutter中显示一些待办事项(v3.0.4)。这是代码看起来像:

CheckboxListTile(
              controlAffinity: ListTileControlAffinity.leading,
              title: Text(element.name,style:element.isCompleted == 1? TextStyle(color: Colors.grey):TextStyle(color: Colors.black)),
              value: element.isCompleted == 1?true:false,
              checkColor: Colors.green,
              selected: element.isCompleted == 1?true:false,

              onChanged: (bool? value) {
                if(value!){
                  element.isCompleted = 1;
                }else{
                  element.isCompleted = 0;
                }
                TodoProvider.updateTodo(element).then((value) => {
                  TodoProvider.getTodos().then((todos) => {
                    buildTodoItems(todos)
                  })
                });
              },
            ))

用户点击checkboxlisttile项目文本时,我想显示待办事项详细信息,当用户点击复选框时,我想更改待办事项任务以完成。现在,我面临的问题是,我无法检测到用户点击的哪一部分,一路触发onchange事件。我已经阅读了CheckBoxListtile源代码,似乎没有API可以执行此操作。我想念什么吗?我应该怎么做才能检测用户选择哪一部分?

I am using CheckboxListTile to show some todo item in flutter(v3.0.4). This is the code looks like:

CheckboxListTile(
              controlAffinity: ListTileControlAffinity.leading,
              title: Text(element.name,style:element.isCompleted == 1? TextStyle(color: Colors.grey):TextStyle(color: Colors.black)),
              value: element.isCompleted == 1?true:false,
              checkColor: Colors.green,
              selected: element.isCompleted == 1?true:false,

              onChanged: (bool? value) {
                if(value!){
                  element.isCompleted = 1;
                }else{
                  element.isCompleted = 0;
                }
                TodoProvider.updateTodo(element).then((value) => {
                  TodoProvider.getTodos().then((todos) => {
                    buildTodoItems(todos)
                  })
                });
              },
            ))

when the user tap the CheckboxListTile item text, I want to show the todo detail information, when the user tap the checkbox, I want to make the todo task changed to complete. Now I am facing a problem is that I could not detect which part the user tap, all the way will trigger onchange event. I have already read the CheckboxListTile source code, seems no api to do this. Am I misssing something? what should I do to detect which part the user select?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2025-02-20 20:51:13

您可以将标题包装在getruestector()中。现在,当标题被点击时, 将运行手势检测器,而不是on Changed()

在此示例中,如果您点击文本“复选框”,则可以看到实际的复选框值未更新,但eStureDetector is 被调用,如果您查看控制台“敲击”是被打印。

这是一个完整的示例。我希望你明白:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

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

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  var _value = false;
  @override
  Widget build(BuildContext context) {
    return CheckboxListTile(
      title: GestureDetector(
        child: Text('Checkbox'),
        onTap: () {
          print('tapped');
          // you can change the value here too
          // setState(() {
          //   _value = !_value;
          // });
        },
      ),
      value: _value,
      onChanged: (bool? value) {
        setState(() {
          _value = value!;
        });
      },
    );
    ;
  }
}

You can wrap your title in a GestureDetector(). Now when the title is tapped, only the gesture detector will be run, and not the onChanged().

In this example, if you tap on the text "Checkbox" then you can see the actual checkbox value is not being updated but the GestureDetector is being called, and if you look at the console "tapped" is being printed.

enter image description here

Here is a complete example. I hope you understand:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

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

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  var _value = false;
  @override
  Widget build(BuildContext context) {
    return CheckboxListTile(
      title: GestureDetector(
        child: Text('Checkbox'),
        onTap: () {
          print('tapped');
          // you can change the value here too
          // setState(() {
          //   _value = !_value;
          // });
        },
      ),
      value: _value,
      onChanged: (bool? value) {
        setState(() {
          _value = value!;
        });
      },
    );
    ;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文