Skip to content

Commit

Permalink
fix: #899 - avoid to reload the product in onboarding (#902)
Browse files Browse the repository at this point in the history
Impacted file:
* `preferences_page.dart`: split in two the process - load product and then display it with preferences
* `smooth_app/pubspec.yaml`: upgrade to openfoodfacts 1.9.1
* `smooth_app/pubspec.lock`: impacted by pubspec.yaml
* `smooth_ui_library/pubspec.yaml`: upgrade to openfoodfacts 1.9.1
* `smooth_ui_library/pubspec.lock`: impacted by pubspec.yaml
* `smooth_ui_library/example/pubspec.lock`: impacted by pubspec.yaml
  • Loading branch information
monsieurtanuki authored Jan 9, 2022
1 parent 79c9695 commit cb320e6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 73 deletions.
154 changes: 86 additions & 68 deletions packages/smooth_app/lib/pages/onboarding/preferences_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class PreferencesPage extends StatefulWidget {
State<PreferencesPage> createState() => _PreferencesPageState();
}

// Just here to load the product and pass it to the next Widget
class _PreferencesPageState extends State<PreferencesPage> {
late Future<void> _initFuture;
late Product _product;
bool _isProductExpanded = false;

@override
void didChangeDependencies() {
Expand All @@ -39,12 +39,7 @@ class _PreferencesPageState extends State<PreferencesPage> {
}

@override
Widget build(BuildContext context) {
final ProductPreferences productPreferences =
context.watch<ProductPreferences>();
final UserPreferences userPreferences = context.watch<UserPreferences>();
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;
return FutureBuilder<void>(
Widget build(BuildContext context) => FutureBuilder<void>(
future: _initFuture,
builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
if (snapshot.hasError) {
Expand All @@ -53,74 +48,97 @@ class _PreferencesPageState extends State<PreferencesPage> {
if (snapshot.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
}
final List<Widget> pageData = <Widget>[
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<ProductPreferences>();
final UserPreferences userPreferences = context.watch<UserPreferences>();
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;
final List<Widget> pageData = <Widget>[
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: <Widget>[
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: <Widget>[
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);
}
}
}
2 changes: 1 addition & 1 deletion packages/smooth_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ packages:
name: openfoodfacts
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.9.1"
package_config:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
latlong2: ^0.8.1
matomo: ^1.1.0
modal_bottom_sheet: ^2.0.0
openfoodfacts: ^1.8.1
openfoodfacts: ^1.9.1
# Uncomment those lines if you want to use a local version of the openfoodfacts package
# openfoodfacts:
# path: ../../../openfoodfacts-dart
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_ui_library/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ packages:
name: openfoodfacts
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.9.1"
path:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_ui_library/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ packages:
name: openfoodfacts
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.9.1"
path:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_ui_library/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
flutter:
sdk: flutter
flutter_svg: ^1.0.0
openfoodfacts: ^1.8.1
openfoodfacts: ^1.9.1
# Uncomment those lines if you want to use a local version of the openfoodfacts package
# openfoodfacts:
# path: ../../../openfoodfacts-dart
Expand Down

0 comments on commit cb320e6

Please sign in to comment.