Skip to content

Commit

Permalink
fix: Scanner buttons not updated (#1140)
Browse files Browse the repository at this point in the history
* fix: Scanner buttons not updated

* Refactor: Own file for ScanHeader
  • Loading branch information
M123-dev authored Feb 18, 2022
1 parent 7783e41 commit 000c2be
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getBarcodes() => _barcodes;
Expand Down
62 changes: 62 additions & 0 deletions packages/smooth_app/lib/pages/scan/scan_header.dart
Original file line number Diff line number Diff line change
@@ -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<ContinuousScanModel>();

final ButtonStyle buttonStyle = ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
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: <Widget>[
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,
),
),
),
],
),
),
);
}
}
1 change: 0 additions & 1 deletion packages/smooth_app/lib/pages/scan/scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class _ScanPageState extends State<ScanPage> {
child: Scaffold(
body: ScannerOverlay(
child: child,
model: _model!,
),
),
);
Expand Down
48 changes: 0 additions & 48 deletions packages/smooth_app/lib/pages/scan/scan_page_helper.dart
Original file line number Diff line number Diff line change
@@ -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<void> openPersonalizedRankingPage(BuildContext context) async {
final ContinuousScanModel model = context.read<ContinuousScanModel>();
Expand All @@ -27,45 +21,3 @@ Future<void> 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>(
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: <Widget>[
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,
),
),
),
],
),
),
);
}
13 changes: 7 additions & 6 deletions packages/smooth_app/lib/pages/scan/scanner_overlay.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -24,6 +23,7 @@ class ScannerOverlay extends StatelessWidget {

@override
Widget build(BuildContext context) {
final ContinuousScanModel model = context.watch<ContinuousScanModel>();
return LayoutBuilder(
builder: (
BuildContext context,
Expand All @@ -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;

Expand Down Expand Up @@ -83,7 +84,7 @@ class ScannerOverlay extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SafeArea(top: true, child: buildButtonsRow(context, model)),
const SafeArea(top: true, child: ScanHeader()),
const Spacer(),
SmoothProductCarousel(
showSearchCard: true,
Expand Down

0 comments on commit 000c2be

Please sign in to comment.