本文基于 Flutter 3.24 编写,所有示例均已在 TRAE IDE 中验证通过。TRAE IDE 的智能代码补全和实时预览功能,让 Flutter UI 开发效率提升 300%。
02|基础布局部件:构建界面的基石
Container - 万能容器
Container 是 Flutter 中最灵活的布局部件,它集成了装饰、变换、约束等多种功能:
// 基础容器示例
Container(
width: 200,
height: 100,
padding: EdgeInsets.all(16),
margin: EdgeInsets.symmetric(horizontal: 20),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 10,
offset: Offset(0, 5),
),
],
),
child: Text(
'TRAE IDE 智能提示',
style: TextStyle(color: Colors.white, fontSize: 16),
),
)TRAE IDE 亮点:在编写 Container 时,TRAE IDE 会智能推荐常用的 decoration 属性组合,一键生成精美的卡片样式。
Row & Column - 线性布局双剑客
// Row 水平布局示例
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(Icons.home, size: 30),
Icon(Icons.search, size: 30),
Icon(Icons.person, size: 30),
],
)
// Column 垂直布局示例
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('用户名', style: TextStyle(fontWeight: FontWeight.bold)),
SizedBox(height: 8),
TextField(
decoration: InputDecoration(
hintText: '请输入用户名',
border: OutlineInputBorder(),
),
),
],
)03|滚动与列表:打造流畅的数据展示
ListView 性能优化技巧
// 使用 builder 模式优化长列表性能
ListView.builder(
itemCount: 1000,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage('https://example.com/avatar/$index'),
),
title: Text('用户 $index'),
subtitle: Text('使用 TRAE IDE 开发 Flutter'),
trailing: Icon(Icons.arrow_forward_ios),
onTap: () {
// TRAE IDE 智能导航到详情页
Navigator.pushNamed(context, '/user_detail', arguments: index);
},
);
},
)CustomScrollView 复合滚动
CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 200,
flexibleSpace: FlexibleSpaceBar(
title: Text('TRAE IDE Flutter'),
background: Image.network(
'https://example.com/header.jpg',
fit: BoxFit.cover,
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(title: Text('Item $index')),
childCount: 50,
),
),
],
)04|输入与交互:提升用户体验的关键
表单验证最佳实践
class LoginForm extends StatefulWidget {
@override
_LoginFormState createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _emailController,
decoration: InputDecoration(
labelText: '邮箱',
prefixIcon: Icon(Icons.email),
border: OutlineInputBorder(),
),
validator: (value) {
if (value?.isEmpty ?? true) {
return '请输入邮箱';
}
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value!)) {
return '邮箱格式不正确';
}
return null;
},
keyboardType: TextInputType.emailAddress,
),
SizedBox(height: 16),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(
labelText: '密码',
prefixIcon: Icon(Icons.lock),
border: OutlineInputBorder(),
),
obscureText: true,
validator: (value) {
if (value?.isEmpty ?? true) {
return '请输入密码';
}
if (value!.length < 6) {
return '密码长度至少6位';
}
return null;
},
),
SizedBox(height: 24),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
// TRAE IDE 智能提示:处理登录逻辑
_handleLogin();
}
},
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 32, vertical: 12),
child: Text('