dart 我不知道为什么我创建的iconbutton不工作

gstyhher  于 2024-01-04  发布在  其他
关注(0)|答案(3)|浏览(229)
  1. class TodoItem extends StatelessWidget {
  2. final Todo todo;
  3. const TodoItem({super.key, required this.todo});
  4. @override
  5. Widget build(BuildContext context) {
  6. return ListTile(
  7. contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
  8. selectedTileColor: Colors.red,
  9. shape: RoundedRectangleBorder(
  10. borderRadius: BorderRadius.circular(15)
  11. ),
  12. tileColor: Colors.grey,
  13. leading: **IconButton(
  14. color: Colors.black,
  15. iconSize: 18,
  16. icon: Icon(
  17. todo.isDone ? Icons.check_box : Icons.check_box_outline_blank
  18. ),
  19. onPressed: () {
  20. todo.isDone = !todo.isDone;
  21. },
  22. ),**
  23. title: Text(
  24. "${todo.body}",
  25. style: const TextStyle(
  26. fontSize: 25,
  27. color: Colors.black,
  28. decoration: TextDecoration.lineThrough,
  29. ),
  30. ),
  31. subtitle: const Text(
  32. 'date',
  33. style: TextStyle(
  34. color: Colors.black,
  35. fontSize: 15,
  36. ),
  37. ),
  38. trailing: Container(
  39. height: 30,
  40. width: 30,
  41. decoration: BoxDecoration(
  42. color: Colors.red,
  43. borderRadius: BorderRadius.circular(5),
  44. ),
  45. child: IconButton(
  46. color: Colors.white,
  47. iconSize: 18,
  48. icon: const Icon(Icons.delete),
  49. onPressed: () {},
  50. ),
  51. ),
  52. );
  53. }
  54. }

字符串
这是一个类,但问题是:

  1. leading: IconButton(
  2. color: Colors.black,
  3. iconSize: 18,
  4. icon: Icon(
  5. todo.isDone ? Icons.check_box : Icons.check_box_outline_blank // here is just giving me the checkbox
  6. ),
  7. onPressed: () {
  8. todo.isDone = !todo.isDone;
  9. },
  10. ),


没有尝试其他任何东西.我知道另一种方法可以工作,但它只是一个图标

e5njpo68

e5njpo681#

我发现了问题:首先我必须使用一个有状态的小部件,在onPressed函数中我需要使用setstate函数

7qhs6swi

7qhs6swi2#

您可以在更改todo.isDone的值后使用setState((){}),方法是创建小部件StatefulWidget,或者使用可以将Todo中的isDone的值声明为bool类型的ValueNotifier,并将ListTIle Package 在ValueListenableBuilder中以重新呈现UI并反映更改。

wsewodh2

wsewodh23#

要更新视图,可以使用有状态的小部件和setState。

  1. class TodoItem extends StatefulWidget {
  2. final Todo todo;
  3. const TodoItem({super.key, required this.todo});
  4. @override
  5. State<TodoItem> createState() => _TodoItemState();
  6. }
  7. class _TodoItemState extends State<TodoItem> {
  8. @override
  9. Widget build(BuildContext context) {
  10. return ListTile(
  11. contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
  12. selectedTileColor: Colors.red,
  13. shape: RoundedRectangleBorder(
  14. borderRadius: BorderRadius.circular(15)
  15. ),
  16. tileColor: Colors.grey,
  17. leading: IconButton(
  18. color: Colors.black,
  19. iconSize: 18,
  20. icon: Icon(
  21. widget.todo.isDone ? Icons.check_box : Icons.check_box_outline_blank
  22. ),
  23. onPressed: () {
  24. setState(() {
  25. widget.todo.isDone = !widget.todo.isDone;
  26. });
  27. },
  28. ),
  29. title: Text(
  30. "${widget.todo.body}",
  31. style: const TextStyle(
  32. fontSize: 25,
  33. color: Colors.black,
  34. decoration: TextDecoration.lineThrough,
  35. ),
  36. ),
  37. subtitle: const Text(
  38. 'date',
  39. style: TextStyle(
  40. color: Colors.black,
  41. fontSize: 15,
  42. ),
  43. ),
  44. trailing: Container(
  45. height: 30,
  46. width: 30,
  47. decoration: BoxDecoration(
  48. color: Colors.red,
  49. borderRadius: BorderRadius.circular(5),
  50. ),
  51. child: IconButton(
  52. color: Colors.white,
  53. iconSize: 18,
  54. icon: const Icon(Icons.delete),
  55. onPressed: () {},
  56. ),
  57. ),
  58. );

字符串
{\fnSimHei\bord1\shad1\pos(200,288)}

展开查看全部

相关问题