@@ -11,8 +11,9 @@ export 't_switch_types.dart';
1111
1212/// 严格受控的开关组件。
1313///
14- /// [value] 由父级持有;[onChanged] 为 null 时禁用。文字、图标和加载形态
15- /// 无法由 Material Switch 完整表达,因此底层保留 TDesign 自定义开关实现。
14+ /// [value] 由父级持有;[onChanged] 为 null 时禁用;[loading] 为 true 时
15+ /// 显示加载指示器并禁用交互。文字、图标和加载内容无法由 Material Switch
16+ /// 完整表达,因此底层保留 TDesign 自定义开关实现。
1617class TSwitch extends StatelessWidget {
1718 const TSwitch ({
1819 super .key,
@@ -29,6 +30,9 @@ class TSwitch extends StatelessWidget {
2930 /// 开关内容形态;未传时读取 [TSwitchThemeData.defaultVariant] 。
3031 this .variant,
3132
33+ /// 是否处于加载状态;加载时显示指示器并禁用交互。
34+ this .loading = false ,
35+
3236 /// text 形态的开启文案。
3337 this .openText,
3438
@@ -48,6 +52,9 @@ class TSwitch extends StatelessWidget {
4852 /// 开关内容形态。
4953 final TSwitchVariant ? variant;
5054
55+ /// 是否处于加载状态;加载时显示指示器并禁用交互。
56+ final bool loading;
57+
5158 /// text 形态的开启文案。
5259 final String ? openText;
5360
@@ -60,8 +67,7 @@ class TSwitch extends StatelessWidget {
6067 final resolvedSize = size ?? theme? .defaultSize ?? TSwitchSize .medium;
6168 final resolvedVariant =
6269 variant ?? theme? .defaultVariant ?? TSwitchVariant .filled;
63- final enabled =
64- onChanged != null && resolvedVariant != TSwitchVariant .loading;
70+ final enabled = onChanged != null && ! loading;
6571 final resolved = TSwitchResolve .resolve (
6672 context: context,
6773 enabled: enabled,
@@ -79,6 +85,7 @@ class TSwitch extends StatelessWidget {
7985 thumbView: _buildThumb (
8086 resolved: resolved,
8187 variant: resolvedVariant,
88+ loading: loading,
8289 openText: openText,
8390 closeText: closeText,
8491 ),
@@ -105,9 +112,17 @@ class TSwitch extends StatelessWidget {
105112 Widget ? _buildThumb ({
106113 required TSwitchResolvedStyle resolved,
107114 required TSwitchVariant variant,
115+ required bool loading,
108116 required String ? openText,
109117 required String ? closeText,
110118 }) {
119+ if (loading) {
120+ return TCircleIndicator (
121+ color: resolved.thumbContentOnColor,
122+ size: 16 ,
123+ lineWidth: 3 ,
124+ );
125+ }
111126 return switch (variant) {
112127 TSwitchVariant .text => SizedBox (
113128 width: 16 ,
@@ -130,11 +145,6 @@ class TSwitch extends StatelessWidget {
130145 ),
131146 ),
132147 ),
133- TSwitchVariant .loading => TCircleIndicator (
134- color: resolved.thumbContentOnColor,
135- size: 16 ,
136- lineWidth: 3 ,
137- ),
138148 TSwitchVariant .icon => Icon (
139149 value ? TIcons .check : TIcons .close,
140150 size: 16 ,
0 commit comments