任务屏幕.dart
import 'package:flutter/material.dart';
import 'package:todoey_app/widgets/tasks_list.dart';
import 'package:todoey_app/screens/add_task_screen.dart';
import 'package:todoey_app/models/task.dart';
class TasksScreen extends StatefulWidget {
@override
State<TasksScreen> createState() => _TasksScreenState();
}
class _TasksScreenState extends State<TasksScreen> {
List<Task> tasks = [
Task(name: 'Buy milk'),
Task(name: 'Buy eggs'),
Task(name: 'Buy bread'),
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.lightBlueAccent,
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => AddTaskScreen(
(newTaskTitle) {
// print(newTaskTitle);
setState(() {
tasks.add(Task(name: newTaskTitle));
});
Navigator.pop(context);
},
),
);
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.only(
top: 60.0, left: 30.0, 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(
'${tasks.length} 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),
),
),
],
),
);
}
}
add_task_screen.dart
import 'package:flutter/material.dart';
class AddTaskScreen extends StatelessWidget {
final Function addTaskCallback;
AddTaskScreen(this.addTaskCallback);
@override
Widget build(BuildContext context) {
// String? newTaskTitle;
// String newTaskTitle = '';
String newTaskTitle = "It's Working";
// String newTaskTitle;
return Container(
color: Color(0xff757575),
child: Container(
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 30.0,
),
),
TextField(
autofocus: true,
textAlign: TextAlign.center,
onChanged: (newText) {
// print(newText);
newTaskTitle = newText;
},
),
TextButton(
onPressed: () {
print(newTaskTitle);
addTaskCallback(newTaskTitle);
},
child: Text(
'Add',
style: TextStyle(color: Colors.white),
),
style: TextButton.styleFrom(
backgroundColor: Colors.lightBlueAccent,
),
)
],
),
),
);
}
}
我已经初始化了newTaskTitle,但是当我在TextField中键入新文本并尝试将其添加到任务列表中时,它只接受我在开始时初始化的值newTaskTitle =“It 's Working”,而不是接受来自textField的新值;。
我该怎么解决呢?
1条答案
按热度按时间w6mmgewl1#
您可以使用ValueSetter进行此操作。
示例: