Browse Source

[fix]调整好友编辑屏蔽按钮

zk 4 months ago
parent
commit
a391c2733f

+ 15 - 13
lib/module/friend/setting/friend_setting_page.dart

@@ -1,8 +1,6 @@
 import 'dart:io';
 
-import 'package:animated_toggle_switch/animated_toggle_switch.dart';
 import 'package:flutter/cupertino.dart';
-
 import 'package:flutter/material.dart';
 import 'package:flutter/src/widgets/framework.dart';
 import 'package:flutter_screenutil/flutter_screenutil.dart';
@@ -13,11 +11,10 @@ import 'package:location/data/bean/user_info.dart';
 import 'package:location/resource/assets.gen.dart';
 import 'package:location/router/app_pages.dart';
 import 'package:location/utils/common_expand.dart';
-import 'package:location/widget/load_switch.dart';
-
 import '../../../resource/colors.gen.dart';
 import '../../../resource/string.gen.dart';
 import '../../../widget/common_view.dart';
+import '../../../widget/gradient_switch.dart';
 import 'friend_setting_controller.dart';
 
 class FriendSettingPage extends BasePage<FriendSettingController> {
@@ -81,7 +78,10 @@ class FriendSettingPage extends BasePage<FriendSettingController> {
                     height: 54.w,
                     decoration: _getSettingCardDecoration(),
                     child: Center(
-                        child: Text(Platform.isAndroid ? StringName.friendDelete : StringName.blockedFriend,
+                        child: Text(
+                            Platform.isAndroid
+                                ? StringName.friendDelete
+                                : StringName.blockedFriend,
                             style: TextStyle(
                                 fontSize: 15.sp,
                                 color: '#E1261A'.color,
@@ -156,7 +156,7 @@ class FriendSettingPage extends BasePage<FriendSettingController> {
   }
 
   Widget buildInfoSwitchType(String title, bool isSwitchOn,
-      {required LoadFutureCallback future}) {
+      {required Future<bool> Function(bool value) future}) {
     return Container(
       height: 54.w,
       padding: EdgeInsets.symmetric(horizontal: 12.w),
@@ -170,14 +170,16 @@ class FriendSettingPage extends BasePage<FriendSettingController> {
               fontWeight: FontWeight.bold),
         ),
         Spacer(),
-        LoadSwitch(
+        GradientSwitch(
+            unselectedGradient:
+                LinearGradient(colors: ['#EBEBEB'.color, '#EBEBEB'.color]),
+            selectedGradient: LinearGradient(
+                colors: [ColorName.colorPrimary, ColorName.colorPrimary]),
+            indicatorSize: 20.w,
+            width: 46.w,
+            height: 25.w,
             value: isSwitchOn,
-            future: future,
-            height: 24.w,
-            borderWidth: 4.w,
-            loadingColor: ColorName.colorPrimary,
-            selectedColor: ColorName.colorPrimary,
-            unselectedColor: '#EBEBEB'.color)
+            onChanged: future)
       ]),
     );
   }

+ 130 - 0
lib/widget/gradient_switch.dart

@@ -0,0 +1,130 @@
+import 'package:flutter/cupertino.dart';
+import 'package:flutter/material.dart';
+
+class GradientSwitch extends StatefulWidget {
+  final bool value;
+  final Future<bool> Function(bool)? onChanged;
+  final double width;
+  final double height;
+  final Duration animationDuration;
+  final double indicatorSize;
+  final LinearGradient selectedGradient;
+  final LinearGradient unselectedGradient;
+  final Color? loadingColor;
+
+  const GradientSwitch({
+    super.key,
+    required this.value,
+    this.onChanged,
+    this.width = 60.0,
+    this.height = 30.0,
+    this.animationDuration = const Duration(milliseconds: 300),
+    this.indicatorSize = 24.0,
+    this.selectedGradient =
+        const LinearGradient(colors: [Colors.blue, Colors.purpleAccent]),
+    this.unselectedGradient =
+        const LinearGradient(colors: [Color(0xFFEEEEEE), Color(0xFFBDBDBD)]),
+    this.loadingColor,
+  });
+
+  @override
+  State<GradientSwitch> createState() => _GradientSwitchState();
+}
+
+class _GradientSwitchState extends State<GradientSwitch>
+    with SingleTickerProviderStateMixin {
+  late bool _currentValue;
+  bool _isLoading = false;
+
+  @override
+  void initState() {
+    super.initState();
+    _currentValue = widget.value;
+  }
+
+  @override
+  void didUpdateWidget(GradientSwitch oldWidget) {
+    super.didUpdateWidget(oldWidget);
+    if (widget.value != _currentValue && !_isLoading) {
+      _currentValue = widget.value;
+    }
+  }
+
+  Future<void> _handleTap() async {
+    if (_isLoading || widget.onChanged == null) return;
+
+    setState(() => _isLoading = true);
+
+    final targetValue = !_currentValue;
+    bool? success;
+
+    try {
+      success = await widget.onChanged!(targetValue);
+    } finally {
+      setState(() => _isLoading = false);
+
+      if (success != null && mounted) {
+        setState(() => _currentValue = success!);
+      }
+    }
+  }
+
+  @override
+  void dispose() {
+    super.dispose();
+  }
+
+  @override
+  Widget build(BuildContext context) {
+    return GestureDetector(
+      onTap: _handleTap,
+      child: AnimatedContainer(
+        duration: widget.animationDuration,
+        width: widget.width,
+        height: widget.height,
+        decoration: BoxDecoration(
+          borderRadius: BorderRadius.circular(widget.height / 2),
+          gradient: _currentValue
+              ? widget.selectedGradient
+              : widget.unselectedGradient,
+          boxShadow: [
+            BoxShadow(
+              color: Colors.black.withOpacity(0.1),
+              blurRadius: 4,
+              offset: const Offset(0, 2),
+            )
+          ],
+        ),
+        child: AnimatedAlign(
+          duration: widget.animationDuration,
+          alignment:
+              _currentValue ? Alignment.centerRight : Alignment.centerLeft,
+          child: Container(
+            margin: const EdgeInsets.symmetric(horizontal: 3),
+            width: widget.indicatorSize,
+            height: widget.indicatorSize,
+            decoration: BoxDecoration(
+              shape: BoxShape.circle,
+              color: Colors.white,
+              boxShadow: [
+                BoxShadow(
+                  color: Colors.black.withOpacity(0.2),
+                  blurRadius: 4,
+                  offset: const Offset(0, 2),
+                )
+              ],
+            ),
+            child: _isLoading
+                ? Center(
+                    child: CupertinoActivityIndicator(
+                      color: widget.loadingColor,
+                      radius: widget.indicatorSize * 0.3,
+                    ),
+                  )
+                : null, // 非加载状态不显示内容
+          ),
+        ),
+      ),
+    );
+  }
+}

+ 0 - 90
lib/widget/load_switch.dart

@@ -1,90 +0,0 @@
-import 'package:animated_toggle_switch/animated_toggle_switch.dart';
-import 'package:flutter/material.dart';
-
-typedef LoadFutureCallback = Future<bool> Function(bool value);
-
-class LoadSwitch extends StatefulWidget {
-  final bool value;
-  final double height;
-  final double borderWidth;
-  final Color loadingColor;
-  final Color selectedColor;
-  final Color unselectedColor;
-
-  final LoadFutureCallback future;
-
-  const LoadSwitch(
-      {super.key,
-      required this.value,
-      required this.height,
-      required this.future,
-      this.borderWidth = 5,
-      this.loadingColor = Colors.blue,
-      this.selectedColor = Colors.green,
-      this.unselectedColor = Colors.red});
-
-  @override
-  State<LoadSwitch> createState() => _LoadSwitchState();
-}
-
-class _LoadSwitchState extends State<LoadSwitch> {
-  bool isSelected = false;
-
-  @override
-  void initState() {
-    // TODO: implement initState
-    super.initState();
-    isSelected = widget.value;
-  }
-
-  @override
-  Widget build(BuildContext context) {
-    return CustomAnimatedToggleSwitch(
-      height: widget.height,
-      indicatorSize: Size.square(widget.height),
-      current: isSelected,
-      values: const [false, true],
-      onChanged: (value) async {
-        bool newValue = await widget.future(value);
-        setState(() {
-          isSelected = newValue;
-        });
-      },
-      animationDuration: const Duration(milliseconds: 350),
-      // iconArrangement: IconArrangement.overlap,
-      // spacing: -16.0,
-      wrapperBuilder: (context, global, child) => DecoratedBox(
-          decoration: BoxDecoration(
-              color: Color.lerp(
-                  Color.lerp(widget.unselectedColor, widget.selectedColor,
-                      global.position),
-                  Colors.grey,
-                  global.loadingAnimationValue),
-              borderRadius:
-                  BorderRadius.all(Radius.circular(widget.height / 2))),
-          child: child),
-      foregroundIndicatorBuilder: (context, global) {
-        return Stack(
-          fit: StackFit.expand,
-          children: [
-            Padding(
-              padding: EdgeInsets.all(widget.borderWidth),
-              child: const DecoratedBox(
-                  decoration: BoxDecoration(
-                      shape: BoxShape.circle, color: Colors.white)),
-            ),
-            Padding(
-              padding: EdgeInsets.all(widget.borderWidth / 2),
-              child: CircularProgressIndicator(
-                strokeWidth: widget.borderWidth,
-                color: widget.loadingColor
-                    .withOpacity(global.loadingAnimationValue),
-              ),
-            ),
-          ],
-        );
-      },
-      iconBuilder: (context, local, global) => const SizedBox(),
-    );
-  }
-}

+ 0 - 8
pubspec.lock

@@ -53,14 +53,6 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "0.4.0"
-  animated_toggle_switch:
-    dependency: "direct main"
-    description:
-      name: animated_toggle_switch
-      sha256: f2e021b1bbbaa76c664b29f6932ed9a37fbd75870da19053a0cc6cd9f790d83a
-      url: "https://pub.flutter-io.cn"
-    source: hosted
-    version: "0.8.4"
   app_tracking_transparency:
     dependency: "direct main"
     description:

+ 0 - 3
pubspec.yaml

@@ -84,9 +84,6 @@ dependencies:
   #socket连接
   web_socket_channel: 3.0.2
 
-  #switch
-  animated_toggle_switch: 0.8.4
-
   #通讯录选择
   flutter_contacts: ^1.1.9+2