错误:未为类型"任务屏幕"定义方法"setState"。(undefined_method at [todoey] lib/screens/tasks_screen.dart:26)



所以我正在尝试使用回调将任务添加到我的任务列表中。但是我无法在回调函数中访问setState((。让我解释一下,

这是我的add_task_screen,它包含我的modalBottomSheet。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class AddTaskScreen extends StatelessWidget {
String newTaskText;
final Function addNewTask;
AddTaskScreen({@required this.addNewTask});
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
child: Container(
color: Color(0xFF757575),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
)
),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 65.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15.0),
child: Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 30.0
),
),
),
TextField(
style: TextStyle(fontSize: 22.0 ),
autofocus: true,
textAlign: TextAlign.center,
onChanged: (value) {
newTaskText = value;
},
),
SizedBox(
height: 15.0,
),
FlatButton(
color: Colors.lightBlueAccent,
child: Text(
'Add',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
onPressed: () {
addNewTask(newTaskText);
},
)
],
),
),
),
),
),
);
}
}

因此,当我点击"添加"时,我想将我的任务添加到我的任务列表中。但使用回调,只是为了更新我的应用程序的其他部分。所以我在tasks_screen中使用回调,它是add_tasks页面的父级。这是我的任务屏幕页面,

import 'package:flutter/material.dart';
import 'package:todoey/widgets/tasks_list.dart';
import 'package:todoey/screens/add_task_screen.dart';
import 'package:todoey/models/task.dart';
class TasksScreen extends StatelessWidget {
List<Task> tasks = [];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.lightBlueAccent,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.lightBlueAccent,
child: Icon(Icons.add, size: 40.0),
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => AddTaskScreen(
addNewTask: (String newTaskText) {
print(newTaskText);
tasks.add(Task(taskText: newTaskText));

// FIXME: Some issue here...
setState(() {
tasks.add(Task(taskText: newTaskText));
});
Navigator.pop(context);
},
),
isScrollControlled: true
);
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 60.0, left: 30, right: 30.0, bottom: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
CircleAvatar(
child: Icon(Icons.list, size: 30.0, color: Colors.lightBlueAccent,),
backgroundColor: Colors.white,
radius: 30.0,
),
SizedBox(
height: 10.0,
),
Text(
'Todoey',
style: TextStyle(
color: Colors.white,
fontSize: 50.0,
fontWeight: FontWeight.w700,
),
),
Text(
'12 Tasks',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
],
),
),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20.0),
topLeft: Radius.circular(20.0)
),
),
child: TasksList(tasks: tasks),
),
),
],
),
);
}
}

所以我在FIXME注释下的setState((上得到了一个错误。错误是问题的标题。具有讽刺意味的是,任务正在我的控制台中打印出来。但是,当我试图更改状态以在任务列表中添加任务,然后从底部表格中弹出时,我会遇到错误。注意:Task是一个模型。

请有人告诉我如何解决这个问题。提前谢谢。

必须将t.ask_screen转换为Stateful Widget〔已解决〕

相关内容

最新更新