为了账号安全,请及时绑定邮箱和手机立即绑定

Flutter 21: 易忽略的【小而巧】的技术点汇总 (二)

标签:
Android

  小菜继续整理一些个人不太注意但非常有用的小功能点,可能也是大家熟悉的,只是为了以后使用方便查找。

1. Opacity 透明度

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

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

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

Widget childOpacityWid(BuildContext context) {  return new Column(
    children: <Widget>[      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'))))
    ],
  );
}

webp

2. Chip 标签

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

const 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:(){} 方法,否则不显示。

webp

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 为例:

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

Color.fromARGB(0xFF, 0xEE, 0x50, 0x48)
  1. ARGB 10进制方式:与16进制使用相同,只是需要逐个转成10进制数;

Color.fromARGB(255, 240, 80, 72)
  1. RGBO 16进制方式:最后一个参数为 0.0~1.0 之间透明度;

Color.fromRGBO(0xEE, 0x50, 0x48, 1.0)
  1. RGBO 10进制方式:与上16进制使用相同;

Color.fromRGBO(240, 80, 72, 1.0)
  1. 直接使用16进制方式:只需添加 0x 代表16进制;

Color(0xFFEE5048)

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

webp

4. Text 文字换行

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

  1. softWrap: false
    只有一行内容时,若超过设置最大宽度,是否自动换行,true 为换行,false 为不换行;

  2. overflow: TextOverflow.clip 只有一行内容,不换行时,默认截断超出内容,与设置 clip 属性效果相同;

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

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

Widget childTextWid(BuildContext context) {  return ListView(
    children: <Widget>[
      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))),
    ],
  );
}

webp

5. BoxConstraints 布局约束

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

Widget childBoxImgWid(BuildContext context) {  return Column(
    children: <Widget>[
      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(),
          )),
    ],
  );
}

webp

Widget childBoxImgWid(BuildContext context) {  return Column(
    children: <Widget>[
      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))
    ],
  );
}

webp



作者:阿策神奇


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
165
获赞与收藏
165

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消