带你读《深入浅出Dart》三十、Flutter实战之TODO应用(1)https://developer.aliyun.com/article/1348583?groupCode=tech_library
步骤 4:创建任务编辑页面
我们将创建一个任务编辑页面,用于添加新任务或编辑现有任务的标题、描述和完成状态。在lib/screens/todo_edit_screen.dart中创建一个新的Dart文件,并添加以下代码:
import 'package:flutter/material.dart';import 'package:todo_app/models/todo.dart'; class TodoEditScreen extends StatefulWidget { final Todo? todo; TodoEditScreen({this.todo}); @override _TodoEditScreenState createState() => _TodoEditScreenState();} class _TodoEditScreenState extends State { late TextEditingController _titleController; late TextEditingController _descriptionController; bool _isCompleted = false; @override void initState() { super.initState(); _titleController = TextEditingController(text: widget.todo?.title ?? ''); _descriptionController = TextEditingController(text: widget.todo?.description ?? ''); _isCompleted = widget.todo?.isCompleted ?? false; } @override void dispose() { _titleController.dispose(); _descriptionController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.todo == null ? 'Add Todo' : 'Edit Todo'), ), body: Padding( padding: EdgeInsets.all(16.0), child: Column( children: [ TextField( controller: _titleController, decoration: InputDecoration( labelText: 'Title', ), ), SizedBox(height: 16.0), TextField( controller: _descriptionController, decoration: InputDecoration( labelText: 'Description', ), ), SizedBox(height: 16.0), CheckboxListTile( title: Text('Completed'), value: _isCompleted, onChanged: (value) { setState(() { _isCompleted = value!; }); }, ), SizedBox(height: 16.0), RaisedButton( onPressed: () { _saveTodo(); }, child: Text('Save'), ), ], ), ), ); } void _saveTodo() { final newTodo = Todo( title: _titleController.text, description: _descriptionController.text, isCompleted: _isCompleted, ); Navigator.pop(context, newTodo); }}
带你读《深入浅出Dart》三十、Flutter实战之TODO应用(3)https://developer.aliyun.com/article/1348581?groupCode=tech_library