error: The method 'setState' isn't defined for the type 'TasksScreen'. (undefined_method at [todoey] lib/screens/tasks_screen.dart:26)
error: The method 'setState' isn't defined for the type 'TasksScreen'. (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);
},
)
],
),
),
),
),
),
);
}
}
所以在这里,我点击 'Add',我想在我的 task-list 中添加我的任务。但使用回调,只是为了更新我应用程序的其他部分。所以我在 tasks_screen 中使用回调,它是 add_tasks 页面的 parent。
这是我的 tasks_screen 页面,
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() 上收到错误。错误是问题的标题。所以具有讽刺意味的是,任务正在我的控制台中打印出来。但是,当我尝试更改状态以在我的任务列表中添加任务然后弹出 bottomSheet 时,出现错误。
注:任务是一个模型。
请有人告诉我如何解决这个问题。提前致谢。
必须将 t.ask_screen 转换为状态小部件
[已解决]
所以我正在尝试使用回调将任务添加到我的任务列表中。但是我无法在我的回调函数中访问 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);
},
)
],
),
),
),
),
),
);
}
}
所以在这里,我点击 'Add',我想在我的 task-list 中添加我的任务。但使用回调,只是为了更新我应用程序的其他部分。所以我在 tasks_screen 中使用回调,它是 add_tasks 页面的 parent。 这是我的 tasks_screen 页面,
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() 上收到错误。错误是问题的标题。所以具有讽刺意味的是,任务正在我的控制台中打印出来。但是,当我尝试更改状态以在我的任务列表中添加任务然后弹出 bottomSheet 时,出现错误。 注:任务是一个模型。
请有人告诉我如何解决这个问题。提前致谢。
必须将 t.ask_screen 转换为状态小部件 [已解决]