mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-30 01:41:05 +01:00
fix: prevent FutureBuilder from reloading on every rebuild in NutritionalPlanScreen
Calling _loadFullPlan() directly inside build() caused a new Future to be created on every rebuild (e.g. when opening the PopupMenuButton), which reset the FutureBuilder to ConnectionState.waiting and showed the loading spinner unnecessarily. Fix: convert to StatefulWidget and initialize the Future once in didChangeDependencies(), per Flutter's official FutureBuilder docs. Ref: https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html Took 45 minutes
This commit is contained in:
@@ -32,20 +32,33 @@ enum NutritionalPlanOptions {
|
||||
delete,
|
||||
}
|
||||
|
||||
class NutritionalPlanScreen extends StatelessWidget {
|
||||
class NutritionalPlanScreen extends StatefulWidget {
|
||||
const NutritionalPlanScreen();
|
||||
|
||||
static const routeName = '/nutritional-plan-detail';
|
||||
|
||||
Future<NutritionalPlan> _loadFullPlan(BuildContext context, int planId) {
|
||||
return Provider.of<NutritionPlansProvider>(context, listen: false).fetchAndSetPlanFull(planId);
|
||||
@override
|
||||
State<NutritionalPlanScreen> createState() => _NutritionalPlanScreenState();
|
||||
}
|
||||
|
||||
class _NutritionalPlanScreenState extends State<NutritionalPlanScreen> {
|
||||
|
||||
Future<NutritionalPlan>? _planFuture;
|
||||
late NutritionalPlan _nutritionalPlan;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
if (_planFuture == null) {
|
||||
_nutritionalPlan = ModalRoute.of(context)!.settings.arguments as NutritionalPlan;
|
||||
_planFuture = Provider.of<NutritionPlansProvider>(context, listen: false)
|
||||
.fetchAndSetPlanFull(_nutritionalPlan.id!);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const appBarForeground = Colors.white;
|
||||
final nutritionalPlan = ModalRoute.of(context)!.settings.arguments as NutritionalPlan;
|
||||
|
||||
return Scaffold(
|
||||
//appBar: getAppBar(nutritionalPlan),
|
||||
floatingActionButton: Row(
|
||||
@@ -60,7 +73,7 @@ class NutritionalPlanScreen extends StatelessWidget {
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).logIngredient,
|
||||
getIngredientLogForm(nutritionalPlan),
|
||||
getIngredientLogForm(_nutritionalPlan),
|
||||
hasListView: true,
|
||||
),
|
||||
);
|
||||
@@ -77,7 +90,7 @@ class NutritionalPlanScreen extends StatelessWidget {
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
LogMealsScreen.routeName,
|
||||
arguments: nutritionalPlan,
|
||||
arguments: _nutritionalPlan,
|
||||
);
|
||||
},
|
||||
child: const SvgIcon(
|
||||
@@ -94,7 +107,7 @@ class NutritionalPlanScreen extends StatelessWidget {
|
||||
pinned: true,
|
||||
iconTheme: const IconThemeData(color: appBarForeground),
|
||||
actions: [
|
||||
if (!nutritionalPlan.onlyLogging)
|
||||
if (!_nutritionalPlan.onlyLogging)
|
||||
IconButton(
|
||||
icon: const SvgIcon(
|
||||
icon: SvgIconData('assets/icons/meal-add.svg'),
|
||||
@@ -105,7 +118,7 @@ class NutritionalPlanScreen extends StatelessWidget {
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).addMeal,
|
||||
MealForm(nutritionalPlan.id!),
|
||||
MealForm(_nutritionalPlan.id!),
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -120,7 +133,7 @@ class NutritionalPlanScreen extends StatelessWidget {
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).edit,
|
||||
PlanForm(nutritionalPlan),
|
||||
PlanForm(_nutritionalPlan),
|
||||
hasListView: true,
|
||||
),
|
||||
);
|
||||
@@ -129,7 +142,7 @@ class NutritionalPlanScreen extends StatelessWidget {
|
||||
Provider.of<NutritionPlansProvider>(
|
||||
context,
|
||||
listen: false,
|
||||
).deletePlan(nutritionalPlan.id!);
|
||||
).deletePlan(_nutritionalPlan.id!);
|
||||
Navigator.of(context).pop();
|
||||
break;
|
||||
}
|
||||
@@ -158,13 +171,13 @@ class NutritionalPlanScreen extends StatelessWidget {
|
||||
flexibleSpace: FlexibleSpaceBar(
|
||||
titlePadding: const EdgeInsets.fromLTRB(56, 0, 56, 16),
|
||||
title: Text(
|
||||
nutritionalPlan.getLabel(context),
|
||||
_nutritionalPlan.getLabel(context),
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(color: appBarForeground),
|
||||
),
|
||||
),
|
||||
),
|
||||
FutureBuilder(
|
||||
future: _loadFullPlan(context, nutritionalPlan.id!),
|
||||
future: _planFuture,
|
||||
builder: (context, AsyncSnapshot<NutritionalPlan> snapshot) =>
|
||||
snapshot.connectionState == ConnectionState.waiting
|
||||
? SliverList(
|
||||
@@ -181,7 +194,7 @@ class NutritionalPlanScreen extends StatelessWidget {
|
||||
)
|
||||
: Consumer<NutritionPlansProvider>(
|
||||
builder: (context, value, child) =>
|
||||
NutritionalPlanDetailWidget(nutritionalPlan),
|
||||
NutritionalPlanDetailWidget(_nutritionalPlan),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user