从一颗坏LED看整个灯条:混联电路故障排查全流程与维修技巧
2026/5/10 12:13:37
作为Google推出的跨平台UI框架,Flutter凭借以下优势成为2023年最受欢迎的移动开发技术之一:
根据2023 StackOverflow开发者调查,Flutter已成为最受欢迎的跨平台框架,超过React Native 12个百分点!
# 1. 安装Flutter SDK(Windows示例) git clone https://github.com/flutter/flutter.git -b stable # 2. 添加环境变量 PATH="$PATH:`pwd`/flutter/bin" # 3. 检查依赖 flutter doctorhttps://img-blog.csdnimg.cn/direct/9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d.png
✅ 绿色对勾表示环境配置成功!Android Studio需安装Flutter插件
flutter create flutter_todo cd flutter_todo// lib/models/task.dart class Task { String title; bool isDone; Task({required this.title, this.isDone = false}); // 切换任务状态 void toggleDone() { isDone = !isDone; } }// lib/widgets/task_list.dart import 'package:flutter/material.dart'; import '../models/task.dart'; class TaskList extends StatefulWidget { @override _TaskListState createState() => _TaskListState(); } class _TaskListState extends State<TaskList> { List<Task> tasks = [ Task(title: "学习Flutter基础", isDone: true), Task(title: "完成Todo App", isDone: false), ]; void _addTask(String newTaskTitle) { setState(() { tasks.add(Task(title: newTaskTitle)); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Flutter Todo')), body: ListView.builder( itemCount: tasks.length, itemBuilder: (context, index) { return CheckboxListTile( value: tasks[index].isDone, onChanged: (bool? value) { setState(() { tasks[index].toggleDone(); }); }, title: Text( tasks[index].title, style: TextStyle( decoration: tasks[index].isDone ? TextDecoration.lineThrough : TextDecoration.none, ), ), ); }, ), floatingActionButton: FloatingActionButton( onPressed: () => _showAddTaskDialog(context), child: Icon(Icons.add), ), ); } void _showAddTaskDialog(BuildContext context) { String newTaskTitle = ''; showDialog( context: context, builder: (context) => AlertDialog( title: Text('添加新任务'), content: TextField( onChanged: (value) => newTaskTitle = value, decoration: InputDecoration(hintText: '输入任务内容'), ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text('取消'), ), TextButton( onPressed: () { if (newTaskTitle.isNotEmpty) { _addTask(newTaskTitle); Navigator.pop(context); } }, child: Text('添加'), ), ], ), ); } }// lib/main.dart import 'package:flutter/material.dart'; import 'widgets/task_list.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Todo', theme: ThemeData( primarySwatch: Colors.blue, useMaterial3: true, // 启用Material 3设计语言 ), home: TaskList(), debugShowCheckedModeBanner: false, // 隐藏右上角debug标签 ); } }void _addTask(String newTaskTitle) { setState(() { // ⚠️ 关键:触发UI重建 tasks.add(Task(title: newTaskTitle)); }); }setState()是StatefulWidget的核心方法ListView.builder( itemCount: tasks.length, itemBuilder: (context, index) { return CheckboxListTile(...); }, )ListView.builder实现懒加载,性能优化关键showDialog( context: context, builder: (context) => AlertDialog(...) )Navigator.pop控制对话框关闭https://img-blog.csdnimg.cn/direct/3a2b1c0d9e8f7a6b5c4d3e2f1a0b9c8d.png
💡小技巧:在iOS模拟器运行时会自动切换为Cupertino风格组件
列表性能:大数据量时使用const构造函数
CheckboxListTile( key: ValueKey(task.title), // 添加唯一key ... )状态管理升级:复杂应用建议使用:
构建Release包:
flutter build apk --release flutter build ios --release👉GitHub仓库地址:https://github.com/yourname/flutter-todo
包含以下增强功能:
https://img-blog.csdnimg.cn/direct/5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b.png
通过这个Todo应用,我们实践了: