Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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
List 颤振开关按钮未更新_List_Flutter_Dart_Button_Switch Statement - Fatal编程技术网

List 颤振开关按钮未更新

List 颤振开关按钮未更新,list,flutter,dart,button,switch-statement,List,Flutter,Dart,Button,Switch Statement,我是个新手,在过去的几天里,我一直在尝试制作一个待办事项列表应用程序。我一直在尝试实现一个功能,用户可以将任务标记为重要任务,然后在listview中突出显示。但是,在我放置switch按钮的页面中,在切换之后,按钮不会更新,即使后端功能正在工作,并且任务被标记为重要。有人能帮我吗?代码如下:- class toDoList extends StatefulWidget { bool data = false; @override createState()

我是个新手,在过去的几天里,我一直在尝试制作一个待办事项列表应用程序。我一直在尝试实现一个功能,用户可以将任务标记为重要任务,然后在listview中突出显示。但是,在我放置switch按钮的页面中,在切换之后,按钮不会更新,即使后端功能正在工作,并且任务被标记为重要。有人能帮我吗?代码如下:-


class toDoList extends StatefulWidget
{
    bool data = false;
    @override
    createState() 
    {
        return new toDoListState();
    }
}

class toDoListState extends State<toDoList>
{
  List<String> tasks = [];
  List<String> completedTasks = [];
  List<String> descriptions = [];
  List<bool> importance = [];
  
    @override
    Widget build(BuildContext context)
    {
        return Scaffold
        (
            body: buildToDoList(),
            floatingActionButton: new FloatingActionButton
            (
                onPressed: addToDoItemScreen, 
                tooltip: 'Add Task',
                child: new Icon(Icons.add),
            ),
        );
    }

    Widget buildToDoList()
    {
        return new ListView.builder
        (
            itemBuilder: (context, index)
            {
                if(index < tasks.length)
                {
                    return row(tasks[index], descriptions[index], index);
                }
            },
        );
    }

    Widget row(String task, String description, int index)
    {                  
        return Dismissible(
        key: UniqueKey(),
        background: Container(color: Colors.red, child: Align(alignment: Alignment.center, child: Text('DELETE', textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 18),))),
        direction: DismissDirection.horizontal,
        onDismissed: (direction) {
        setState(() {
          tasks.removeAt(index);
          if(completedTasks.contains(task))
          {
              completedTasks.removeAt(index);
          }
          descriptions.removeAt(index);
          importance.removeAt(index);
        });
          Scaffold.of(context).showSnackBar(SnackBar(content: Text(task+" dismissed")));
        },
        child: CheckboxListTile(
          controlAffinity: ListTileControlAffinity.leading,
          title: Text(task, style: (completedTasks.contains(task)) ? TextStyle(decoration: TextDecoration.lineThrough) : TextStyle(),),
          subtitle: (importance[index]) ? Text('This is important') : Text('No it\'s not'),
          value: completedTasks.contains(task),
          onChanged: (bool value) {
           setState(() {
              if(!completedTasks.contains(task))
              {
                  completedTasks.add(task);
              }
              else
              {
                  completedTasks.remove(task);
              }
           });
          },
        ));
    }
  
  void addToDoItemScreen()
    {
      int index = tasks.length;
      while(importance.length > tasks.length)
      {
          importance.removeLast();   
      }
      importance.add(false);
      Navigator.of(context).push
      (
        new MaterialPageRoute(
         builder: (context) {
          return new Scaffold(
            appBar: new AppBar(
              title: new Text('Add a new task')
            ),
            body: Form
            (child: Column
            (
              children: <Widget>
              [TextField
              (
                autofocus: true,
                onSubmitted: (name) {
                  addToDoItem(name);
                  //Navigator.pop(context); // Close the add todo screen
              },
              decoration: new InputDecoration(
                hintText: 'Enter something to do...',
                contentPadding: const EdgeInsets.all(20.0),
                border: OutlineInputBorder()
              ),
             ),
             TextField
             (
               //autofocus: true,
               onSubmitted: (val)
               {
                  addDescription(val, index);
               },
               decoration: new InputDecoration(
                hintText: 'Enter a task decription...',
                contentPadding: const EdgeInsets.all(20.0),
                border: OutlineInputBorder()
              ),
             ),
             Center
             (
               child: Switch
               (
                  value: widget.data,
                  onChanged: (val)  
                  {
                    setState(() {
                      widget.data = val;
                      widget.data = !widget.data;
                      print(widget.data);
                    });
                    impTask(index);
                  },
               ),
             )
             ],
             ),)
             );
      }));
    }

    void addToDoItem(String task)
    {
        setState(() {
          tasks.add(task);
          descriptions.add("No description");
        });
    }

    void addDescription(String desc, int index)
    {
        setState(() {
          descriptions[index] = desc;
        });
    }

    void impTask(int index)
    {
        setState(() {
          if(importance[index])
          {
            importance[index] = false;
          }
          else 
          {
            importance[index] = true;
          }
        });
    }
}

类toDoList扩展了StatefulWidget
{
布尔数据=假;
@凌驾
createState()
{
返回新的toDoListState();
}
}
类toDoListState扩展了状态
{
列出任务=[];
列出已完成的任务=[];
列表描述=[];
列表重要性=[];
@凌驾
小部件构建(构建上下文)
{
返回脚手架
(
正文:buildToDoList(),
floatingActionButton:新的floatingActionButton
(
onPressed:addToDoItemScreen,
工具提示:“添加任务”,
子:新图标(Icons.add),
),
);
}
小部件buildToDoList()
{
返回新的ListView.builder
(
itemBuilder:(上下文,索引)
{
如果(索引tasks.length)
{
重要性。removeLast();
}
重要性。添加(false);
导航器.of(上下文).push
(
新材料路线(
生成器:(上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“添加新任务”)
),
正文:表格
(子:列)
(
儿童:
[TextField
(
自动对焦:对,
提交人:(姓名){
addToDoItem(名称);
//Navigator.pop(context);//关闭addtodo屏幕
},
装饰:新的输入装饰(
hintText:“输入要做的事情…”,
contentPadding:const EdgeInsets.all(20.0),
边框:OutlineInputBorder()
),
),
文本字段
(
//自动对焦:对,
提交:(val)
{
addDescription(val,index);
},
装饰:新的输入装饰(
hintText:'输入任务说明…',
contentPadding:const EdgeInsets.all(20.0),
边框:OutlineInputBorder()
),
),
居中
(
孩子:开关
(
值:widget.data,
一旦更改:(val)
{
设置状态(){
widget.data=val;
widget.data=!widget.data;
打印(widget.data);
});
输入任务(索引);
},
),
)
],
),)
);
}));
}
void addToDoItem(字符串任务)
{
设置状态(){
任务。添加(任务);
说明。添加(“无说明”);
});
}
void addDescription(字符串描述,整数索引)
{
设置状态(){
描述[索引]=描述;
});
}
无效impTask(整数索引)
{
设置状态(){
if(重要性[索引])
{
重要性[指数]=假;
}
其他的
{
重要性[指数]=真;
}
});
}
}

将一个
StateFulBuilder
添加到
addToDoItemScreen()

void addToDoItemScreen(){
int index=tasks.length;
while(重要性.length>tasks.length){
重要性。removeLast();
}
重要性。添加(false);
Navigator.of(context.push(newmaterialpageroute)(生成器:(context){
return StatefulBuilder(builder:(context,setState){//这是新的
归还新脚手架(
appBar:new appBar(标题:新建文本(“添加新任务”),
正文:表格(
芝加哥
  void addToDoItemScreen() {
    int index = tasks.length;
    while (importance.length > tasks.length) {
      importance.removeLast();
    }
    importance.add(false);
    Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return StatefulBuilder(builder: (context, setState) { // this is new
        return new Scaffold(
            appBar: new AppBar(title: new Text('Add a new task')),
            body: Form(
              child: Column(
                children: <Widget>[
                  TextField(
                    autofocus: true,
                    onSubmitted: (name) {
                      addToDoItem(name);
                      //Navigator.pop(context); // Close the add todo screen
                    },
                    decoration: new InputDecoration(
                        hintText: 'Enter something to do...',
                        contentPadding: const EdgeInsets.all(20.0),
                        border: OutlineInputBorder()),
                  ),
                  TextField(
                    //autofocus: true,
                    onSubmitted: (val) {
                      addDescription(val, index);
                    },
                    decoration: new InputDecoration(
                        hintText: 'Enter a task decription...',
                        contentPadding: const EdgeInsets.all(20.0),
                        border: OutlineInputBorder()),
                  ),
                  Center(
                    child: Switch(
                      value: widget.data,
                      onChanged: (val) {
                        setState(() {
                          widget.data = val;
                        });
                        impTask(index);
                      },
                    ),
                  )
                ],
              ),
            ));
      });
    }));
  }