Skip to content

Commit

Permalink
fix: openfoodfacts#899 - avoid to reload the product in onboarding
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
  • Loading branch information
monsieurtanuki committed Jan 8, 2022
1 parent edd46be commit 9a2096d
Showing 1 changed file with 86 additions and 68 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);
}
}
}

0 comments on commit 9a2096d

Please sign in to comment.