前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Flutter 专题】128 图解 ColorTween 颜色补间动画 & ButtonBar 按钮容器

【Flutter 专题】128 图解 ColorTween 颜色补间动画 & ButtonBar 按钮容器

作者头像
阿策小和尚
发布2021-07-05 15:19:04
6490
发布2021-07-05 15:19:04
举报
文章被收录于专栏:阿策小和尚

和尚在尝试做主题颜色切换时,希望背景色有一个自然的过渡过程,于是了解到 ColorTween 颜色补间差值器,配合 AnimationController 实现两种颜色间的自然过渡;和尚简单尝试一下;

ColorTween

源码分析

ColorTween 的源码很简单,继承自 Tween 补间动画,与 Tween 相同,只是 beginendColor 替代;其中若需要透明状态,可以将 begin / end 设置为 nullColors.transparent 再此代表黑色透明,会淡入淡出黑色;

代码语言:javascript
复制
class ColorTween extends Tween<Color?> {
  ColorTween({ Color? begin, Color? end }) : super(begin: begin, end: end);

  @override
  Color? lerp(double t) => Color.lerp(begin, end, t);
}

案例源码

和尚预先设置好需要主题颜色切换的 UI Widget,之后通过混入 TickerProviderStateMixin,在 initState() 初始化时设置好 AnimationController,将颜色传递给背景色;

代码语言:javascript
复制
AnimationController _controller;
Animation<Color> _colors;
Color _currentColor = Colors.black;

@override
void initState() {
  super.initState();
  _controller = AnimationController(duration: Duration(seconds: 3), vsync: this);
  _colors = ColorTween(begin: _currentColor, end: Colors.amber).animate(_controller);
}

_bodyWid() => Material(
    child: AnimatedBuilder(
        animation: _colors,
        builder: (BuildContext _, Widget childWidget) {
          return Scaffold(backgroundColor: _colors.value, body: _itemListWid());
        }));

通过 AnimationController 控制淡入淡出时机;reset() 重置控制器,forward()beginend 颜色切换;reward()endbegin 颜色切换;repeat() 重复循环切换;

代码语言:javascript
复制
_changeColorWid() => Container(
    color: Colors.white,
    child: Column(children: [
      ListTile(title: Text('切换 ThemeColor:')),
      Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
        _itemColorWid(Colors.deepOrange), _itemColorWid(Colors.teal),
        _itemColorWid(Colors.blue), _itemColorWid(Colors.pink),
        _itemColorWid(Colors.indigoAccent)
      ])
    ]));

_itemColorWid(color) => GestureDetector(
    child: Container(width: 50.0, height: 50.0, color: color),
    onTap: () {
      _colors = ColorTween(begin: _currentColor, end: color).animate(_controller);
      setState(() {
        _controller.reset();
        _controller.forward();
      });
      _currentColor = color;
    });

ButtonBar

和尚在很多场景中设置水平均分或右对齐,为此和尚了解到一个新的容器方式,ButtonBar 默认水平方式放置子 Widget 当水平宽度无法完全放置所有子 Widget 时会竖直方向放置,和尚简单学习一下;

源码分析

代码语言:javascript
复制
const ButtonBar({
    Key key,
    this.alignment,         // 对齐方式
    this.mainAxisSize,      // 主轴上占据空间范围
    this.buttonTextTheme,   // 按钮文本主题
    this.buttonMinWidth,    // 子按钮最小宽度
    this.buttonHeight,      // 子按钮最高度
    this.buttonPadding,     // 子按钮内边距
    this.buttonAlignedDropdown, // 下拉菜单是否与子按钮对齐
    this.layoutBehavior,
    this.overflowDirection, // 子按钮排列顺序
    this.overflowButtonSpacing, // 子按钮之间间距
    this.children = const <Widget>[],
})

简单分析源码,ButtonBar 作为一个无状态的 StatelessWidgetRow 类似,作为一个存放子 Widget 的容器,其中包括了类似于对齐方式等属性方便应用;和尚简单理解为变形的 Row,实际是继承自 Flex_ButtonBarRow

案例尝试

构造方法

ButtonBar 作为一个 Widget 容器,用于水平存放各 Widget,若子 Widget 占据空间范围大于分配空间时,则竖直方向展示;

代码语言:javascript
复制
_buttonBarWid01() => ButtonBar(children: <Widget>[
      RaisedButton(child: Text('Button 01'), onPressed: null),
      RaisedButton(child: Text('Button 02'), onPressed: null) ]);

_buttonBarWid02() => ButtonBar(children: <Widget>[
      RaisedButton(child: Text('Button 01'), onPressed: null),
      RaisedButton(child: Text('Button 02'), onPressed: null),
      RaisedButton(child: Text('Button 03'), onPressed: null),
      RaisedButton(child: Text('Button 04'), onPressed: null) ]);

_buttonBarWid03() => ButtonBar(children: <Widget>[
      RaisedButton(child: Text('Button 01'), onPressed: null),
      RaisedButton(child: Text('Button 02'), onPressed: null),
      RaisedButton(child: Text('Button 03'), onPressed: null),
      RaisedButton(child: Text('Button 04'), onPressed: null),
      RaisedButton(child: Text('Button 05'), onPressed: null) ]);
1. alignment

alignment 为容器内子 Widget 的对齐方式,不设置或为 null 时默认为 end 方式对齐,此时与 ltr / rtl 相关;

代码语言:javascript
复制
_buttonBarWid01(index) {
  MainAxisAlignment alignment;
  if (index == 0) {
    alignment = MainAxisAlignment.start;
  } else if (index == 1) {
    alignment = MainAxisAlignment.center;
  } else if (index == 2) {
    alignment = MainAxisAlignment.spaceAround;
  } else if (index == 3) {
    alignment = MainAxisAlignment.spaceBetween;
  } else if (index == 4) {
    alignment = MainAxisAlignment.spaceEvenly;
  } else {
    alignment = MainAxisAlignment.end;
  }
  return ButtonBar(alignment: alignment, children: <Widget>[
    RaisedButton(child: Text('Button'), onPressed: null),
    RaisedButton(child: Text('${alignment.toString()}'), onPressed: null) ]);
}
2. mainAxisSize

mainAxisSize 为主轴上占据空间范围,与 Row / Column 一致,分为 min / max 最小范围和最大填充范围两种;

代码语言:javascript
复制
_buttonBarWid05(mainAxisSize) => Container(
    color: Colors.blue.withOpacity(0.3),
    child: ButtonBar(mainAxisSize: mainAxisSize, children: <Widget>[
      RaisedButton(child: Text('Button 01'), onPressed: null),
      RaisedButton(child: Text('Button 02'), onPressed: null),
      RaisedButton(child: Text('Button 03'), onPressed: null)
    ]));
3. buttonTextTheme

buttonTextTheme 为子 Widget 按钮主题,主要包括 normal / accent / primary 三种主题样式,分别对应 ThemeData.brightness / accentColor / primaryColor

代码语言:javascript
复制
_buttonBarWid04(theme) =>
    ButtonBar(buttonTextTheme: theme, children: <Widget>[
      RaisedButton(child: Text('Button 01'), onPressed: null),
      RaisedButton(child: Text('Button 02', style: TextStyle(color: Colors.blue)), onPressed: null),
      RaisedButton(child: Text('${theme.toString()}'), onPressed: null),
    ]);
4. buttonMinWidth & buttonHeight

buttonMinWidth & buttonHeight 分别对应子 Widget 默认的最小按钮宽度和按钮高度;

代码语言:javascript
复制
_buttonBarWid06(width, height, alignment) =>
    ButtonBar(buttonMinWidth: width, buttonHeight: height, children: <Widget>[
      RaisedButton(child: Text('Button 01'), onPressed: null),
      RaisedButton(child: Text('Button 02', style: TextStyle(color: Colors.blue)), onPressed: null),
      RaisedButton(child: Text('${alignment.toString()}'), onPressed: null),
    ]);
5. overflowButtonSpacing & buttonPadding

overflowButtonSpacing 对应子按钮外间距,类似于 GridView 元素间间距;buttonPadding 对应子按钮内边距;

代码语言:javascript
复制
_buttonBarWid07(padding, spacing) => ButtonBar(
      overflowButtonSpacing: spacing,
      buttonPadding: EdgeInsets.all(padding),
      children: <Widget>[
        RaisedButton(child: Text('Button 01'), onPressed: null),
        RaisedButton(child: Text('Button 02', style: TextStyle(color: Colors.blue)), onPressed: null),
        RaisedButton(child: Text('Button 03'), onPressed: null)
      ]);
6. overflowDirection

overflowDirection 为若容器内子 Widget 所占范围超过最大限制范围时,垂直排列顺序,和尚理解为顺序和倒序两种;

代码语言:javascript
复制
_buttonBarWid08(direction) =>
    ButtonBar(overflowDirection: direction, children: <Widget>[
      RaisedButton(child: Text('Button 01'), onPressed: null),
      RaisedButton(child: Text('Button 02', style: TextStyle(color: Colors.blue)), onPressed: null),
      RaisedButton(child: Text('Button 03'), onPressed: null),
      RaisedButton(child: Text('${direction.toString()}'), onPressed: null),
    ]);

ColorTween 案例源码 & ButtonBar 案例源码


ColorTweenButtonBar 的应用非常简单,这次和尚在实际场景中进行尝试学习,如有错误,请多多指导!

来源:阿策小和尚

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 阿策小和尚 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ColorTween
    • 源码分析
      • 案例源码
      • ButtonBar
        • 源码分析
          • 案例尝试
            • 构造方法
            • 1. alignment
            • 2. mainAxisSize
            • 3. buttonTextTheme
            • 4. buttonMinWidth & buttonHeight
            • 5. overflowButtonSpacing & buttonPadding
            • 6. overflowDirection
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档