diff --git a/packages/smooth_app/lib/pages/onboarding/country_selector.dart b/packages/smooth_app/lib/pages/onboarding/country_selector.dart index 078bcb77063c..09310a262c52 100644 --- a/packages/smooth_app/lib/pages/onboarding/country_selector.dart +++ b/packages/smooth_app/lib/pages/onboarding/country_selector.dart @@ -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 createState() => _CountrySelectorState(); @@ -41,8 +48,7 @@ class _CountrySelectorState extends State { } @override - Widget build(BuildContext context) { - return FutureBuilder( + Widget build(BuildContext context) => FutureBuilder( future: _initFuture, builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasError) { @@ -51,21 +57,11 @@ class _CountrySelectorState extends State { 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( 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>((Country country) { return DropdownMenuItem( @@ -73,18 +69,17 @@ class _CountrySelectorState extends State { 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] @@ -138,8 +133,7 @@ class _CountrySelectorState extends State { List _reorderCountries(List 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; } diff --git a/packages/smooth_app/lib/pages/onboarding/welcome_page.dart b/packages/smooth_app/lib/pages/onboarding/welcome_page.dart index f29c8f79b9de..40ccaa07c354 100644 --- a/packages/smooth_app/lib/pages/onboarding/welcome_page.dart +++ b/packages/smooth_app/lib/pages/onboarding/welcome_page.dart @@ -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( diff --git a/packages/smooth_app/lib/pages/user_preferences_profile.dart b/packages/smooth_app/lib/pages/user_preferences_profile.dart index b6c0781d1fc5..55a3ff30782a 100644 --- a/packages/smooth_app/lib/pages/user_preferences_profile.dart +++ b/packages/smooth_app/lib/pages/user_preferences_profile.dart @@ -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. @@ -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),