Skip to content

Commit

Permalink
feat: openfoodfacts#1034 - tap to open search card
Browse files Browse the repository at this point in the history
  • Loading branch information
cli1005 committed Apr 6, 2022
1 parent 06b3c16 commit 79722db
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ContinuousScanModel with ChangeNotifier {
_states.clear();
_latestScannedBarcode = null;
await refreshProductList();
for (final String barcode in _productList.barcodes.reversed) {
for (final String barcode in _productList.barcodes) {
_barcodes.add(barcode);
_states[barcode] = ScannedProductState.CACHED;
_latestScannedBarcode = barcode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:smooth_app/pages/onboarding/sample_health_card_page.dart';
import 'package:smooth_app/pages/onboarding/scan_example.dart';
import 'package:smooth_app/pages/onboarding/welcome_page.dart';
import 'package:smooth_app/pages/page_manager.dart';
import 'package:smooth_app/pages/scan/inherited_data_manager.dart';

enum OnboardingPage {
NOT_STARTED,
Expand Down Expand Up @@ -91,7 +92,7 @@ class OnboardingFlowNavigator {
return _wrapWidgetInCustomBackNavigator(
context, page, PreferencesPage(localDatabase));
case OnboardingPage.ONBOARDING_COMPLETE:
return PageManager();
return InheritedDataManager(child: PageManager());
}
}

Expand Down
23 changes: 17 additions & 6 deletions packages/smooth_app/lib/pages/page_manager.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:smooth_app/pages/scan/inherited_data_manager.dart';
import 'package:smooth_app/widgets/tab_navigator.dart';

enum BottomNavigationTab {
Expand Down Expand Up @@ -34,6 +35,7 @@ class PageManagerState extends State<PageManager> {
};

BottomNavigationTab _currentPage = BottomNavigationTab.Scan;
List<Widget> _tabs = <Widget>[];

void _selectTab(BottomNavigationTab tabItem, int index) {
if (tabItem == _currentPage) {
Expand All @@ -50,6 +52,12 @@ class PageManagerState extends State<PageManager> {
@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;
_tabs = <Widget>[
_buildOffstageNavigator(BottomNavigationTab.Profile),
_buildOffstageNavigator(BottomNavigationTab.Scan),
_buildOffstageNavigator(BottomNavigationTab.History),
];

return WillPopScope(
onWillPop: () async {
final bool isFirstRouteInCurrentTab =
Expand All @@ -64,18 +72,21 @@ class PageManagerState extends State<PageManager> {
return isFirstRouteInCurrentTab;
},
child: Scaffold(
body: Stack(children: <Widget>[
_buildOffstageNavigator(BottomNavigationTab.Profile),
_buildOffstageNavigator(BottomNavigationTab.Scan),
_buildOffstageNavigator(BottomNavigationTab.History),
]),
body: Stack(children: _tabs),
bottomNavigationBar: BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
selectedItemColor: Colors.white,
backgroundColor: Theme.of(context).appBarTheme.backgroundColor,
onTap: (int index) {
_selectTab(_pageKeys[index], index);
if (_currentPage == BottomNavigationTab.Scan &&
_pageKeys[index] == BottomNavigationTab.Scan) {
InheritedDataManager.of(context).resetShowSearchCard(true);
_selectTab(_pageKeys[index], index);
} else {
InheritedDataManager.of(context).resetShowSearchCard(false);
_selectTab(_pageKeys[index], index);
}
},
currentIndex: _currentPage.index,
items: <BottomNavigationBarItem>[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class _ProductPageState extends State<ProductPage> {
? FloatingActionButton(
backgroundColor: colorScheme.primary,
onPressed: () {
Navigator.pop(context);
Navigator.maybePop(context);
},
child: const Icon(
Icons.arrow_back,
Expand Down
58 changes: 58 additions & 0 deletions packages/smooth_app/lib/pages/scan/inherited_data_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/material.dart';

class InheritedDataManager extends StatefulWidget {
const InheritedDataManager({
Key? key,
required this.child,
}) : super(key: key);

final Widget child;

static InheritedDataManagerState of(BuildContext context) {
return context
.dependOnInheritedWidgetOfExactType<_InheritedDataManagerProvider>()!
.data;
}

@override
State<InheritedDataManager> createState() => InheritedDataManagerState();
}

class InheritedDataManagerState extends State<InheritedDataManager> {
late bool showSearchCard;

@override
void initState() {
showSearchCard = false;
super.initState();
}

void resetShowSearchCard(bool newValue) {
setState(() {
showSearchCard = newValue;
});
}

@override
Widget build(BuildContext context) {
return _InheritedDataManagerProvider(
data: this,
child: widget.child,
);
}
}

class _InheritedDataManagerProvider extends InheritedWidget {
const _InheritedDataManagerProvider({
Key? key,
required this.data,
required Widget child,
}) : super(key: key, child: child);

final InheritedDataManagerState data;

@override
bool updateShouldNotify(_InheritedDataManagerProvider oldWidget) {
return data.showSearchCard;
}
}
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/pages/scan/scanner_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ScannerOverlay extends StatelessWidget {
const SafeArea(top: true, child: ScanHeader()),
const Spacer(),
SmoothProductCarousel(
showSearchCard: true,
containSearchCard: true,
height: carouselHeight,
),
],
Expand Down
26 changes: 18 additions & 8 deletions packages/smooth_app/lib/widgets/smooth_product_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import 'package:smooth_app/cards/product_cards/smooth_product_card_not_found.dar
import 'package:smooth_app/cards/product_cards/smooth_product_card_thanks.dart';
import 'package:smooth_app/data_models/continuous_scan_model.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_card.dart';
import 'package:smooth_app/pages/scan/inherited_data_manager.dart';
import 'package:smooth_app/pages/scan/scan_product_card.dart';
import 'package:smooth_app/pages/scan/search_page.dart';

class SmoothProductCarousel extends StatefulWidget {
const SmoothProductCarousel({
this.showSearchCard = false,
this.containSearchCard = false,
required this.height,
});

final bool showSearchCard;
final bool containSearchCard;
final double height;

@override
Expand All @@ -28,18 +29,22 @@ class SmoothProductCarousel extends StatefulWidget {
class _SmoothProductCarouselState extends State<SmoothProductCarousel> {
final CarouselController _controller = CarouselController();
List<String> barcodes = <String>[];
bool _returnToSearchCard = false;

int get _searchCardAdjustment => widget.showSearchCard ? 1 : 0;
int get _searchCardAdjustment => widget.containSearchCard ? 1 : 0;

@override
void didChangeDependencies() {
super.didChangeDependencies();
final ContinuousScanModel model = context.watch<ContinuousScanModel>();
setState(() {
barcodes = model.getBarcodes();
});
barcodes = model.getBarcodes();
_returnToSearchCard = InheritedDataManager.of(context).showSearchCard;
if (_controller.ready) {
_controller.animateToPage(barcodes.length - 1 + _searchCardAdjustment);
if (_returnToSearchCard && widget.containSearchCard) {
_controller.animateToPage(0);
} else {
_controller.animateToPage(barcodes.length - 1 + _searchCardAdjustment);
}
}
}

Expand All @@ -50,7 +55,7 @@ class _SmoothProductCarouselState extends State<SmoothProductCarousel> {
itemBuilder: (BuildContext context, int itemIndex, int itemRealIndex) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 2.0),
child: widget.showSearchCard && itemIndex == 0
child: widget.containSearchCard && itemIndex == 0
? SearchCard(height: widget.height)
: _getWidget(itemIndex - _searchCardAdjustment),
);
Expand All @@ -61,6 +66,11 @@ class _SmoothProductCarouselState extends State<SmoothProductCarousel> {
viewportFraction: 0.91,
height: widget.height,
enableInfiniteScroll: false,
onPageChanged: (int index, CarouselPageChangedReason reason) {
if (index > 0 && InheritedDataManager.of(context).showSearchCard) {
InheritedDataManager.of(context).resetShowSearchCard(false);
}
},
),
);
}
Expand Down

0 comments on commit 79722db

Please sign in to comment.