From 000c2bed99a5736a172dd772e58797cdafb17963 Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Fri, 18 Feb 2022 17:21:39 +0100 Subject: [PATCH] fix: Scanner buttons not updated (#1140) * fix: Scanner buttons not updated * Refactor: Own file for ScanHeader --- .../data_models/continuous_scan_model.dart | 1 - .../lib/pages/scan/scan_header.dart | 62 +++++++++++++++++++ .../smooth_app/lib/pages/scan/scan_page.dart | 1 - .../lib/pages/scan/scan_page_helper.dart | 48 -------------- .../lib/pages/scan/scanner_overlay.dart | 13 ++-- 5 files changed, 69 insertions(+), 56 deletions(-) create mode 100644 packages/smooth_app/lib/pages/scan/scan_header.dart diff --git a/packages/smooth_app/lib/data_models/continuous_scan_model.dart b/packages/smooth_app/lib/data_models/continuous_scan_model.dart index cdde50a9d8a..4a0a0eb74a9 100644 --- a/packages/smooth_app/lib/data_models/continuous_scan_model.dart +++ b/packages/smooth_app/lib/data_models/continuous_scan_model.dart @@ -33,7 +33,6 @@ class ContinuousScanModel with ChangeNotifier { late DaoProduct _daoProduct; late DaoProductList _daoProductList; - bool get hasMoreThanOneProduct => getBarcodes().length > 1; ProductList get productList => _productList; List getBarcodes() => _barcodes; diff --git a/packages/smooth_app/lib/pages/scan/scan_header.dart b/packages/smooth_app/lib/pages/scan/scan_header.dart new file mode 100644 index 00000000000..b2b7903089f --- /dev/null +++ b/packages/smooth_app/lib/pages/scan/scan_header.dart @@ -0,0 +1,62 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:provider/provider.dart'; +import 'package:smooth_app/data_models/continuous_scan_model.dart'; +import 'package:smooth_app/helpers/ui_helpers.dart'; +import 'package:smooth_app/pages/scan/scan_page_helper.dart'; +import 'package:smooth_app/widgets/ranking_floating_action_button.dart'; + +class ScanHeader extends StatelessWidget { + const ScanHeader({Key? key}) : super(key: key); + + static const Duration _duration = Duration(milliseconds: 50); + static const double _visibleOpacity = 0.8; + static const double _invisibleOpacity = 0.0; + + @override + Widget build(BuildContext context) { + final AppLocalizations appLocalizations = AppLocalizations.of(context)!; + final ContinuousScanModel model = context.watch(); + + final ButtonStyle buttonStyle = ButtonStyle( + shape: MaterialStateProperty.all( + RoundedRectangleBorder( + borderRadius: BorderRadius.circular(18.0), + ), + ), + ); + + return AnimatedOpacity( + opacity: + model.getBarcodes().isNotEmpty ? _visibleOpacity : _invisibleOpacity, + duration: _duration, + child: Padding( + padding: const EdgeInsets.symmetric( + vertical: VERY_SMALL_SPACE, + horizontal: MEDIUM_SPACE, + ), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + ElevatedButton.icon( + style: buttonStyle, + icon: const Icon(Icons.cancel_outlined), + onPressed: model.clearScanSession, + label: Text(appLocalizations.clear), + ), + ElevatedButton.icon( + style: buttonStyle, + icon: const Icon(RankingFloatingActionButton.rankingIconData), + onPressed: () => openPersonalizedRankingPage(context), + label: Text( + appLocalizations.plural_compare_x_products( + model.getBarcodes().length, + ), + ), + ), + ], + ), + ), + ); + } +} diff --git a/packages/smooth_app/lib/pages/scan/scan_page.dart b/packages/smooth_app/lib/pages/scan/scan_page.dart index 1a465c11244..cde74ff46c7 100644 --- a/packages/smooth_app/lib/pages/scan/scan_page.dart +++ b/packages/smooth_app/lib/pages/scan/scan_page.dart @@ -99,7 +99,6 @@ class _ScanPageState extends State { child: Scaffold( body: ScannerOverlay( child: child, - model: _model!, ), ), ); diff --git a/packages/smooth_app/lib/pages/scan/scan_page_helper.dart b/packages/smooth_app/lib/pages/scan/scan_page_helper.dart index 0393e55447f..8ee2aff9ee9 100644 --- a/packages/smooth_app/lib/pages/scan/scan_page_helper.dart +++ b/packages/smooth_app/lib/pages/scan/scan_page_helper.dart @@ -1,14 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:provider/provider.dart'; import 'package:smooth_app/data_models/continuous_scan_model.dart'; -import 'package:smooth_app/helpers/ui_helpers.dart'; import 'package:smooth_app/pages/personalized_ranking_page.dart'; import 'package:smooth_app/pages/product/common/product_query_page_helper.dart'; -import 'package:smooth_app/widgets/ranking_floating_action_button.dart'; - -bool areButtonsRendered(ContinuousScanModel model) => - model.hasMoreThanOneProduct; Future openPersonalizedRankingPage(BuildContext context) async { final ContinuousScanModel model = context.read(); @@ -27,45 +21,3 @@ Future openPersonalizedRankingPage(BuildContext context) async { ); await model.refresh(); } - -Widget buildButtonsRow(BuildContext context, ContinuousScanModel model) { - final AppLocalizations appLocalizations = AppLocalizations.of(context)!; - final ButtonStyle buttonStyle = ButtonStyle( - shape: MaterialStateProperty.all( - RoundedRectangleBorder( - borderRadius: BorderRadius.circular(18.0), - ), - ), - ); - return AnimatedOpacity( - opacity: areButtonsRendered(model) ? 0.8 : 0.0, - duration: const Duration(milliseconds: 50), - child: Padding( - padding: const EdgeInsets.symmetric( - vertical: VERY_SMALL_SPACE, - horizontal: MEDIUM_SPACE, - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - ElevatedButton.icon( - style: buttonStyle, - icon: const Icon(Icons.cancel_outlined), - onPressed: model.clearScanSession, - label: Text(appLocalizations.clear), - ), - ElevatedButton.icon( - style: buttonStyle, - icon: const Icon(RankingFloatingActionButton.rankingIconData), - onPressed: () => openPersonalizedRankingPage(context), - label: Text( - appLocalizations.plural_compare_x_products( - model.getBarcodes().length, - ), - ), - ), - ], - ), - ), - ); -} diff --git a/packages/smooth_app/lib/pages/scan/scanner_overlay.dart b/packages/smooth_app/lib/pages/scan/scanner_overlay.dart index d1c6c6a0c33..cf8759454fa 100644 --- a/packages/smooth_app/lib/pages/scan/scanner_overlay.dart +++ b/packages/smooth_app/lib/pages/scan/scanner_overlay.dart @@ -1,8 +1,9 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; import 'package:smooth_app/data_models/continuous_scan_model.dart'; import 'package:smooth_app/generic_lib/animations/smooth_reveal_animation.dart'; import 'package:smooth_app/generic_lib/widgets/smooth_view_finder.dart'; -import 'package:smooth_app/pages/scan/scan_page_helper.dart'; +import 'package:smooth_app/pages/scan/scan_header.dart'; import 'package:smooth_app/widgets/smooth_product_carousel.dart'; /// This builds all the essential widgets which are displayed above the camera @@ -11,11 +12,9 @@ import 'package:smooth_app/widgets/smooth_product_carousel.dart'; class ScannerOverlay extends StatelessWidget { const ScannerOverlay({ required this.child, - required this.model, }); final Widget child; - final ContinuousScanModel model; static const double carouselHeightPct = 0.55; static const double scannerWidthPct = 0.6; @@ -24,6 +23,7 @@ class ScannerOverlay extends StatelessWidget { @override Widget build(BuildContext context) { + final ContinuousScanModel model = context.watch(); return LayoutBuilder( builder: ( BuildContext context, @@ -36,8 +36,9 @@ class ScannerOverlay extends StatelessWidget { ); final double carouselHeight = constraints.maxHeight * ScannerOverlay.carouselHeightPct; - final double buttonRowHeight = - areButtonsRendered(model) ? ScannerOverlay.buttonRowHeightPx : 0; + final double buttonRowHeight = model.getBarcodes().isNotEmpty + ? ScannerOverlay.buttonRowHeightPx + : 0; final double availableScanHeight = constraints.maxHeight - carouselHeight - buttonRowHeight; @@ -83,7 +84,7 @@ class ScannerOverlay extends StatelessWidget { child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ - SafeArea(top: true, child: buildButtonsRow(context, model)), + const SafeArea(top: true, child: ScanHeader()), const Spacer(), SmoothProductCarousel( showSearchCard: true,