Flutter文章目录
提示:如有雷同,请联系作者删除
文章目录
前言
Flutter 是一款开源的跨平台移动应用开发框架,它可以让开发者在单个代码库中构建高性能、美观的 iOS 和 Android 应用。Flutter 采用了现代化的编程语言 Dart,并自带丰富的 UI 组件和工具库。通过 Flutter,开发者可以快速地创建出具有高质量的视觉效果、流畅的用户体验、和响应式的布局的应用,同时避免了传统跨平台框架的繁琐和不稳定。
提示:以下是本篇文章正文内容,下面案例可供参考
一、Image 组件详解
1. 加载远程图片
class MyApp extends StatelessWidget{
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.all(10.0),
width: 100,
height: 100,
color: Colors.red,
child: Image.network(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
fit: BoxFit.cover,
),
),
);
}
}
2. Container实现圆形图片
class MyImage1 extends StatelessWidget{
const MyImage1({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.all(10.0),
width: 100,
height: 100,
decoration: const BoxDecoration(
shape: BoxShape.circle, // 圆形装饰线
image: DecorationImage(
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
),
fit: BoxFit.cover, // 图片填充方式
),
),
),
);
}
}
3. clipOval 实现圆形图片
class MyImage2 extends StatelessWidget{
const MyImage2({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Center(
child: ClipOval(
child: Image.network(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
width: 100,
height: 100,
fit: BoxFit.cover,
),
),
);
}
}
4. circleAvatar 实现圆形图片
class MyImage3 extends StatelessWidget{
const MyImage3({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return const Center(
child: CircleAvatar(
backgroundImage: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
),
radius: 50, // 圆形半径
),
);
}
}
5. 加载本地图片
加载本地图片需要主意细节
- 需要找到pubspec.yaml
- 找到assets把你存在本地的图片放上去
主意格式 格式不对会加载不出来
class MyImage4 extends StatelessWidget{
const MyImage4({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return const Center(
child: CircleAvatar(
backgroundImage: AssetImage(
'images/04.png',
),
radius: 50,
),
);
}
}
二、Icon 组件详解
1. Flutter 使用框架自带的Icon是非常简单啊
代码如下(示例):
class MyApp extends StatelessWidget{
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Center(
child: Column(
children:const [
Icon(
Icons.favorite,
color: Colors.pink,
size: 24.0,
semanticLabel: 'Text to announce in accessibility modes', // 无障碍访问
),
SizedBox(height: 20),
Icon(
Icons.audiotrack,
color: Colors.green, // 图标颜色
size: 30.0, // 图标大小
),
SizedBox(height: 20),
Icon(
Icons.beach_access,
color: Colors.blue,
size: 36.0,
),
SizedBox(height: 20),
Icon(
Icons.access_alarm,
color: Colors.orange,
size: 48.0,
),
SizedBox(height: 20),
Icon(
Icons.access_time,
color: Colors.red,
size: 60.0,
),
]
)
);
}
}
2. 这里主要是介绍自定义图片使用方法
- 去阿里巴巴矢量图标库找到自己想要的图标添加到购物车
- 点击购物车图标弹出右边侧栏点击下载代码
- 在我们项目中新建fonts文件把下载文件里面的iconfont.ttf和iconfont.json放到我们项目的fonts中
- 找打pubspec.yaml文件在文件中找到fonts,中定义自定义图标
- 在lib里面新建utils文件夹在文件夹中新建自己自定义图片的规则
代码如下(示例):
提示:主意Unicode编码就是json里面的Unicode,前面0x是规范
// 比如我在utils里面新建的文件叫my_icon.dart
// 导入图标库
import 'package:flutter/material.dart';
// 自定义图标
class MyIcons {
static const IconData zhifubao = IconData(
0xe654, // 图标的Unicode编码
fontFamily: 'myIcon', // 图标的字体
matchTextDirection: true // 文字方向和图标方向一致
);
static const IconData weixin = IconData(
0xe7ba,
fontFamily: 'myIcon',
matchTextDirection: true
);
static const IconData xiaomi = IconData(
0xe781,
fontFamily: 'myIcon',
matchTextDirection: true
);
}
使用自定义图标
import 'package:flutter/material.dart';
import 'utils/my_icon.dart'; // 导入图标库
void main() {
runApp(MaterialApp(
title: 'Flutter Tutorial',
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Tutorial'),
),
body: Column(
children: const [
MyApp(),
],
)
)
));
}
// container 组件详解
class MyApp extends StatelessWidget{
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Center(
child: Column(
children:const [
// 实现自定义图标
Icon(
MyIcons.zhifubao,
color: Colors.blue,
size: 60.0,
),
SizedBox(height: 20),
Icon(
MyIcons.weixin,
color: Colors.green,
size: 60.0,
),
SizedBox(height: 20),
Icon(
MyIcons.xiaomi,
color: Colors.orange,
size: 60.0,
),
]
)
);
}
}
2.ListView 组件详解
1.垂直列表
代码如下(示例):
class MyApp extends StatelessWidget{
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return ListView(
children: const <Widget>[
ListTile(
leading: Icon( // 左侧图标 主意leading和trailing的他们单单可以放图标 也可以放其他的组件
Icons.favorite,
color: Colors.pink,
size: 24.0,
semanticLabel: 'Text to announce in accessibility modes',
),
title: Text('One-line with leading widget'),
trailing: Icon( // 右侧图标
Icons.arrow_forward_ios_outlined,
color: Colors.grey,
size: 20.0,
),
),
// 线条 用于分割
Divider(),
ListTile(
leading: Icon(
Icons.audiotrack,
color: Colors.green,
size: 30.0,
),
title: Text('One-line with leading widget'),
trailing: Icon(
Icons.arrow_forward_ios_outlined,
color: Colors.grey,
size: 20.0,
),
),
Divider(),
ListTile(
leading: Icon(
Icons.beach_access,
color: Colors.blue,
size: 36.0,
),
title: Text('One-line with leading widget'),
// 右侧图标
trailing: Icon(
Icons.arrow_forward_ios_outlined,
color: Colors.grey,
size: 20.0,
),
),
Divider(),
],
);
}
}
2. 水平列表
class MyApp extends StatelessWidget{
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return ListView(
scrollDirection: Axis.horizontal, // 水平列表
children: <Widget>[
Container(
width: 160.0,
color: Colors.red,
),
Container(
width: 160.0,
color: Colors.blue,
),
Container(
width: 160.0,
color: Colors.green,
),
Container(
width: 160.0,
color: Colors.yellow,
),
Container(
width: 160.0,
color: Colors.orange,
),
],
);
}
}
3. 动态列表的几种写法
提示:个人比较倾向于第一种写法
class MyApp extends StatelessWidget{
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return ListView.builder(
itemCount: 20,
itemExtent: 50.0, // 强制高度为50.0
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text("$index"));
}
);
}
}
class MyApp extends StatelessWidget{
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return ListView.separated(
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text("$index"));
},
separatorBuilder: (BuildContext context, int index) {
return index % 2 == 0 ? Divider(color: Colors.blue) : Divider(color: Colors.red);
}
);
}
}
class MyApp extends StatelessWidget{
const MyApp({Key? key}) : super(key: key);
// ignore: non_constant_identifier_names
List<Widget> _ListData() {
List<Widget> list = [];
for (var i = 0; i < 20; i++) {
list.add(ListTile(title: Text("我是列表$i")));
// 添加分割线 奇数蓝色 偶数红色
if (i % 2 == 0) {
list.add(const Divider(color: Colors.blue));
} else {
list.add(const Divider(color: Colors.red));
}
// 最后一个不添加分割线
if (i == 19) {
list.removeLast();
}
}
return list;
}
Widget build(BuildContext context) {
return ListView(
children: [
Column(
children: _ListData(),
)
],
);
}
}
总结
提示:这里对文章进行总结:
在 Flutter 中,Image 组件用于展示图片,Icon 组件用于展示图标,ListView 组件用于展示列表。Image 组件通过 image 、 width 、 height 、 fit 和 alignment 等属性来展示图片;Icon 组件通过 icon 、 color 和 size 等属性来展示图标;ListView 组件通过 children 、 scrollDirection 、 reverse 、 controller 、 itemExtent 、 padding 、 shrinkWrap 和 physics 等属性来展示列表。使用这些组件,开发者可以快速地构建出具有高质量的视觉效果、流畅的用户体验和响应式的布局的应用。同时,Flutter 采用了现代化的编程语言 Dart,并自带丰富的 UI 组件和工具库,为开发者提供了更加高效和有力的工具和支持。 有些功能还需要大家慢慢去发掘这里就不一一为大家介绍了.