Flutter switch 开关组件浅析

Flutter switch 开关组件浅析


前言

在日常开发中,switch 开关组件,也是比较常用的组件的,本篇文章将记录switch 简单使用及属性。


Switch 组件的属性

我们先来看一下 Switch 组件的源码

  const Switch({
    super.key,
    required this.value,
    required this.onChanged,
    this.activeColor,
    this.activeTrackColor,
    this.inactiveThumbColor,
    this.inactiveTrackColor,
    this.activeThumbImage,
    this.onActiveThumbImageError,
    this.inactiveThumbImage,
    this.onInactiveThumbImageError,
    this.thumbColor,
    this.trackColor,
    this.materialTapTargetSize,
    this.dragStartBehavior = DragStartBehavior.start,
    this.mouseCursor,
    this.focusColor,
    this.hoverColor,
    this.overlayColor,
    this.splashRadius,
    this.focusNode,
    this.autofocus = false,
  })

从上面源码中,我们可以看到 Switch 有非常多的属性,下面来介绍一下常用的属性

  • activeColor: Color - 激活时原点的颜色。
  • activeThumbImage: ImageProvider - 原点还支持图片,激活时的效果。
  • activeTrackColor: Color - 激活时横条的颜色。
  • inactiveThumbColor: Color - 非激活时原点的颜色。
  • inactiveThumbImage: ImageProvider - 非激活原点的图片效果。
  • inactiveTrackColor: Color - 非激活时横条的颜色。
  • onChanged: ValueChanged - 改变时触发。
  • value: → bool - 切换按钮的值。

需要说明的是,Switch 有一个变体 adaptive,也是我们最常用的一个变体:

        Switch.adaptive(value: model.isSwitch, onChanged: (value){
          print(value);
          model.isSwitch = value;
          model.notifyListeners();
        }),

使用效果:

在这里插入图片描述


总结

本篇文章就简单的记录到这里,比较简单,我们主要记住 Switch.adaptive 这个变体,就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半身风雪

感谢打赏,你的鼓励,是我创作的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值