flutter text 自动换行、透明度、Chip流式布局、Colors、BoxConstraints 布局约束

1. Opacity 透明度
和尚以前在处理 Widget 的显隐性时用的是设置 Widget 宽高均为0,方式不太好,偶然间了解到 Opacity,可以通过处理透明度来处理 Widget 的显隐性。

Opacity 可以使子控件透明,可以通过设置 0.0~1.0之间的值来设置透明度;对于 0.0 子控件基本没有绘制过,对于 1.0 会立即绘制而不使用中间缓冲区。这种方式比直接在 Widget中添加和删除子控件更有效。

Tips: opacity 的值必须在 0.0~1.0 之间,类似于 Android 中的 Visible 和 inVisible 效果。

Widget childOpacityWid(BuildContext context) {
  return new Column(
    children: [
      new Opacity(
          opacity: 1.0,
          child: new Container(
              height: 80.0,
              color: Colors.blue,
              child: new Center(child: new Text('当前 opacity = 1.0')))),
      new Opacity(
          opacity: 0.0,
          child: new Container(
              height: 80.0,
              color: Colors.green,
              child: new Center(child: new Text('当前 opacity = 0.0')))),
      new Opacity(
          opacity: 0.5,
          child: new Container(
              height: 80.0,
              color: Colors.redAccent,
              child: new Center(child: new Text('当前 opacity = 0.5'))))
    ],
  );
}

2. Chip 标签

和尚以前自定义的标签都是用 Row 配合其他 Widget 来实现的,后来了解到 Chip 不仅节省了很多时间,效果也很好。配合 Wrap 流式布局可以解决很多问题。

Chip({
Key key,
this.avatar,  // 左侧图标
@required this.label,  // 标签内容,一般是文字
 this.labelStyle,  // 标签样式
 this.labelPadding,  // 标签内边距
 this.deleteIcon,  // 删除图标,自己配置
 this.onDeleted,  // 删除方法,必须调用才会显示删除图标
 this.deleteIconColor,  // 删除图标颜色
 this.deleteButtonTooltipMessage,  // 删除图标的提示消息
 this.shape,  // 形状,主要是整个标签样式,包括圆角等
 this.clipBehavior = Clip.none,
this.backgroundColor,  // 背景颜色
 this.padding,  // 整个标签内边距
 this.materialTapTargetSize,  // 删除图标点击范围,可不处理
 })
Widget childChipWid(var string, var state) {
  Widget childChip;
  if (state == 1) {  // 默认圆角
     childChip = Chip(
       label: Text(string, style: new TextStyle(fontSize: 16.0)),
       deleteIcon: Icon(Icons.clear, color: Colors.black12),
       labelPadding: EdgeInsets.fromLTRB(6.0, 0.0, 6.0, 0.0),
     );
  } else if (state == 2) {  // 设置形状为圆角矩形
     childChip = Chip(
       label: Text(string, style: new TextStyle(fontSize: 16.0)),
       deleteIcon: Icon(Icons.clear, color: Colors.black12),
       labelPadding: EdgeInsets.fromLTRB(2.0, 0.0, 2.0, 0.0),
       shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(3.0)),
       onDeleted: () {},
     );
  } else {  // 添加前置图标样式
     childChip = Chip(
       label: Text(string, style: new TextStyle(fontSize: 16.0)),
       avatar: Icon(Icons.search),
       deleteIcon: Icon(Icons.clear, color: Colors.black12),
       labelPadding: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
       onDeleted: () {},
     );
  }
  return childChip;
}

Tips: 如果要显示删除图标,一定要实现 onDeleted:(){} 方法,否则不显示。

3. Color 颜色
Color 对于每个开发者都很熟悉,对于和尚来说也必须用的属性,Flutter 提供了很多设置颜色的方式,和尚简单整理一下。

Colors 方式
Flutter 提供了很多便利的色值可以直接使用;大多数色值的颜色从 100 到 900,增量为 100,数字越小,颜色越浅,数字越大,颜色越深。重点样本(例如redAccent)仅具有值 100/200/400/700。

Colors.redAccent,Colors.red[200],Colors.red,Colors.red[800],

还有一系列具有常见不透明度的黑色和白色。例如:black54 是纯黑色,具有 54% 的不透明度。

Colors.black54,Colors.black87,

Color 方式
和尚在做 Android 开发时一般是用 16进制的色值,Flutter 同样支持,以 #EE5048 为例:

ARGB 16进制方式:0x 代表16进制,进行拆分,第一个参数为透明度;

Color.fromARGB(0xFF, 0xEE, 0x50, 0x48)

ARGB 10进制方式:与16进制使用相同,只是需要逐个转成10进制数;

Color.fromARGB(255, 240, 80, 72)

RGBO 16进制方式:最后一个参数为 0.0~1.0 之间透明度;

Color.fromRGBO(0xEE, 0x50, 0x48, 1.0)

RGBO 10进制方式:与上16进制使用相同;

Color.fromRGBO(240, 80, 72, 1.0)

直接使用16进制方式:只需添加 0x 代表16进制;

Color(0xFFEE5048)

Tips: 0x 后需要设置透明度 FF (完全不透明) 或其他透明度,如果不设置透明度,默认是全透明。

4. Text 文字换行
Text 是我们日常一定会用到的 Widget,根据设置不同的属性产生不同的样式效果。和尚主要尝试了一下换行时的效果。

softWrap: false

只有一行内容时,若超过设置最大宽度,是否自动换行,true 为换行,false 为不换行;
overflow: TextOverflow.clip 只有一行内容,不换行时,默认截断超出内容,与设置 clip属性效果相同;

overflow: TextOverflow.fade

只有一行内容,不换行时,将超出的文本淡化为透明;当设置多行显示时,会将最后一行底部略透明显示;

overflow: TextOverflow.ellipsis

只有一行内容,不换行时,将超出部分用 … 代替;当设置超过多行时,最后内容以 … 显示。但就目前和尚研究中,无法像 Android 设置 … 在中间或跑马灯效果,如果有哪位大神了解还请多多指教。

Widget childTextWid(BuildContext context) {
  return ListView(
    children: [
      Container(
          height: 50.0,
          color: Colors.green,
          child: Padding(
              padding: EdgeInsets.all(5.0),
              child: Text(
                  'Text 测试,超过内容自动换行!Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?',
                  softWrap: true))),
      Container(
          height: 50.0,
          color: Colors.black54,
          child: Padding(
              padding: EdgeInsets.all(5.0),
              child: Text(
                  'Text 测试,超过内容不换行!默认自动截断,Hello, How are you?Hello, How are you?Hello, How are you?',
                  softWrap: false))),
      Container(
          height: 50.0,
          color: Colors.black38,
          child: Padding(
              padding: EdgeInsets.all(5.0),
              child: Text(
                  'Text 测试,超过内容不换行!手动设置自动截断,Hello, How are you?Hello, How are you?Hello, How are you?',
                  softWrap: false,
                  overflow: TextOverflow.clip))),
      Container(
          height: 50.0,
          color: Colors.redAccent,
          child: Padding(
              padding: EdgeInsets.all(5.0),
              child: Text(
                  'Text 测试,超过内容不换行!渐变隐藏,Hello, How are you?Hello, How are you?Hello, How are you?',
                  softWrap: false,
                  overflow: TextOverflow.fade))),
      Container(
          height: 50.0,
          color: Colors.blue,
          child: Padding(
              padding: EdgeInsets.all(5.0),
              child: Text(
                  'Text 测试,超过内容不换行!省略号样式,Hello, How are you?Hello, How are you?Hello, How are you?',
                  softWrap: false,
                  overflow: TextOverflow.ellipsis))),
      Container(
          color: Colors.blueGrey,
          child: Padding(
              padding: EdgeInsets.all(5.0),
              child: Text(
                  'Text 测试,超过内容换行!设置超出超出行数渐变隐藏,Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?',
                  softWrap: true,
                  maxLines: 2,
                  overflow: TextOverflow.fade))),
      Container(
          color: Colors.brown,
          height: 50.0,
          child: Padding(
              padding: EdgeInsets.all(5.0),
              child: Text(
                  'Text 测试,超过内容换行!设置超出超出行数渐变隐藏,Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?',
                  softWrap: true,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis))),
    ],
  );
}

 

5. BoxConstraints 布局约束
和尚在处理图片时,为了方便适配,借助 Expanded 均分大小而非固定大小,此时和尚想把图片截取中间部分填充满而不拉伸,采用 Image 的 BoxFit 不能很好的实现,和尚学习了一下 BoxConstraints 布局约束方式,根据父类布局的最大最小宽高进行填充。

 

Widget childBoxImgWid(BuildContext context) {
  return Column(
    children: [
      Expanded(
          flex: 1,
          child: ConstrainedBox(
            child: Image.asset(
              'images/icon_hzw01.jpg',
              fit: BoxFit.cover,
            ),
            constraints: new BoxConstraints.expand(),
          )),
      Expanded(
          flex: 1,
          child: ConstrainedBox(
            child: Image.asset(
              'images/icon_hzw02.jpg',
              fit: BoxFit.cover,
            ),
            constraints: new BoxConstraints.expand(),
          )),
      Expanded(
          flex: 1,
          child: ConstrainedBox(
            child: Image.asset('images/icon_hzw03.jpg', fit: BoxFit.cover),
            constraints: new BoxConstraints.expand(),
          )),
    ],
  );
}

 

Widget childBoxImgWid(BuildContext context) {
  return Column(
    children: [
      Expanded(
          flex: 1,
          child: Image.asset('images/icon_hzw01.jpg', fit: BoxFit.cover)),
      Expanded(
          flex: 1,
          child: Image.asset('images/icon_hzw02.jpg', fit: BoxFit.cover)),
      Expanded(
          flex: 1,
          child: Image.asset('images/icon_hzw03.jpg', fit: BoxFit.cover))
    ],
  );
}

 

 

————————————————
版权声明:本文为CSDN博主「噼里啪啦酱牛肉」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_31716637/article/details/113072336

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值