网站首页 > 博客文章 正文
每一种桌面,手机APP软件,侧重于交互,这样来看,这样类型的软件分为界面与逻辑两大部分,初步学习flutter框架,如果一头先扎进dart语言本身里,不是一个好的方法,不如从界面开始,先学会界面,所得所见,理解快一些,再从界面开始,实现逻辑,通信,存储,无非如此。
下面一个例子,来理解各组件的初步方法:
一 软件界面
二 代码实现
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
// 顶级组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('StatelessWidget 学习小例子'),
),
body: Center(
child: UserProfileCard(
userName: 'linuxer',
userBio: 'Flutter开发者 | 数据分析者',
),
),
),
);
}
}
// 自定义的 StatelessWidget
class UserProfileCard extends StatelessWidget {
final String userName;
final String userBio;
// 构造函数:接收外部参数
const UserProfileCard({
Key? key,
required this.userName,
required this.userBio,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white70,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.3),
spreadRadius: 2,
blurRadius: 7,
offset: Offset(0, 3),
)
],
),
width: 300,
child: Column( //线性垂直布局
mainAxisSize: MainAxisSize.min,
children: [
// 头像部分,容器实现
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage('assets/profile.webp'), // 需要添加图片资源
),
),
),
SizedBox(height: 16), //
// 姓名,基础组件text
Text(
userName,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.blue[800],
),
),
SizedBox(height: 8),
// 简介
Text(
userBio,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.grey[600],
height: 1.4,
),
),
SizedBox(height: 16),
// 按钮
ElevatedButton.icon(
icon: Icon(Icons.message, size: 18),
label: Text('发送消息'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue[600],
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
),
onPressed: () {
print('按钮被点击'); // 实际使用时可以添加交互逻辑
},
),
],
),
);
}
}
三 代码分析
- 包含以下典型组件:
- 布局组件:Container, Column, SizedBox, Center
- 基础组件:Text, Icon, ElevatedButton
- 样式组件:BoxDecoration, BoxShadow, TextStyle
- 使用特点:
- 通过构造函数接收参数(userName 和 userBio)
- 使用不可变属性(final 修饰)
- 包含完整的组件树结构
- 使用了多种样式配置
- 包含按钮的点击事件(虽然无状态但可以与父组件交互)
- 使用说明:
- 需要在自己的项目中添加图片资源(在项目目录创建 assets 文件夹,并在 pubspec.yaml 中配置资源路径)
- 可以修改颜色、尺寸等参数查看不同效果
- 点击按钮会在控制台输出信息
这个示例展示了如何:
- 创建自定义 StatelessWidget
- 通过构造函数接收参数
- 组合多个基础组件
- 添加基本样式和交互
- 构建可复用的界面组件
四 总结
通过改变代码中的各参数,重新编译后,看看效果,很快地掌握这个框架
猜你喜欢
- 2025-05-23 Flutter——路由
- 2025-05-23 flutter软件开发笔记18-线程管理
- 2025-05-23 Flutter TextField 边框样式以及提示文本
- 2025-05-23 Flutter缓存数据可以怎么做
- 2025-05-23 flutter软件开发笔记20-flutter web开发
- 2025-05-23 flutter软件开发笔记25-内置组件总结
- 2025-05-23 flutter软件开发笔记17-isolate的机制作用
- 2025-05-23 Flutter实战经验(十二):不同平台操作体验的差异和适配(一)
- 2025-05-23 Flutter GetX 硬核分享:Rx 扩展方法实践指南
- 2025-05-23 Flutter与Android iOS 的双向通信
你 发表评论:
欢迎- 378℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 373℃用AI Agent治理微服务的复杂性问题|QCon
- 364℃初次使用IntelliJ IDEA新建Maven项目
- 361℃IT全明星|IntelliJ IDEA学习笔记(四、idea中怎么创建maven项目)
- 358℃Maven技术方案最全手册(mavena)
- 354℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 352℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 352℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)