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

gstyhher  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(149)
class TodoItem extends StatelessWidget {

  final Todo todo;
  const TodoItem({super.key, required this.todo});

  
  @override
  Widget build(BuildContext context) {

    return  ListTile(
      contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
      selectedTileColor: Colors.red,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(15)
      ),
      tileColor: Colors.grey,
      leading: **IconButton(
          color: Colors.black,
          iconSize: 18,
          icon: Icon( 
            todo.isDone ? Icons.check_box : Icons.check_box_outline_blank
            ),
          onPressed: () {
              todo.isDone = !todo.isDone;
          },
        ),**
      title:  Text(
        "${todo.body}",
        style: const TextStyle(
          fontSize: 25,
          color: Colors.black,
          decoration: TextDecoration.lineThrough,
        ),
      ),
      subtitle: const Text(
        'date',
        style: TextStyle(
          color: Colors.black,
          fontSize: 15,
        ),
      ),
      trailing: Container(
        height: 30,
        width: 30,
        decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.circular(5),
        ),
        child: IconButton(
          color: Colors.white,
          iconSize: 18,
          icon: const Icon(Icons.delete),
          onPressed: () {},
        ),
      ),
    );
  }
}

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

leading: IconButton(
          color: Colors.black,
          iconSize: 18,
          icon: Icon( 
            todo.isDone ? Icons.check_box : Icons.check_box_outline_blank // here is just giving me the checkbox
            ),
          onPressed: () {
              todo.isDone = !todo.isDone;
          },
        ),


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

e5njpo68

e5njpo681#

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

7qhs6swi

7qhs6swi2#

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

wsewodh2

wsewodh23#

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

class TodoItem extends StatefulWidget {

   final Todo todo;
  const TodoItem({super.key, required this.todo});

   @override
   State<TodoItem> createState() => _TodoItemState();
   }

   class _TodoItemState extends State<TodoItem> {

  @override
  Widget build(BuildContext context) {

return  ListTile(
  contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
  selectedTileColor: Colors.red,
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(15)
  ),
  tileColor: Colors.grey,
  leading: IconButton(
      color: Colors.black,
      iconSize: 18,
      icon: Icon( 
        widget.todo.isDone ? Icons.check_box : Icons.check_box_outline_blank
        ),
      onPressed: () {
          setState(() {
            widget.todo.isDone = !widget.todo.isDone;
          });
      },
    ),
  title:  Text(
    "${widget.todo.body}",
    style: const TextStyle(
      fontSize: 25,
      color: Colors.black,
      decoration: TextDecoration.lineThrough,
    ),
  ),
  subtitle: const Text(
    'date',
    style: TextStyle(
      color: Colors.black,
      fontSize: 15,
    ),
  ),
  trailing: Container(
    height: 30,
    width: 30,
    decoration: BoxDecoration(
      color: Colors.red,
      borderRadius: BorderRadius.circular(5),
    ),
    child: IconButton(
      color: Colors.white,
      iconSize: 18,
      icon: const Icon(Icons.delete),
      onPressed: () {},
    ),
  ),
);

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

相关问题