From e3cbc2eb501c37610928927b46d0cea6d2769e5d Mon Sep 17 00:00:00 2001 From: wanghongenpin Date: Sun, 1 Mar 2026 23:21:25 +0800 Subject: [PATCH] Add compatibility helpers for Flutter API differences and update color opacity methods --- lib/ui/desktop/left_menus/navigation.dart | 3 ++ .../desktop/setting/request_breakpoint.dart | 11 ++++--- lib/ui/mobile/menu/bottom_navigation.dart | 7 ++-- lib/ui/mobile/menu/drawer.dart | 3 ++ lib/ui/mobile/setting/request_breakpoint.dart | 3 ++ lib/utils/flutter_compat.dart | 33 +++++++++++++++++++ 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 lib/utils/flutter_compat.dart diff --git a/lib/ui/desktop/left_menus/navigation.dart b/lib/ui/desktop/left_menus/navigation.dart index e3e1256..cbf8a7c 100644 --- a/lib/ui/desktop/left_menus/navigation.dart +++ b/lib/ui/desktop/left_menus/navigation.dart @@ -20,6 +20,9 @@ import 'package:proxypin/ui/configuration.dart'; import 'package:proxypin/ui/desktop/preference.dart'; import 'package:url_launcher/url_launcher.dart'; +// Compat helpers (withValues extension) +import 'package:proxypin/utils/flutter_compat.dart'; + ///左侧导航栏 /// @author wanghongen /// 2024/8/6 diff --git a/lib/ui/desktop/setting/request_breakpoint.dart b/lib/ui/desktop/setting/request_breakpoint.dart index b41ecf8..491874a 100644 --- a/lib/ui/desktop/setting/request_breakpoint.dart +++ b/lib/ui/desktop/setting/request_breakpoint.dart @@ -15,6 +15,9 @@ import 'package:proxypin/ui/component/widgets.dart'; import '../../component/app_dialog.dart' show CustomToast; import '../../component/http_method_popup.dart'; +// Compat helpers (withValues extension) +import 'package:proxypin/utils/flutter_compat.dart'; + class RequestBreakpointPage extends StatefulWidget { final int? windowId; @@ -215,7 +218,7 @@ class _RequestBreakpointPageState extends State { }, child: Container( padding: const EdgeInsets.only(top: 10), - decoration: BoxDecoration(border: Border.all(color: Colors.grey.withValues(alpha: 0.2))), + decoration: BoxDecoration(border: Border.all(color: Colors.grey.withOpacity(0.2))), child: Column( children: [ Padding( @@ -249,7 +252,7 @@ class _RequestBreakpointPageState extends State { return InkWell( highlightColor: Colors.transparent, splashColor: Colors.transparent, - hoverColor: primaryColor.withValues(alpha: 0.3), + hoverColor: primaryColor.withOpacity(0.3), onDoubleTap: () => _editRule(rule: rule), onSecondaryTapDown: (details) => _showMenu(details.globalPosition, index: index), onHover: (hover) { @@ -275,9 +278,9 @@ class _RequestBreakpointPageState extends State { }, child: Container( color: selected.contains(index) - ? primaryColor.withValues(alpha: 0.5) + ? primaryColor.withOpacity(0.5) : index.isEven - ? Colors.grey.withValues(alpha: 0.1) + ? Colors.grey.withOpacity(0.1) : null, height: 32, padding: const EdgeInsets.all(5), diff --git a/lib/ui/mobile/menu/bottom_navigation.dart b/lib/ui/mobile/menu/bottom_navigation.dart index 6576133..9733976 100644 --- a/lib/ui/mobile/menu/bottom_navigation.dart +++ b/lib/ui/mobile/menu/bottom_navigation.dart @@ -44,6 +44,9 @@ import '../../component/widgets.dart'; import '../setting/proxy.dart'; import '../setting/request_map.dart'; +// Add compatibility helpers (withValues extension, BuildContext.colorScheme) +import 'package:proxypin/utils/flutter_compat.dart'; + /// @author wanghongen /// 2024/9/30 class ConfigPage extends StatefulWidget { @@ -159,7 +162,7 @@ class _ConfigPageState extends State { leading: Icon(Icons.javascript_outlined, color: color), trailing: arrow, onTap: () => navigator(context, const MobileScript())), - Divider(height: 0, thickness: 0.3, color: Theme.of(context).dividerColor.withOpacity(alpha: 0.22)), + Divider(height: 0, thickness: 0.3, color: Theme.of(context).dividerColor.withOpacity(0.22)), ListTile( title: Text(localizations.breakpoint), leading: Icon(Icons.bug_report_outlined, color: color), @@ -236,7 +239,7 @@ class SettingPage extends StatelessWidget { proxyServer: proxyServer, title: '${localizations.proxy}${isEn ? ' ' : ''}${localizations.port}', textStyle: const TextStyle(fontSize: 16)), - Divider(height: 0, thickness: 0.3, color: Theme.of(context).dividerColor.withOpacity(alpha: 0.22)), + Divider(height: 0, thickness: 0.3, color: Theme.of(context).dividerColor.withOpacity(0.22)), if (Platform.isAndroid) ListTile( title: Text(localizations.systemProxy), diff --git a/lib/ui/mobile/menu/drawer.dart b/lib/ui/mobile/menu/drawer.dart index 354c055..6d54e5d 100644 --- a/lib/ui/mobile/menu/drawer.dart +++ b/lib/ui/mobile/menu/drawer.dart @@ -41,6 +41,9 @@ import 'package:proxypin/ui/mobile/setting/ssl.dart'; import 'package:proxypin/ui/mobile/widgets/about.dart'; import 'package:proxypin/utils/listenable_list.dart'; +// Compat helpers (withValues extension) +import 'package:proxypin/utils/flutter_compat.dart'; + import '../../component/proxy_port_setting.dart'; import '../../component/widgets.dart'; import '../../desktop/setting/external_proxy.dart'; diff --git a/lib/ui/mobile/setting/request_breakpoint.dart b/lib/ui/mobile/setting/request_breakpoint.dart index d63bb6a..8e51bee 100644 --- a/lib/ui/mobile/setting/request_breakpoint.dart +++ b/lib/ui/mobile/setting/request_breakpoint.dart @@ -11,6 +11,9 @@ import 'package:proxypin/network/http/http.dart'; import 'package:proxypin/network/util/logger.dart'; import 'package:proxypin/ui/component/widgets.dart'; +// Compat helpers (withValues extension) +import 'package:proxypin/utils/flutter_compat.dart'; + import '../../component/http_method_popup.dart'; class MobileRequestBreakpointPage extends StatefulWidget { diff --git a/lib/utils/flutter_compat.dart b/lib/utils/flutter_compat.dart new file mode 100644 index 0000000..01b58d3 --- /dev/null +++ b/lib/utils/flutter_compat.dart @@ -0,0 +1,33 @@ +import 'package:flutter/material.dart'; + +/// Compatibility helpers for small Flutter API differences used in this project. +/// +/// Provides: +/// - Color/MaterialColor.withValues({double? alpha, Map? values}) +/// to emulate older/newer helper methods used in the codebase. +/// - BuildContext.colorScheme getter as a convenience. + +extension ColorWithValues on Color { + /// If [alpha] is provided, return this color with that opacity. + /// If [values] is provided, return a MaterialColor constructed from this color value. + /// Otherwise return `this`. + Color withValues({double? alpha, Map? values}) { + if (alpha != null) return withOpacity(alpha); + if (values != null) return MaterialColor(value, values); + return this; + } +} + +extension MaterialColorWithValues on MaterialColor { + /// Mirror above semantics for MaterialColor. + Color withValues({double? alpha, Map? values}) { + if (values != null) return MaterialColor(this.value, values); + if (alpha != null) return Color(this.value).withOpacity(alpha); + return this; + } +} + +extension BuildContextColorScheme on BuildContext { + ColorScheme get colorScheme => Theme.of(this).colorScheme; +} +