Skip to content

Commit

Permalink
feat: #849 - added country selector to preferences page (#861)
Browse files Browse the repository at this point in the history
Impacted files:
* `country_selector.dart`: fixed an async bug; added facultative default country, decoration and padding
* `user_preferences_profile.dart`: added a "select country" item
* `welcome_page.dart`: added decoration and padding as parameters to `CountrySelector`
  • Loading branch information
monsieurtanuki authored Jan 5, 2022
1 parent fa54d0e commit b90bbaf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
50 changes: 22 additions & 28 deletions packages/smooth_app/lib/pages/onboarding/country_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import 'package:iso_countries/iso_countries.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/product_query.dart';
import 'package:smooth_ui_library/util/ui_helpers.dart';

/// A selector for selecting user's country.
class CountrySelector extends StatefulWidget {
const CountrySelector();
const CountrySelector({
required this.initialCountryCode,
this.inputDecoration,
this.padding,
});

final String? initialCountryCode;
final InputDecoration? inputDecoration;
final EdgeInsetsGeometry? padding;

@override
State<CountrySelector> createState() => _CountrySelectorState();
Expand Down Expand Up @@ -41,8 +48,7 @@ class _CountrySelectorState extends State<CountrySelector> {
}

@override
Widget build(BuildContext context) {
return FutureBuilder<void>(
Widget build(BuildContext context) => FutureBuilder<void>(
future: _initFuture,
builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
if (snapshot.hasError) {
Expand All @@ -51,40 +57,29 @@ class _CountrySelectorState extends State<CountrySelector> {
if (snapshot.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
}
return Padding(
padding:
const EdgeInsets.only(top: MEDIUM_SPACE, bottom: LARGE_SPACE),
return Container(
padding: widget.padding,
child: DropdownButtonFormField<Country>(
value: _chosenValue,
style: const TextStyle(color: Colors.black),
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Color.fromARGB(255, 235, 235, 235)),
borderRadius: BorderRadius.circular(VERY_LARGE_SPACE),
),
filled: true,
fillColor: const Color.fromARGB(255, 235, 235, 235),
),
decoration: widget.inputDecoration,
items: _countryList
.map<DropdownMenuItem<Country>>((Country country) {
return DropdownMenuItem<Country>(
value: country,
child: Text(country.name),
);
}).toList(),
onChanged: (Country? value) {
setState(() async {
if (value != null) {
_chosenValue = value;
await _setUserCountry(_chosenValue.countryCode);
}
});
onChanged: (Country? value) async {
if (value != null) {
_chosenValue = value;
await _setUserCountry(_chosenValue.countryCode);
setState(() {});
}
},
),
);
});
}
},
);

/// Sanitize the country list by removing countries that are not in [OpenFoodFactsCountry]
/// and providing a fallback English name for countries that are in [OpenFoodFactsCountry]
Expand Down Expand Up @@ -138,8 +133,7 @@ class _CountrySelectorState extends State<CountrySelector> {
List<Country> _reorderCountries(List<Country> countries) {
countries
.sort((final Country a, final Country b) => a.name.compareTo(b.name));
final String? mostLikelyUserCountryCode =
WidgetsBinding.instance?.window.locale.countryCode?.toLowerCase();
final String? mostLikelyUserCountryCode = widget.initialCountryCode;
if (mostLikelyUserCountryCode == null) {
return countries;
}
Expand Down
20 changes: 19 additions & 1 deletion packages/smooth_app/lib/pages/onboarding/welcome_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ class WelcomePage extends StatelessWidget {
style: bodyTextStyle,
),
),
const CountrySelector(),
CountrySelector(
initialCountryCode: WidgetsBinding
.instance?.window.locale.countryCode
?.toLowerCase(),
padding: const EdgeInsets.only(
top: MEDIUM_SPACE,
bottom: LARGE_SPACE,
),
inputDecoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Color.fromARGB(255, 235, 235, 235)),
borderRadius: BorderRadius.circular(VERY_LARGE_SPACE),
),
filled: Theme.of(context).colorScheme.brightness ==
Brightness.light,
fillColor: const Color.fromARGB(255, 235, 235, 235),
),
),
Padding(
padding: const EdgeInsets.only(left: SMALL_SPACE),
child: Text(
Expand Down
7 changes: 7 additions & 0 deletions packages/smooth_app/lib/pages/user_preferences_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/helpers/user_management_helper.dart';
import 'package:smooth_app/pages/abstract_user_preferences.dart';
import 'package:smooth_app/pages/onboarding/country_selector.dart';
import 'package:smooth_app/pages/user_management/login_page.dart';

/// Collapsed/expanded display of profile for the preferences page.
Expand Down Expand Up @@ -70,6 +71,12 @@ class UserPreferencesProfile extends AbstractUserPreferences {
),
),
),
ListTile(
leading: const Icon(Icons.public),
title: CountrySelector(
initialCountryCode: userPreferences.userCountryCode,
),
),
ListTile(
leading: const Icon(Icons.rotate_left),
title: Text(appLocalizations.reset),
Expand Down

0 comments on commit b90bbaf

Please sign in to comment.