From 2424b31c7573d40171157a478f7810d1d5aa4da8 Mon Sep 17 00:00:00 2001 From: Yasien Mac Mini Date: Thu, 21 Aug 2025 12:11:17 +0200 Subject: [PATCH] adjust screen drightness on card open --- .../android/app/src/main/AndroidManifest.xml | 16 +++ Frontend/ios/Podfile.lock | 6 + .../builder/build_loyalty_card_list.dart | 107 +++++++++++++++++- .../package_tools/mih_cards.dart | 2 +- .../Flutter/GeneratedPluginRegistrant.swift | 2 + Frontend/pubspec.lock | 56 +++++++++ Frontend/pubspec.yaml | 1 + .../flutter/generated_plugin_registrant.cc | 3 + .../windows/flutter/generated_plugins.cmake | 1 + 9 files changed, 189 insertions(+), 5 deletions(-) diff --git a/Frontend/android/app/src/main/AndroidManifest.xml b/Frontend/android/app/src/main/AndroidManifest.xml index 29db9a4d..4d5c1b60 100644 --- a/Frontend/android/app/src/main/AndroidManifest.xml +++ b/Frontend/android/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ xmlns:tools="http://schemas.android.com/tools"> + @@ -42,6 +43,21 @@ + + + + + + + + diff --git a/Frontend/ios/Podfile.lock b/Frontend/ios/Podfile.lock index eca87e34..eb5c2f45 100644 --- a/Frontend/ios/Podfile.lock +++ b/Frontend/ios/Podfile.lock @@ -65,6 +65,8 @@ PODS: - FlutterMacOS - printing (1.0.0): - Flutter + - screen_brightness_ios (0.1.0): + - Flutter - SDWebImage (5.20.0): - SDWebImage/Core (= 5.20.0) - SDWebImage/Core (5.20.0) @@ -96,6 +98,7 @@ DEPENDENCIES: - mobile_scanner (from `.symlinks/plugins/mobile_scanner/darwin`) - path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`) - printing (from `.symlinks/plugins/printing/ios`) + - screen_brightness_ios (from `.symlinks/plugins/screen_brightness_ios/ios`) - share_plus (from `.symlinks/plugins/share_plus/ios`) - shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`) - syncfusion_flutter_pdfviewer (from `.symlinks/plugins/syncfusion_flutter_pdfviewer/ios`) @@ -138,6 +141,8 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/path_provider_foundation/darwin" printing: :path: ".symlinks/plugins/printing/ios" + screen_brightness_ios: + :path: ".symlinks/plugins/screen_brightness_ios/ios" share_plus: :path: ".symlinks/plugins/share_plus/ios" shared_preferences_foundation: @@ -167,6 +172,7 @@ SPEC CHECKSUMS: mobile_scanner: 9157936403f5a0644ca3779a38ff8404c5434a93 path_provider_foundation: 080d55be775b7414fd5a5ef3ac137b97b097e564 printing: 54ff03f28fe9ba3aa93358afb80a8595a071dd07 + screen_brightness_ios: 9953fd7da5bd480f1a93990daeec2eb42d4f3b52 SDWebImage: 73c6079366fea25fa4bb9640d5fb58f0893facd8 share_plus: 50da8cb520a8f0f65671c6c6a99b3617ed10a58a shared_preferences_foundation: 9e1978ff2562383bd5676f64ec4e9aa8fa06a6f7 diff --git a/Frontend/lib/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart b/Frontend/lib/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart index bc4dc3af..a13648eb 100644 --- a/Frontend/lib/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart +++ b/Frontend/lib/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart @@ -1,5 +1,7 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter_speed_dial/flutter_speed_dial.dart'; import 'package:go_router/go_router.dart'; +import 'package:ken_logger/ken_logger.dart'; import 'package:mzansi_innovation_hub/main.dart'; import 'package:mzansi_innovation_hub/mih_components/mih_objects/arguments.dart'; import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_banner_ad.dart'; @@ -20,6 +22,7 @@ import 'package:mzansi_innovation_hub/mih_components/mih_objects/loyalty_card.da import 'package:mzansi_innovation_hub/mih_packages/mzansi_wallet/components/mih_card_display.dart'; import 'package:flutter/material.dart'; import 'package:barcode_widget/barcode_widget.dart'; +import 'package:screen_brightness/screen_brightness.dart'; class BuildLoyaltyCardList extends StatefulWidget { final AppUser signedInUser; @@ -49,6 +52,7 @@ class _BuildLoyaltyCardListState extends State { final TextEditingController _nicknameController = TextEditingController(); final TextEditingController _cardNumberController = TextEditingController(); late int _noFavourites; + double? _originalBrightness; final _formKey = GlobalKey(); void openscanner() async { @@ -474,10 +478,11 @@ class _BuildLoyaltyCardListState extends State { }, ), ], - onWindowTapClose: widget.onCardViewClose ?? - () { - Navigator.pop(context); - }, + onWindowTapClose: () { + widget.onCardViewClose; + resetScreenBrightness(); + context.pop(); + }, windowBody: Column( mainAxisSize: MainAxisSize.max, children: [ @@ -561,6 +566,99 @@ class _BuildLoyaltyCardListState extends State { return count; } + Future setScreenBrightness(double newBrightness) async { + if (!kIsWeb) { + bool canChange = + await ScreenBrightness.instance.canChangeSystemBrightness; + + KenLogger.success("Can change system brightness: $canChange"); + if (canChange) { + // Permission is granted, you can now change the system brightness + ScreenBrightness.instance.system.then((brightness) { + setState(() { + _originalBrightness = brightness; + }); + KenLogger.success("Original brightness: $_originalBrightness"); + }); + await ScreenBrightness.instance + .setSystemScreenBrightness(newBrightness); + KenLogger.success("Brightness set to: $newBrightness"); + } else { + context.pop(); + showDialog( + context: context, + builder: (context) { + return MihPackageAlert( + alertIcon: Icon( + Icons.brightness_7_rounded, + size: 150, + color: MihColors.getSecondaryColor( + MzansiInnovationHub.of(context)!.theme.mode == "Dark"), + ), + alertTitle: "Permission Required", + alertBody: Column( + children: [ + Text( + "Sometimes it can be tough to scan your loyalty card if your phone screen is dim. To make sure your scan is successful every time, we need your permission to temporarily increase your screen brightness.\n\nWould you mind enabling this in your device settings?", + style: TextStyle( + color: MihColors.getSecondaryColor( + MzansiInnovationHub.of(context)!.theme.mode == + "Dark"), + fontSize: 15, + fontWeight: FontWeight.bold, + ), + ), + const SizedBox(height: 25), + MihButton( + onPressed: () async { + context.pop(); + await ScreenBrightness.instance + .setSystemScreenBrightness(newBrightness); + }, + buttonColor: MihColors.getGreenColor( + MzansiInnovationHub.of(context)!.theme.mode == "Dark"), + width: 300, + child: Text( + "Grant Permission", + style: TextStyle( + color: MihColors.getPrimaryColor( + MzansiInnovationHub.of(context)!.theme.mode == + "Dark"), + fontSize: 20, + fontWeight: FontWeight.bold, + ), + ), + ), + ], + ), + alertColour: MihColors.getSecondaryColor( + MzansiInnovationHub.of(context)!.theme.mode == "Dark"), + ); + }, + ); + } + } else { + KenLogger.warning( + "Screen brightness adjustment is not supported on Web."); + // _originalBrightness = 1.0; // Default brightness for web + // await ScreenBrightness.instance.setSystemScreenBrightness(1.0); + // KenLogger.success("Brightness set to default value: 1.0"); + } + } + + void resetScreenBrightness() async { + if (!kIsWeb) { + KenLogger.success( + "Resetting screen brightness to original value: $_originalBrightness"); + if (_originalBrightness != null) { + await ScreenBrightness.instance + .setSystemScreenBrightness(_originalBrightness!); + } + } else { + KenLogger.warning("Screen brightness reset is not supported on Web."); + } + } + @override void initState() { super.initState(); @@ -596,6 +694,7 @@ class _BuildLoyaltyCardListState extends State { height: 100, ), onTap: () { + setScreenBrightness(1.0); viewCardWindow(index, size.width); }, ); diff --git a/Frontend/lib/mih_packages/mzansi_wallet/package_tools/mih_cards.dart b/Frontend/lib/mih_packages/mzansi_wallet/package_tools/mih_cards.dart index 93442c9b..fdd49288 100644 --- a/Frontend/lib/mih_packages/mzansi_wallet/package_tools/mih_cards.dart +++ b/Frontend/lib/mih_packages/mzansi_wallet/package_tools/mih_cards.dart @@ -461,7 +461,7 @@ class _MihCardsState extends State { setState(() { _bannerAd = MihBannerAd(); }); - Navigator.pop(context); + // Navigator.pop(context); }, ); }, diff --git a/Frontend/macos/Flutter/GeneratedPluginRegistrant.swift b/Frontend/macos/Flutter/GeneratedPluginRegistrant.swift index 66bdd02e..c7fd789d 100644 --- a/Frontend/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/Frontend/macos/Flutter/GeneratedPluginRegistrant.swift @@ -14,6 +14,7 @@ import local_auth_darwin import mobile_scanner import path_provider_foundation import printing +import screen_brightness_macos import share_plus import shared_preferences_foundation import syncfusion_pdfviewer_macos @@ -30,6 +31,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) PrintingPlugin.register(with: registry.registrar(forPlugin: "PrintingPlugin")) + ScreenBrightnessMacosPlugin.register(with: registry.registrar(forPlugin: "ScreenBrightnessMacosPlugin")) SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) SyncfusionFlutterPdfViewerPlugin.register(with: registry.registrar(forPlugin: "SyncfusionFlutterPdfViewerPlugin")) diff --git a/Frontend/pubspec.lock b/Frontend/pubspec.lock index fbc20592..f3de8561 100644 --- a/Frontend/pubspec.lock +++ b/Frontend/pubspec.lock @@ -1072,6 +1072,62 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.2" + screen_brightness: + dependency: "direct main" + description: + name: screen_brightness + sha256: b6cb9381b83fef7be74187ea043d54598b9a265b4ef6e40b69345ae28699b13e + url: "https://pub.dev" + source: hosted + version: "2.1.6" + screen_brightness_android: + dependency: transitive + description: + name: screen_brightness_android + sha256: fb5fa43cb89d0c9b8534556c427db1e97e46594ac5d66ebdcf16063b773d54ed + url: "https://pub.dev" + source: hosted + version: "2.1.2" + screen_brightness_ios: + dependency: transitive + description: + name: screen_brightness_ios + sha256: "2493953340ecfe8f4f13f61db50ce72533a55b0bbd58ba1402893feecf3727f5" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + screen_brightness_macos: + dependency: transitive + description: + name: screen_brightness_macos + sha256: "4edf330ad21078686d8bfaf89413325fbaf571dcebe1e89254d675a3f288b5b9" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + screen_brightness_ohos: + dependency: transitive + description: + name: screen_brightness_ohos + sha256: af2680660f7df785bcd2b1bef9b9f3c172191166dd27098f2dfe020c50c3dea4 + url: "https://pub.dev" + source: hosted + version: "2.1.1" + screen_brightness_platform_interface: + dependency: transitive + description: + name: screen_brightness_platform_interface + sha256: "737bd47b57746bc4291cab1b8a5843ee881af499514881b0247ec77447ee769c" + url: "https://pub.dev" + source: hosted + version: "2.1.0" + screen_brightness_windows: + dependency: transitive + description: + name: screen_brightness_windows + sha256: d3518bf0f5d7a884cee2c14449ae0b36803802866de09f7ef74077874b6b2448 + url: "https://pub.dev" + source: hosted + version: "2.1.0" scroll_to_index: dependency: transitive description: diff --git a/Frontend/pubspec.yaml b/Frontend/pubspec.yaml index a1d32b5e..8d7eec34 100644 --- a/Frontend/pubspec.yaml +++ b/Frontend/pubspec.yaml @@ -49,6 +49,7 @@ dependencies: country_code_picker: ^3.3.0 ken_logger: ^0.0.3 go_router: ^16.1.0 + screen_brightness: ^2.1.6 dev_dependencies: flutter_test: diff --git a/Frontend/windows/flutter/generated_plugin_registrant.cc b/Frontend/windows/flutter/generated_plugin_registrant.cc index cd68881b..3632ed0b 100644 --- a/Frontend/windows/flutter/generated_plugin_registrant.cc +++ b/Frontend/windows/flutter/generated_plugin_registrant.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) { registry->GetRegistrarForPlugin("LocalAuthPlugin")); PrintingPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("PrintingPlugin")); + ScreenBrightnessWindowsPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("ScreenBrightnessWindowsPlugin")); SharePlusWindowsPluginCApiRegisterWithRegistrar( registry->GetRegistrarForPlugin("SharePlusWindowsPluginCApi")); SyncfusionPdfviewerWindowsPluginRegisterWithRegistrar( diff --git a/Frontend/windows/flutter/generated_plugins.cmake b/Frontend/windows/flutter/generated_plugins.cmake index 339c6add..6c32f4da 100644 --- a/Frontend/windows/flutter/generated_plugins.cmake +++ b/Frontend/windows/flutter/generated_plugins.cmake @@ -8,6 +8,7 @@ list(APPEND FLUTTER_PLUGIN_LIST geolocator_windows local_auth_windows printing + screen_brightness_windows share_plus syncfusion_pdfviewer_windows url_launcher_windows