From dfb52223e02ba2c727c8f2561ea6bf388535a349 Mon Sep 17 00:00:00 2001 From: Serhii Date: Tue, 8 Nov 2022 17:49:51 +0200 Subject: [PATCH] fix increasing brightness brightness decreases after leaving "gift card barcode screen" --- lib/main.dart | 2 + .../cards/ionia_gift_card_detail_page.dart | 16 +++-- lib/utils/route_aware.dart | 65 +++++++++++++++++++ 3 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 lib/utils/route_aware.dart diff --git a/lib/main.dart b/lib/main.dart index 3cd5679b3..035552eb7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -40,6 +40,7 @@ import 'package:cake_wallet/wallet_type_utils.dart'; final navigatorKey = GlobalKey(); final rootKey = GlobalKey(); +final RouteObserver routeObserver = RouteObserver(); Future main() async { try { @@ -282,6 +283,7 @@ class AppState extends State with SingleTickerProviderStateMixin { authenticationStore: authenticationStore, navigatorKey: navigatorKey, child: MaterialApp( + navigatorObservers: [routeObserver], navigatorKey: navigatorKey, debugShowCheckedModeBanner: false, theme: settingsStore.theme, diff --git a/lib/src/screens/ionia/cards/ionia_gift_card_detail_page.dart b/lib/src/screens/ionia/cards/ionia_gift_card_detail_page.dart index 1c768fa17..d2ff358ee 100644 --- a/lib/src/screens/ionia/cards/ionia_gift_card_detail_page.dart +++ b/lib/src/screens/ionia/cards/ionia_gift_card_detail_page.dart @@ -11,6 +11,7 @@ import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart'; import 'package:cake_wallet/typography.dart'; import 'package:cake_wallet/utils/show_bar.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; +import 'package:cake_wallet/utils/route_aware.dart'; import 'package:cake_wallet/view_model/ionia/ionia_gift_card_details_view_model.dart'; import 'package:device_display_brightness/device_display_brightness.dart'; import 'package:flutter/material.dart'; @@ -47,10 +48,7 @@ class IoniaGiftCardDetailPage extends BasePage { //highlightColor: Colors.transparent, //splashColor: Colors.transparent, //padding: EdgeInsets.all(0), - onPressed: () { - onClose(context); - DeviceDisplayBrightness.setBrightness(viewModel.brightness); - }, + onPressed: ()=> onClose(context), child: _backButton), ), ), @@ -67,7 +65,6 @@ class IoniaGiftCardDetailPage extends BasePage { @override Widget body(BuildContext context) { - viewModel.increaseBrightness(); reaction((_) => viewModel.redeemState, (ExecutionState state) { if (state is FailureState) { WidgetsBinding.instance.addPostFrameCallback((_) { @@ -84,7 +81,12 @@ class IoniaGiftCardDetailPage extends BasePage { } }); - return ScrollableWithBottomSection( + return RouteAwareWidget( + pushToWidget: ()=> viewModel.increaseBrightness(), + pushToNextWidget: ()=> DeviceDisplayBrightness.setBrightness(viewModel.brightness), + popNextWidget: ()=> viewModel.increaseBrightness(), + popWidget: ()=> DeviceDisplayBrightness.setBrightness(viewModel.brightness), + child: ScrollableWithBottomSection( contentPadding: EdgeInsets.all(24), content: Column( children: [ @@ -163,7 +165,7 @@ class IoniaGiftCardDetailPage extends BasePage { }, ), ), - ); + )); } Widget buildIoniaTile(BuildContext context, {required String title, required String subTitle}) { diff --git a/lib/utils/route_aware.dart b/lib/utils/route_aware.dart new file mode 100644 index 000000000..28c72c4a4 --- /dev/null +++ b/lib/utils/route_aware.dart @@ -0,0 +1,65 @@ +import 'package:flutter/material.dart'; +import 'package:cake_wallet/main.dart'; + +class RouteAwareWidget extends StatefulWidget { + RouteAwareWidget( + {required this.child, + this.pushToWidget, + this.pushToNextWidget, + this.popWidget, + this.popNextWidget}); + + final Widget child; + final Function()? pushToWidget; + final Function()? pushToNextWidget; + final Function()? popWidget; + final Function()? popNextWidget; + + @override + State createState() => RouteAwareWidgetState(); +} + +class RouteAwareWidgetState extends State with RouteAware { + @override + void didChangeDependencies() { + super.didChangeDependencies(); + routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute); + } + + @override + void dispose() { + routeObserver.unsubscribe(this); + super.dispose(); + } + + @override + void didPush() { + if (widget.pushToWidget != null) { + widget.pushToWidget!(); + } + } + + @override + void didPushNext() { + if (widget.pushToNextWidget != null) { + widget.pushToNextWidget!(); + } + } + + @override + void didPop() { + if (widget.popWidget != null) { + widget.popWidget!(); + } + } + + @override + void didPopNext() { + if (widget.popNextWidget != null) { + widget.popNextWidget!(); + } + } + + @override + Widget build(BuildContext context) => widget.child; +}