From 9a2096d0e9dbb8e107024ba85c3b1de961014255 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Sat, 8 Jan 2022 15:20:30 +0100 Subject: [PATCH] fix: #899 - avoid to reload the product in onboarding Impacted file: * `preferences_page.dart`: split in two the process - load product and then display it with preferences --- .../pages/onboarding/preferences_page.dart | 154 ++++++++++-------- 1 file changed, 86 insertions(+), 68 deletions(-) diff --git a/packages/smooth_app/lib/pages/onboarding/preferences_page.dart b/packages/smooth_app/lib/pages/onboarding/preferences_page.dart index 7fd05230c5c..1a78eb3e366 100644 --- a/packages/smooth_app/lib/pages/onboarding/preferences_page.dart +++ b/packages/smooth_app/lib/pages/onboarding/preferences_page.dart @@ -18,10 +18,10 @@ class PreferencesPage extends StatefulWidget { State createState() => _PreferencesPageState(); } +// Just here to load the product and pass it to the next Widget class _PreferencesPageState extends State { late Future _initFuture; late Product _product; - bool _isProductExpanded = false; @override void didChangeDependencies() { @@ -39,12 +39,7 @@ class _PreferencesPageState extends State { } @override - Widget build(BuildContext context) { - final ProductPreferences productPreferences = - context.watch(); - final UserPreferences userPreferences = context.watch(); - final AppLocalizations appLocalizations = AppLocalizations.of(context)!; - return FutureBuilder( + Widget build(BuildContext context) => FutureBuilder( future: _initFuture, builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasError) { @@ -53,74 +48,97 @@ class _PreferencesPageState extends State { if (snapshot.connectionState != ConnectionState.done) { return const CircularProgressIndicator(); } - final List pageData = [ - Padding( - padding: const EdgeInsets.only( - right: LARGE_SPACE, - left: LARGE_SPACE, - bottom: LARGE_SPACE, - ), - child: Text( - appLocalizations.productDataUtility, - style: Theme.of(context).textTheme.headline2!.apply( - color: Colors.black, - ), - ), - ), - Container( - height: _isProductExpanded ? null : 150, - padding: const EdgeInsets.only( - bottom: LARGE_SPACE, - right: LARGE_SPACE, - left: LARGE_SPACE, - ), - child: GestureDetector( - onTap: () => _expandProductCard(), - child: SummaryCard( - _product, - productPreferences, - isFullVersion: _isProductExpanded, - ), + return _Helper(_product); + }, + ); +} + +// In order to avoid to reload the product when refreshing the preferences. +class _Helper extends StatefulWidget { + const _Helper(this.product); + + final Product product; + + @override + State<_Helper> createState() => _HelperState(); +} + +class _HelperState extends State<_Helper> { + bool _isProductExpanded = false; + + @override + Widget build(BuildContext context) { + final ProductPreferences productPreferences = + context.watch(); + final UserPreferences userPreferences = context.watch(); + final AppLocalizations appLocalizations = AppLocalizations.of(context)!; + final List pageData = [ + Padding( + padding: const EdgeInsets.only( + right: LARGE_SPACE, + left: LARGE_SPACE, + bottom: LARGE_SPACE, + ), + child: Text( + appLocalizations.productDataUtility, + style: Theme.of(context).textTheme.headline2!.apply( + color: Colors.black, ), + ), + ), + Container( + height: _isProductExpanded ? null : 150, + padding: const EdgeInsets.only( + bottom: LARGE_SPACE, + right: LARGE_SPACE, + left: LARGE_SPACE, + ), + child: GestureDetector( + onTap: () => _expandProductCard(), + child: SummaryCard( + widget.product, + productPreferences, + isFullVersion: _isProductExpanded, + ), + ), + ), + ]; + pageData.addAll( + UserPreferencesFood( + productPreferences: productPreferences, + setState: setState, + context: context, + userPreferences: userPreferences, + appLocalizations: appLocalizations, + themeData: Theme.of(context), + ).getContent(), + ); + return Scaffold( + body: Stack( + children: [ + ListView( + // bottom padding is very large because [NextButton] is stacked on top of the page. + padding: const EdgeInsets.only( + top: LARGE_SPACE, + bottom: VERY_LARGE_SPACE * 5, ), - ]; - pageData.addAll(UserPreferencesFood( - productPreferences: productPreferences, - setState: setState, - context: context, - userPreferences: userPreferences, - appLocalizations: appLocalizations, - themeData: Theme.of(context), - ).getContent()); - return Scaffold( - body: Stack( - children: [ - ListView( - // bottom padding is very large because [NextButton] is stacked on top of the page. - padding: const EdgeInsets.only( - top: LARGE_SPACE, - bottom: VERY_LARGE_SPACE * 5, - ), - shrinkWrap: true, - children: pageData, - ), - const Positioned( - child: Align( - alignment: Alignment.bottomCenter, - child: NextButton(OnboardingPage.PREFERENCES_PAGE), - ), - ), - ], + shrinkWrap: true, + children: pageData, + ), + const Positioned( + child: Align( + alignment: Alignment.bottomCenter, + child: NextButton(OnboardingPage.PREFERENCES_PAGE), ), - ); - }); + ), + ], + ), + ); } void _expandProductCard() { if (!_isProductExpanded) { - setState(() { - _isProductExpanded = true; - }); + setState(() => _isProductExpanded = true); } } }