KitSwitch class

create_user: zhengzaihong email:1096877329@qq.com create_date: 2022/11/17 create_time: 17:44 describe: 开关按钮组件 / Switch Component

支持 iOS 和 Android 两种风格的开关按钮 Supports both iOS and Android style switches

需要更多自定义样式请使用 ImageSwitch 组件 For more customization, use ImageSwitch component

功能特性 / Features

  • 🍎 支持 iOS 风格 (CupertinoSwitch) / iOS style support
  • 🤖 支持 Android 风格 (Switch) / Android style support
  • 🎨 支持自定义颜色 / Custom colors
  • 📍 支持标签左右位置 / Label position (left/right)
  • 📏 支持缩放 / Scale support
  • 🎯 支持初始化回调 / Initial callback support

基础示例 / Basic Example

// iOS 风格开关
KitSwitch(
  isOpen: true,
  isInnerStyle: true,
  label: Text("开启通知"),
  activeColor: Colors.green,
  onChange: (value) {
    print('开关状态: $value');
  },
)

// Android 风格开关
KitSwitch(
  isOpen: false,
  isInnerStyle: false,
  label: Text("自动更新"),
  activeColor: Colors.blue,
  activeTrackColor: Colors.blue.shade200,
  onChange: (value) => print(value),
)

// 标签在右侧
KitSwitch(
  isOpen: true,
  labelRight: true,
  label: Text("飞行模式"),
  onChange: (value) => print(value),
)

// 自定义缩放和间距
KitSwitch(
  isOpen: false,
  scale: 0.8,
  margin: 10,
  label: Text("省电模式"),
  activeColor: Colors.orange,
  onChange: (value) => print(value),
)

// 自定义颜色
KitSwitch(
  isOpen: true,
  activeColor: Colors.purple,
  activeTrackColor: Colors.purple.shade200,
  inactiveThumbColor: Colors.grey,
  inactiveTrackColor: Colors.grey.shade300,
  label: Text("深色模式"),
  onChange: (value) => print(value),
)

注意事项 / Notes

  • isInnerStyle 为 true 时使用 iOS 风格 / iOS style when true
  • isInnerStyle 为 false 时使用 Android 风格 / Android style when false
  • enableFirstCallBack 为 true 时会在初始化时触发回调 / Triggers callback on init when true
Inheritance
Available extensions

Constructors

KitSwitch({bool isOpen = true, ValueChanged<bool>? onChange, bool labelRight = false, bool? isInnerStyle = false, Widget label = const SizedBox(), bool? enableFirstCallBack = false, double? margin = 0, double scale = 1, Color? activeColor, Color? activeTrackColor, Color? inactiveThumbColor, Color? inactiveTrackColor, Key? key})
const

Properties

activeColor Color?
激活状态颜色 / Active color
final
activeTrackColor Color?
激活状态轨道颜色 / Active track color
final
enableFirstCallBack bool?
是否在初始化时触发回调 / Enable first callback 默认值: false / Default: false
final
hashCode int
The hash code for this object.
no setterinherited
inactiveThumbColor Color?
未激活状态滑块颜色 / Inactive thumb color
final
inactiveTrackColor Color?
未激活状态轨道颜色 / Inactive track color
final
isInnerStyle bool?
开关样式 / Switch style true: iOS 风格 (内嵌样式) / iOS style (inner style) false: Android 风格 (外突样式) / Android style (outer style) 默认值: false / Default: false
final
isOpen bool
是否打开 / Is open 默认值: true / Default: true
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
label Widget
标签组件 / Label widget 默认值: SizedBox() / Default: SizedBox()
final
labelRight bool
标签是否在右侧 / Label on right side 默认值: false (左侧) / Default: false (left side)
final
margin double?
标签与开关的间距 / Margin between label and switch 默认值: 0 / Default: 0
final
onChange ValueChanged<bool>?
状态改变回调 / Change callback
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scale double
缩放比例 / Scale 默认值: 1.0 / Default: 1.0
final

Methods

blurred({double blur = 5, Color blurColor = Colors.white, BorderRadius? borderRadius, double colorOpacity = 0.5, Widget? overlay, AlignmentGeometry alignment = Alignment.center}) Blur

Available on Widget, provided by the BlurExtension extension

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<KitSwitch>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
frosted({double blur = 5, Color frostColor = Colors.white, AlignmentGeometry alignment = Alignment.center, double? height, double? width, double frostOpacity = 0.0, BorderRadius? borderRadius, EdgeInsetsGeometry padding = EdgeInsets.zero}) Blur

Available on Widget, provided by the FrostExtension extension

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited