Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 为什么setState在使用ListView.Builder时不更新我的UI_Flutter_Dart_Setstate - Fatal编程技术网

Flutter 为什么setState在使用ListView.Builder时不更新我的UI

Flutter 为什么setState在使用ListView.Builder时不更新我的UI,flutter,dart,setstate,Flutter,Dart,Setstate,我正在学习Angela Yu的颤振课程的最后一个模块(创建todo应用程序)。我没有照着这样做,而是使用了我自己的观点。直到现在,setState()方法还没有显示我添加的任务。这是从我的屏幕添加任务 ... Padding( padding: const EdgeInsets.all(25.0), child: TextField( controller: myController,

我正在学习Angela Yu的颤振课程的最后一个模块(创建todo应用程序)。我没有照着这样做,而是使用了我自己的观点。直到现在,setState()方法还没有显示我添加的任务。这是从我的屏幕添加任务

...
Padding(
              padding: const EdgeInsets.all(25.0),
              child: TextField(
                controller: myController,
                autofocus: true,
              ),
            ),
Padding(
              padding: const EdgeInsets.all(20.0),
              child: ElevatedButton(
                onPressed: () {
                  todoList.add(Task(taskN: myController.text));
                  setState(() {
                    TaskScreen();
                  });
                },
                child: Text('Add'),
                autofocus: true,
              ),
            )
Howevet任务是通过热重新加载添加的。这就是TaskScreen()返回的内容

              child: Container(
                margin: EdgeInsets.only(top: 35.0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(20),
                      topRight: Radius.circular(20)),
                ),
                padding: EdgeInsets.only(left: 10.0, right: 10.0),
                width: double.infinity,
                child: ListView.builder(
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    return TaskView(
                        todoList[index].taskN, index, todoList[index].isDone);
                  },
                  itemCount: todoList.length,
                  // widget.todoList.length
                ),
              )

> [Task Class]
class Task {
  String taskN;
  bool isDone;

  Task({this.taskN, this.isDone = false});
}

List<Task> todoList = [
  Task(taskN: 'Buy 1', isDone: false),
  Task(taskN: 'Buy 2', isDone: false),
  Task(taskN: 'Buy 3', isDone: false),
];

那么,我如何解决这个问题以将键入的文本添加到屏幕上?

什么是
TaskTile
,您可以添加它的实现吗?现在添加@MidhunMPI刚刚检查了您的代码,看起来不错,您在哪里显示TaskScreen?
class TaskView extends StatefulWidget {
  final String taskName;
  int indexNo;
  bool ripStarter;
  TaskView(this.taskName, this.indexNo, this.ripStarter);

  @override
  _TaskViewState createState() => _TaskViewState();
}

class _TaskViewState extends State<TaskView> {
  @override
  Widget build(BuildContext context) {
    return TaskTile(
        newStarter: widget.ripStarter,
        toggleCheckBoxState: (bool checkBoxState) {
          setState(() {
            widget.ripStarter = checkBoxState;
          });
        },
        titleText: widget.taskName);
  }
}
class TaskTile extends StatelessWidget {
  final bool newStarter;
  final Function toggleCheckBoxState;
  final String titleText;
  TaskTile(
      {@required this.newStarter,
      @required this.toggleCheckBoxState,
      @required this.titleText});

  @override
  Widget build(BuildContext context) {
    return CheckboxListTile(
      value: newStarter,
      onChanged: toggleCheckBoxState,
      title: Text(
        titleText,
        style: TextStyle(
            decoration: newStarter ? TextDecoration.lineThrough : null),
      ),
    );
  }
}