Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #3046 - refactored NutritionPage around Nutrient #3194

Merged
merged 4 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ packages:
name: openfoodfacts
url: "https://pub.dartlang.org"
source: hosted
version: "1.26.0"
version: "1.26.2"
openfoodfacts_flutter_lints:
dependency: "direct dev"
description:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/OrderedNutrient.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_text_form_field.dart';
import 'package:smooth_app/pages/product/nutrition_container.dart';
import 'package:smooth_app/pages/text_field_helper.dart';

/// Button that opens an "add nutrient" dialog.
///
/// The [nutritionContainer] will tell which nutrients can be added, and that's
/// where the "new" nutrient will eventually be added.
/// The [refreshParent] will refresh the parent widget when a nutrient is added.
class NutritionAddNutrientButton extends StatelessWidget {
const NutritionAddNutrientButton({
required this.nutritionContainer,
required this.refreshParent,
});

final NutritionContainer nutritionContainer;
final VoidCallback refreshParent;

@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
return ElevatedButton.icon(
onPressed: () async {
final List<OrderedNutrient> leftovers = List<OrderedNutrient>.from(
nutritionContainer.getLeftoverNutrients(),
);
leftovers.sort((final OrderedNutrient a, final OrderedNutrient b) =>
a.name!.compareTo(b.name!));
List<OrderedNutrient> filteredList =
List<OrderedNutrient>.from(leftovers);
final TextEditingControllerWithInitialValue nutritionTextController =
TextEditingControllerWithInitialValue();
final OrderedNutrient? selected = await showDialog<OrderedNutrient>(
context: context,
builder: (BuildContext context) => StatefulBuilder(
builder: (
BuildContext context,
void Function(VoidCallback fn) setState,
) =>
SmoothAlertDialog(
body: SizedBox(
height: MediaQuery.of(context).size.height / 2,
width: MediaQuery.of(context).size.width,
child: Column(
children: <Widget>[
SmoothTextFormField(
prefixIcon: const Icon(Icons.search),
hintText: appLocalizations.search,
type: TextFieldTypes.PLAIN_TEXT,
controller: nutritionTextController,
onChanged: (String? query) => setState(
() => filteredList = leftovers
.where((OrderedNutrient item) => item.name!
.toLowerCase()
.contains(query!.toLowerCase()))
.toList(),
),
),
Expanded(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
final OrderedNutrient nutrient = filteredList[index];
return ListTile(
title: Text(nutrient.name!),
onTap: () => Navigator.of(context).pop(nutrient),
);
},
itemCount: filteredList.length,
shrinkWrap: true,
),
),
],
),
),
positiveAction: SmoothActionButton(
onPressed: () => Navigator.pop(context),
text: appLocalizations.cancel,
),
),
),
);
if (selected != null) {
nutritionContainer.add(selected);
refreshParent.call();
}
},
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
const RoundedRectangleBorder(
borderRadius: ROUNDED_BORDER_RADIUS,
side: BorderSide.none,
),
),
),
icon: const Icon(Icons.add),
label: Text(appLocalizations.nutrition_page_add_nutrient),
);
}
}
Loading