Text组件
构造方法
const Text(
String this.data, {
Key? key,
this.style,
this.strutStyle,
this.textAlign,
this.textDirection,
this.locale,
this.softWrap,
this.overflow,
this.textScaleFactor,
this.maxLines,
this.semanticsLabel,
this.textWidthBasis,
this.textHeightBehavior,
})
名称 | 功能 |
---|---|
textAlign | 文本对齐方式(center 居中,left 左对齐,right 右对齐,justfy 两端对齐) |
textDirection | 文本方向(ltr 从左至右,rtl 从右至左) |
overflow | 文字超出屏幕之后的处理方式(clip裁剪,fade 渐隐,ellipsis 省略号) |
textScaleFactor | 字体显示倍率 |
maxLines | 文字显示最大行数 |
style | 字体的样式设置 |
TextStyle参数
名称 | 参数 |
---|---|
decoration | 文字装饰线(none 没有线,lineThrough 删除线,overline 上划线,underline 下划线) |
decorationColor | 文字装饰线颜色 |
decorationStyle | 文字装饰线风格([dashed,dotted]虚线,double 两根线,solid 一根实线,wavy 波浪线) |
wordSpacing | 单词间隙(如果是负值,会让单词变得更紧凑) |
letterSpacing | 字母间隙(如果是负值,会让字母变得更紧凑) |
fontStyle | 文字样式(italic 斜体,normal 正常体) |
fontSize | 文字大小 |
color | 文字颜色 |
fontWeight | 字体粗细(bold 粗体,normal 正常体) |
// ignore_for_file: non_constant_identifier_names
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Demo'),
),
body: const HomeContent(),
),
theme: ThemeData(
primarySwatch: Colors.yellow,
),
);
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: const Text(
'我是一个文本我是一个文本我是一个文本我是一个文本',
textAlign: TextAlign.center,
//最大显示行数
maxLines: 2,
//文本超过一行时的显示方式
overflow: TextOverflow.fade,
//把文本放大两倍
textScaleFactor: 2,
//设置字体样式
style: TextStyle(
//字体大小
fontSize: 16.0,
//字体颜色
color: Colors.red,
//字体加粗
fontWeight: FontWeight.w800,
//字体倾斜
fontStyle: FontStyle.italic,
//分割线
decoration: TextDecoration.lineThrough,
//分割线颜色
decorationColor: Colors.black,
//分割线样式
decorationStyle: TextDecorationStyle.dotted,
//字间距
letterSpacing: 3.0
),
),
height: 300,
width: 300,
decoration: BoxDecoration(
color: Colors.yellow,
border: Border.all(color: Colors.blue, width: 2.0),
),
),
);
}
}
Container组件
Container({
Key? key,
this.alignment,
this.padding,
this.color,
this.decoration,
this.foregroundDecoration,
double? width,
double? height,
BoxConstraints? constraints,
this.margin,
this.transform,
this.transformAlignment,
this.child,
this.clipBehavior = Clip.none,
})
参数 | 名称 |
---|---|
alignment | topCenter:顶部居中对齐 topLeft:顶部左对齐 topRight:顶部右对齐 center:水平垂直居中对齐 centerLeft:垂直居中水平居左对齐 centerRight:垂直居中水平居右对齐 bottomCenter 底部居中对齐 bottomLeft:底部居左对齐 bottomRight:底部居右对齐 |
decoration | decoration: BoxDecoration( color: Colors.blue, border: Border.all(color:Colors.red,width: 2.0,), borderRadius: BorderRadius.all( Radius.circular(8.0))) |
margin | margin 属性是表示 Container 与外部其他组件的距离。EdgeInsets.all(20.0), padding padding 就 是 Container 的 内 边 距 , 指Container 边缘与 Child 之间的距离padding: EdgeInsets.all(10.0) |
transform | 让 Container 容易进行一些旋转之类,transform: Matrix4.rotationZ(0.2) |
height | 容器高度 |
width | 容器宽度 |
child | 容器子元素 |
// ignore_for_file: non_constant_identifier_names
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Demo'),
),
body: const HomeContent(),
),
theme: ThemeData(
primarySwatch: Colors.yellow,
),
);
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: const Text(
'我是一个文本我是一个文本我是一个文本我是一个文本',
textAlign: TextAlign.center,
//最大显示行数
maxLines: 2,
//文本超过一行时的显示方式
overflow: TextOverflow.fade,
//把文本放大两倍
textScaleFactor: 2,
//设置字体样式
style: TextStyle(
//字体大小
fontSize: 16.0,
//字体颜色
color: Colors.red,
//字体加粗
fontWeight: FontWeight.w800,
//字体倾斜
fontStyle: FontStyle.italic,
//分割线
decoration: TextDecoration.lineThrough,
//分割线颜色
decorationColor: Colors.black,
//分割线样式
decorationStyle: TextDecorationStyle.dotted,
//字间距
letterSpacing: 3.0
),
),
height: 300,
width: 300,
decoration: BoxDecoration(
color: Colors.yellow,
border: Border.all(
color: Colors.blue,
width: 2.0),
//设置圆角
borderRadius: const BorderRadius.all(
//当里面的值设为150时为一个圆
Radius.circular(15),
)
),
//设置内边距
padding: const EdgeInsets.all(10),
//设置偏移旋转
transform: Matrix4.rotationZ(0.1),
alignment: Alignment.bottomLeft,
),
);
}
}
图片组件
图片组件是显示图片的组件
加载图片常用的两种方式
- Image.asset(), 本地图片
- Image.network() 远程图片
常用属性
名称 | 类型 | 说明 |
---|---|---|
alignment | Alignment | 图片的对齐方式 |
color 和colorBlendMode | 设置图片的背景颜色,通常和 colorBlendMode 配合一起使用这样可以是图片颜色和背景色混合。上面的图片就是进行了颜色的混合,绿色背景和图片红色的混合 | |
fit | BoxFit | fit 属性用来控制图片的拉伸和挤压,这都是根据父容器来的。 BoxFit.fill:全图显示,图片会被拉伸,并充满父容器。 BoxFit.contain:全图显示,显示原比例,可能会有空隙。 BoxFit.cover:显示可能拉伸,可能裁切,充满(图片要充满整个容器,还不变形)。 BoxFit.fitWidth:宽度充满(横向充满),显示可能拉伸,可能裁切。 BoxFit.fitHeight :高度充满(竖向充满),显示可能拉伸,可能裁切。 BoxFit.scaleDown:效果和 contain 差不多,但是此属性不允许显示超过源图片大小,可小不可大。 |
repeat | 平铺 | ImageRepeat.repeat : 横向和纵向都进行重复,直到铺满整个画布。 ImageRepeat.repeatX: 横向重复,纵向不重复。 ImageRepeat.repeatY:纵向重复,横向不重复。 |
width | 宽度 一般结合 ClipOval 才能看到效果 | |
height | 高度 一般结合 ClipOval 才能看到效果 |
加载网络图片
// ignore_for_file: non_constant_identifier_names
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Demo'),
),
body: const HomeContent(),
),
theme: ThemeData(
primarySwatch: Colors.yellow,
),
);
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: Image.network(
"https://cdn.stocksnap.io/img-thumbs/960w/flower-bouquet_X6UBKGVU0Z.jpg",
//容器中图片显示方位
alignment: Alignment.topLeft,
//配置图片的拉伸
fit: BoxFit.cover,
),
width: 300,
height: 300,
decoration: BoxDecoration(
color: Colors.yellow
),
)
);
}
}
加载本地图片
-
在根目录下创建images文件夹,在images文件夹下再创建2.0x,3.0x,4.0x文件夹,然后将我们需要的图片添加到每个文件夹下
-
在pubspec.yaml文件下添加每张图片的路径
assets:
- images/a.jpg
- images/2.0x/a.jpg
- images/3.0x/a.jpg
- images/4.0x/a.jpg
位置如下图:
3. 使用
// ignore_for_file: non_constant_identifier_names
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo'),
),
body: const HomeContent(),
),
theme: ThemeData(
primarySwatch: Colors.yellow,
),
);
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: ClipOval(
child: Image.asset(
"images/a.jpg",
width: 300,
height: 300,
fit: BoxFit.cover,
),
),
),
);
}
}
设置圆角及圆形图片
- 在Container的decoration中通过设置圆角,然后再添加image实现
// ignore_for_file: non_constant_identifier_names
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo'),
),
body: const HomeContent(),
),
theme: ThemeData(
primarySwatch: Colors.yellow,
),
);
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 300,
height: 300,
decoration: BoxDecoration(
color: Colors.yellow,
borderRadius: BorderRadius.circular(150),
image: const DecorationImage(
image: NetworkImage("https://cdn.stocksnap.io/img-thumbs/960w/flower-bouquet_X6UBKGVU0Z.jpg"),
fit: BoxFit.cover
)
),
)
);
}
}
- 通过ClipOval组件类实现
ClipOval对子组件进行裁剪,如果 child 为正方形时剪裁之后是圆形,如果 child 为矩形时,剪裁之后为椭圆形。
我们在裁剪图片时还要限制图片为正方形
// ignore_for_file: non_constant_identifier_names
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo'),
),
body: const HomeContent(),
),
theme: ThemeData(
primarySwatch: Colors.yellow,
),
);
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: ClipOval(
child: Image.network(
"https://cdn.stocksnap.io/img-thumbs/960w/flower-bouquet_X6UBKGVU0Z.jpg",
width: 300,
height: 300,
fit: BoxFit.cover,
),
),
),
);
}
}
3.CircleAvatar()类处理
这种是专门处理头像的,将图片圆形化
// ignore_for_file: prefer_const_constructors, prefer_collection_literals, deprecated_member_use, unused_local_variable, must_be_immutable, prefer_const_literals_to_create_immutables
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo'),
),
body: const HomeContent(),
),
theme: ThemeData(
primarySwatch: Colors.yellow,
),
);
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: CircleAvatar(
backgroundImage:NetworkImage("https://cdn.stocksnap.io/img-thumbs/960w/flower-bouquet_X6UBKGVU0Z.jpg")
),
),
);
}
}
图片占位及错误处理
FadeInImage(
// 正常显示网络图片
image: NetworkImage(LocalImage.TEST_NET_IMG),
// 网络图片加载过程loading中占位图片
placeholder: AssetImage(LocalImage.CAT_LOGO),
width: ScreenAdapter.size(68),
height: ScreenAdapter.size(68),
fit: BoxFit.cover,
// 图片加载错误显示的图片
imageErrorBuilder: (context, error, stackTrace) {
return Image.asset(
LocalImage.CAT_LOGO,
width: ScreenAdapter.size(68),
height: ScreenAdapter.size(68),
);
},
),