Skip to content

Commit

Permalink
feat: #1034 - tap to open search card (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
cli1005 authored Apr 7, 2022
1 parent c5780f0 commit 7bf48b0
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 @@ -9,6 +9,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';
import 'package:smooth_app/themes/constant_icons.dart';

enum OnboardingPage {
Expand Down Expand Up @@ -109,7 +110,7 @@ class OnboardingFlowNavigator {
return _wrapWidgetInCustomBackNavigator(
context, page, const ConsentAnalytics());
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),
];

final ThemeData themeData = Theme.of(context);
final bool brightnessCheck = themeData.brightness == Brightness.light;
return WillPopScope(
Expand All @@ -66,19 +74,22 @@ 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(
unselectedItemColor: brightnessCheck ? Colors.black : Colors.grey,
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 @@ -70,7 +70,7 @@ class _ProductPageState extends State<ProductPage> {
? FloatingActionButton(
backgroundColor: colorScheme.primary,
onPressed: () {
Navigator.pop(context);
Navigator.maybePop(context);
},
child: Icon(
ConstantIcons.instance.getBackIcon(),
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 @@ -89,7 +89,7 @@ class ScannerOverlay extends StatelessWidget {
Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: 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 @@ -10,16 +10,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 @@ -29,18 +30,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 @@ -51,7 +56,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 @@ -62,6 +67,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 7bf48b0

Please sign in to comment.