Skip to content

Commit

Permalink
feat: Ensure Country / Language / Nutrient picker look the same (#4389)
Browse files Browse the repository at this point in the history
* Ensure Country / Language / Nutrient picker look the same

* Add a const constructor

* Some small improvements
  • Loading branch information
g123k authored Jul 28, 2023
1 parent f9b7b9d commit fe31fd9
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:smooth_app/generic_lib/buttons/smooth_simple_button.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_responsive.dart';
import 'package:smooth_app/helpers/app_helper.dart';
import 'package:smooth_app/helpers/keyboard_helper.dart';

/// Custom Dialog to use in the app
///
Expand Down Expand Up @@ -538,3 +539,77 @@ class SmoothSimpleErrorAlertDialog extends StatelessWidget {
);
}
}

class SmoothListAlertDialog extends StatelessWidget {
SmoothListAlertDialog({
required this.title,
required this.list,
this.header,
ScrollController? scrollController,
this.positiveAction,
this.negativeAction,
this.actionsAxis,
this.actionsOrder,
}) : _scrollController = scrollController ?? ScrollController();

final String title;
final Widget? header;
final Widget list;
final SmoothActionButton? positiveAction;
final SmoothActionButton? negativeAction;
final Axis? actionsAxis;
final SmoothButtonsBarOrder? actionsOrder;
final ScrollController _scrollController;

@override
Widget build(BuildContext context) {
return SmoothAlertDialog(
contentPadding: const EdgeInsetsDirectional.symmetric(
horizontal: 0.0,
vertical: SMALL_SPACE,
),
body: SizedBox(
height: MediaQuery.of(context).size.height /
(context.keyboardVisible ? 1.0 : 1.5),
width: MediaQuery.of(context).size.width,
child: Column(
children: <Widget>[
Container(
alignment: AlignmentDirectional.centerStart,
padding: const EdgeInsetsDirectional.only(
start: 23.0,
end: 24.0,
top: SMALL_SPACE,
),
child: Text(
title,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
),
if (header != null)
Padding(
padding: const EdgeInsets.symmetric(
horizontal: SMALL_SPACE,
vertical: MEDIUM_SPACE,
),
child: header,
),
Expanded(
child: Scrollbar(
controller: _scrollController,
child: list,
),
)
],
),
),
positiveAction: positiveAction,
negativeAction: negativeAction,
actionsAxis: actionsAxis,
actionsOrder: actionsOrder,
);
}
}
119 changes: 59 additions & 60 deletions packages/smooth_app/lib/generic_lib/widgets/language_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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/preferences/user_preferences_languages_list.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/widgets/smooth_text.dart';

class LanguageSelector extends StatelessWidget {
const LanguageSelector({
Expand Down Expand Up @@ -100,6 +101,7 @@ class LanguageSelector extends StatelessWidget {
final BuildContext context, {
final Iterable<OpenFoodFactsLanguage>? selectedLanguages,
}) async {
final ScrollController scrollController = ScrollController();
final AppLocalizations appLocalizations = AppLocalizations.of(context);
final TextEditingController languageSelectorController =
TextEditingController();
Expand Down Expand Up @@ -135,70 +137,67 @@ class LanguageSelector extends StatelessWidget {
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(
type: TextFieldTypes.PLAIN_TEXT,
hintText: appLocalizations.search,
prefixIcon: const Icon(Icons.search),
controller: languageSelectorController,
onChanged: (String? query) {
setState(
() {
filteredList = leftovers
.where((OpenFoodFactsLanguage item) =>
_languages
.getNameInEnglish(item)
.toLowerCase()
.contains(query!.toLowerCase()) ||
_languages
.getNameInLanguage(item)
.toLowerCase()
.contains(query.toLowerCase()) ||
item.code.contains(query))
.toList();
},
);
},
),
Expanded(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
final OpenFoodFactsLanguage language =
filteredList[index];
final String nameInLanguage =
_languages.getNameInLanguage(language);
final String nameInEnglish =
_languages.getNameInEnglish(language);
final bool selected = selectedLanguages != null &&
selectedLanguages.contains(language);
return ListTile(
dense: true,
trailing: selected ? const Icon(Icons.check) : null,
title: Text(
'$nameInLanguage ($nameInEnglish)',
softWrap: false,
overflow: TextOverflow.fade,
style: selected
? const TextStyle(fontWeight: FontWeight.bold)
: null,
),
onTap: () => Navigator.of(context).pop(language),
);
},
itemCount: filteredList.length,
shrinkWrap: true,
),
SmoothListAlertDialog(
title: appLocalizations.language_selector_title,
header: SmoothTextFormField(
type: TextFieldTypes.PLAIN_TEXT,
hintText: appLocalizations.search,
prefixIcon: const Icon(Icons.search),
controller: languageSelectorController,
onChanged: (String? query) {
query = query?.trim().toLowerCase();

setState(
() {
filteredList = leftovers
.where((OpenFoodFactsLanguage item) =>
_languages
.getNameInEnglish(item)
.toLowerCase()
.contains(query!.toLowerCase()) ||
_languages
.getNameInLanguage(item)
.toLowerCase()
.contains(query.toLowerCase()) ||
item.code.contains(query))
.toList();
},
);
},
),
scrollController: scrollController,
list: ListView.separated(
controller: scrollController,
itemBuilder: (BuildContext context, int index) {
final OpenFoodFactsLanguage language = filteredList[index];
final String nameInLanguage =
_languages.getNameInLanguage(language);
final String nameInEnglish =
_languages.getNameInEnglish(language);
final bool selected = selectedLanguages != null &&
selectedLanguages.contains(language);
return ListTile(
dense: true,
trailing: selected ? const Icon(Icons.check) : null,
title: TextHighlighter(
text: '$nameInLanguage ($nameInEnglish)',
filter: languageSelectorController.text,
selected: selected,
),
],
onTap: () => Navigator.of(context).pop(language),
);
},
separatorBuilder: (_, __) => const Divider(
height: 1.0,
),
itemCount: filteredList.length,
shrinkWrap: true,
),
positiveAction: SmoothActionButton(
onPressed: () => Navigator.of(context).pop(),
onPressed: () {
languageSelectorController.clear();
Navigator.of(context).pop();
},
text: appLocalizations.cancel,
),
),
Expand Down
4 changes: 4 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,10 @@
"@country_selector_title": {
"description": "Label written as the title of the dialog to select the user country"
},
"language_selector_title": "Select your language:",
"@language_selector_title": {
"description": "Label written as the title of the dialog to select the user language"
},
"action_delete_list": "Delete",
"@action_delete_list": {
"description": "Delete a list action in a menu"
Expand Down
Loading

0 comments on commit fe31fd9

Please sign in to comment.