FlutterCH 02ch02_first_app
ch02-first-app
第一个 Flutter App:main、runApp、Widget、热重载。完整工程共 100 个文件,其中 60 个文本源文件可在下面在线阅读。
60
在线文本文件
3,337
源码行数
dart / c / yaml
语言
275 KB
打包体积
在线阅读源码
左侧按目录浏览,点击文件切换。右上角可复制代码、复制路径或单独下载文件。在线视图只覆盖文本源码,完整工程请下载 zip。
textxmldartjsonkotlin
60 text files · 3,337 lines
dartlib/src/home_page.dart142 行 · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 首页:一个纯粹的"组装"型 Widget。
//
// 这一章还没有状态(StatefulWidget 在 ch06),页面上的所有文字都是常量,
// 所以整棵子树可以是 const 的——const 子树在热重载/重建时会被框架直接跳过。
import 'package:flutter/material.dart';
/// 一条知识点。用 record 而不是 class:类的语法在 ch03 才讲。
typedef Fact = ({String title, String body});
/// const 列表 = 编译期常量,运行时零分配。
const List<Fact> facts = [
(
title: 'main() → runApp()',
body: '两个调用就完成启动。C 里你要 CreateWindowEx + 消息循环,Go 里要自己起 GUI 库的事件泵。',
),
(
title: '一切皆 Widget',
body: 'Text、Padding、Row、甚至"居中"本身,都是 Widget:配置数据,不是真实控件对象。',
),
(
title: 'build() 是纯函数',
body: '框架可以随时重新调用它。别在 build 里发请求、改全局状态、或者 new 缓存对象。',
),
(
title: '热重载 r',
body: 'flutter run 下按 r 重载代码并保留状态;按 R 重启并丢状态。修改 build 方法基本都能秒级生效。',
),
];
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
final ColorScheme scheme = Theme.of(context).colorScheme;
return Scaffold(
// Scaffold = 一页的骨架:AppBar + body + FAB + 抽屉,都是可选的插槽。
appBar: AppBar(
title: const Text('第一个 Flutter App'),
backgroundColor: scheme.surfaceContainerHighest,
),
// ListView 是滚动容器:桌面窗口拉窄/拉矮时内容仍可滚动。
body: ListView(
padding: const EdgeInsets.all(16),
children: [
_Intro(accent: scheme.primary),
const SizedBox(height: 16),
// 把 const 列表展开成 Widget 列表:与 ch01 的 ... 展开运算符是同一件事。
for (final Fact fact in facts) _FactCard(fact: fact),
],
),
floatingActionButton: FloatingActionButton.extended(
// Key 是给测试/框架定位用的身份标识,相当于给控件挂个稳定 ID。
key: const Key('about-fab'),
onPressed: () => _showAbout(context),
icon: const Icon(Icons.info_outline),
label: const Text('关于'),
),
);
}
/// 无状态也能有交互:这里只是弹一个对话框,不改任何状态。
void _showAbout(BuildContext context) {
showDialog<void>(
context: context,
builder: (BuildContext ctx) => AlertDialog(
title: const Text('关于'),
content: const Text(
'第 2 章示例:main() → runApp() → Widget 树。\n'
'桌面三平台与移动端通用,不依赖任何第三方包。',
),
actions: [
TextButton(
onPressed: () => Navigator.of(ctx).pop(),
child: const Text('好'),
),
],
),
);
}
}
/// 顶部介绍块。参数只用 Color,说明 Widget 之间靠"传值"通信,不共享可变状态。
class _Intro extends StatelessWidget {
const _Intro({required this.accent});
final Color accent;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: accent.withValues(alpha: 0.08),
borderRadius: BorderRadius.circular(16),
border: Border.all(color: accent.withValues(alpha: 0.3)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Widget 树就是你的 UI',
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
color: accent,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
const Text(
'Flutter 里没有"控件对象",只有一层层描述。引擎每帧把描述变成真实的绘制指令。'
'想改界面,就重新描述;框架负责算出最小改动。',
),
],
),
);
}
}
/// 知识卡片。构造函数是 const 的:调用处能用 const 就在编译期把它变成常量。
class _FactCard extends StatelessWidget {
const _FactCard({required this.fact});
final Fact fact;
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.only(bottom: 12),
child: ListTile(
leading: Icon(Icons.arrow_right, color: Theme.of(context).colorScheme.primary),
title: Text(fact.title, style: const TextStyle(fontWeight: FontWeight.w600)),
subtitle: Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(fact.body),
),
isThreeLine: true,
),
);
}
}
代码构成:text 60 KB · xml 41 KB · dart 9 KB · json 4 KB
本地运行
zip → 解压 → 执行$cd examples/ch02-first-app && flutter run -d macos(Linux → -d linux,Windows → -d windows,Android → 设备 id)这个 zip 里有什么
- • 完整工程目录(含 Android / iOS / 桌面平台脚手架)
- • lib / bin / test / native 全部源码与测试
- • pubspec.yaml、锁文件与平台配置文件
- • 已排除 .dart_tool / build / target / ephemeral 等构建产物
已验证
- flutter analyze(0 告警)、flutter test(4 个用例全绿)、flutter build macos --debug(真实构建并启动成功)