Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void main() {
providers: [
ChangeNotifierProvider(create: (context) => ImageLoader()),
ChangeNotifierProvider(create: (_) => ImageLibraryProvider()),
ChangeNotifierProvider(create: (_) => LocaleProvider()),
// Load saved locale on startup
ChangeNotifierProvider(create: (_) => LocaleProvider()..loadSavedLocale()),
],
Comment on lines -15 to 24
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadSavedLocale() is async but is kicked off without being awaited; this will boot the app in the default locale and then rebuild once the preference loads (visible language “flash” on cold start). If you want the correct locale from the first frame, make main() async, call WidgetsFlutterBinding.ensureInitialized(), and await locale loading before runApp (or gate MaterialApp behind a loading state).

Copilot uses AI. Check for mistakes.
child: const MyApp(),
),
Expand Down
34 changes: 32 additions & 2 deletions lib/provider/locale_provider.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
Comment on lines 1 to +2
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shared_preferences is imported here, but the package isn’t declared in pubspec.yaml dependencies (so this change will fail to compile). Add shared_preferences to pubspec.yaml and run flutter pub get / update lockfile as needed.

Copilot uses AI. Check for mistakes.

/// A provider class for managing the application's locale.
///
/// This class extends [ChangeNotifier] to allow widgets to listen for
/// changes in the locale and rebuild accordingly.
class LocaleProvider with ChangeNotifier {
static const String _localeKey = 'app_locale';

Locale _locale = const Locale('en');

Locale get locale => _locale;

/// Sets the application's locale.
/// Loads the saved locale from local storage (SharedPreferences).
/// Call this once when the provider is created.
Future<void> loadSavedLocale() async {
final prefs = await SharedPreferences.getInstance();
final code = prefs.getString(_localeKey);

if (code == null || code.isEmpty) return;

_locale = Locale(code);
notifyListeners();
}
Comment on lines +17 to +25
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SharedPreferences.getInstance() / read/write calls can throw (platform/channel issues). Since these methods are called without awaiting at the call site (e.g., startup cascade), an exception could surface as an unhandled async error. Consider wrapping SharedPreferences operations in try/catch and failing gracefully (keep the in-memory locale, optionally log).

Copilot uses AI. Check for mistakes.

/// Sets the application's locale and persists it.
///
/// When the locale is changed, it notifies all listening widgets to rebuild.
void setLocale(Locale newLocale) {
Future<void> setLocale(Locale newLocale) async {
_locale = newLocale;

final prefs = await SharedPreferences.getInstance();
await prefs.setString(_localeKey, newLocale.languageCode);

Comment on lines +23 to +35
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locale persistence currently stores only newLocale.languageCode and restores via Locale(code). This loses countryCode/scriptCode and can’t round-trip supported locales like pt_BR, nb_NO, or zh-Hans/Hant (see AppLocalizations.supportedLocales). Persist the full locale (e.g., toLanguageTag() or language+script+country) and parse it back with Locale.fromSubtags(...).

Copilot uses AI. Check for mistakes.
notifyListeners();
}

/// Optional: Reset to system/default behavior.
/// Use this if you provide a "System Default" option in UI.
Future<void> clearLocale() async {
_locale = const Locale('en'); // or set to null if your app supports null locale
Comment on lines +39 to +42
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment says “Reset to system/default behavior”, but the implementation forces English (Locale('en')). Either update the comment to match the behavior, or change the provider/API to support a true system-default mode (e.g., nullable locale passed to MaterialApp.locale).

Suggested change
/// Optional: Reset to system/default behavior.
/// Use this if you provide a "System Default" option in UI.
Future<void> clearLocale() async {
_locale = const Locale('en'); // or set to null if your app supports null locale
/// Reset to the app's default locale (English) and clear any saved override.
/// Use this if you provide a "Use app default" option in the UI.
Future<void> clearLocale() async {
_locale = const Locale('en');

Copilot uses AI. Check for mistakes.

final prefs = await SharedPreferences.getInstance();
await prefs.remove(_localeKey);

notifyListeners();
}
}