A demonstration of using Riverpod for dynamic locale switching in-app

Overview

Riverpod Localization

A demonstration of using Riverpod for dynamic locale switching in-app, with persistence.

drawing

How It Works

  • Fallback Locale: Declared inside locale_state.dart, and set with the @Default decorator.
  • Default Locale: At startup, gets the system's Platform.localeName (via dart:io) and uses it to set the app's locale.
    • If the platform locale does not match a supported locale
      • The first matching language code locale will be set.
      • Otherwise, the fallback locale will be used.
  • Supported Locales: These are stored in a provider, accessible from anywhere. Update this list to add more locales.
  • Current Locale: Accessible from a provider anywhere the ref object is available. Watch this provider to rebuild whenever the locale changes. You can still use Localizations.localeOf(context) to get the Locale.

Locale Changes

  • initState sets the initial locale from the platform.

    • App starts up with fallback locale
    • initState immediately attempts to restore a locale from persistent storage to Riverpod state.
    • If there is no locale in storage, attempts to use the Platform's locale.
  • The drop down button can be used to select a new locale.

    • This updates state and saves the locale to persistent storage.
  • The MaterialApp() is wrapped in a Riverpod ConsumerWidget which will rebuild automatically anytime the watched ref.watch() locale value changes.

  • All translated strings are rebuilt as part of this process.

Platform Locale

The Platform.locale property of dart:io library only works on mobile. For web, we need to use dart:html's window.navigator.language. This is implemented with the PlatformLocale() interface and conditional imports.

Dependencies

Getting Started

Code Generator: This app uses build_runner to generate code for freezed.

# flutter pub run build_runner build --delete-conflicting-outputs
You might also like...

On making this project I learned using Getx to change between dark mode to light mode, learned about the time zone and schedule notification , That I have Integrated in this small app, This is the small section of making a todo app.

On making this project I learned using Getx to change between dark mode to light mode, learned about the time zone and schedule notification , That I have Integrated in this small app, This is the small section of making a todo app.

On making this project I learned using Getx to change between dark mode to light mode, learned about the time zone and schedule notification , That I have Integrated in this small app, This is the small section of making a todo app.

Aug 22, 2022

News app - News App using Flutter and Firebase Auth

News app - News App using Flutter and Firebase Auth

News App in Flutter using Firebase Auth, Firebase Realtime Database & Times of I

Nov 6, 2022

Cryptocurrency App with MVP Design Pattern to track all the coins data in realtime for android & iOS . Written in dart using Flutter SDK.

Cryptocurrency App with MVP Design Pattern to track all the coins data in realtime for android & iOS . Written in dart using Flutter SDK.

Flutter CryptoCurrency App (MVP) Cryptocurrency App with MVP design pattern to track all the coins data in realtime for android & iOS . Written in dar

Dec 30, 2022

A tourism mobile app using flutter

A tourism mobile app using flutter

Palghar Tourism project_description Explore the docs » View Demo · Report Bug · Request Feature Table of Contents About The Project Built With Getting

Dec 9, 2022

Wallpaper app using flutter and pexels api

wallpaper A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if thi

Oct 9, 2021

Cloned Skype App using Flutter & Firebase

Cloned Skype App using Flutter & Firebase

Cloned Skype App using Flutter & Firebase

Aug 3, 2022

Meetup app made using Flutter

Kaigi - HackMIT Introduction Cross platform application that can help people make friends online and host events. Tested on Android only. Technologies

Jan 3, 2022

A simple fully working weather app with state management build using flutter.

A simple fully working weather app with state management build using flutter.

A simple fully working weather app with state management build using flutter.

Apr 12, 2022

A fully responsive BMI calculator app made using flutter and dart with beautiful minimalistic user interface design and easy to use .

A fully responsive BMI calculator app made using flutter and dart with beautiful minimalistic user interface design and easy to use .

BMI_Calculator_Flutter A fully responsive BMI calculator app made using flutter and dart with beautiful minimalistic user interface design and easy to

Oct 9, 2021
Comments
  • Localizations in providers

    Localizations in providers

    Hi! Thank you for you interesting repo.

    Did you thought about a solution to have localized text in a provider itself. I'm using provider as viemodels, and it would be super handy if i could use a text from localization directly in the providers. But I have no build context in there.

    Looking forward if you have an idea.

    opened by matecode 0
Owner
Matthew Rideout
Currently specializing in Firebase, Node.js, React, and Flutter.
Matthew Rideout
A demonstration of basic Flutter transition animations with Hydro-SDK

A demonstration of basic Flutter transition animations with Hydro-SDK

Hydro-SDK 2 Feb 21, 2022
A Completed Functional Flutter App (BLoC + Json API + Unit Test + Azure Active Directory OAuth + Access Dynamic CRM Entitis)

A Completed Functional Flutter App (BLoC + Json API + Unit Test + Azure Active Directory OAuth + Access Dynamic CRM Entitis)A Completed Functional Flutter App (BLoC + Json API + Unit Test + Azure Active Directory OAuth + Access Dynamic CRM Entitis)

Ali Tazik 9 Dec 23, 2022
Flutter State Management: Movie App with Provider, Riverpod, flutter_bloc & more

Flutter State Management: Movie App with Provider, Riverpod, flutter_bloc & more This reference project shows how to implement a (Netflix-inspired) mo

Andrea Bizzotto 427 Dec 29, 2022
Use dynamic and beautiful card view pagers (horizontal direction) to help you create great apps.

Use dynamic and beautiful card view pagers (horizontal direction) to help you create great apps. Preview Mobile Vertical Card Pager Web Web Link Insta

Jeongtae Kim 27 Dec 9, 2022
Flutter App using the stack overflow questions api which we made using django.

Flutter StackOverflow App Flutter StackOverflow App using the stack overflow questions api which we made using django. Show some ❤️ and star the repo

Pawan Kumar 206 Dec 30, 2022
The objective is to create a simple app using Dart/Flutter, for make a REST API integration with Flutter, using free JSON API

ListView Builder App The objective is to create a simple app using Dart/Flutter, for make a REST API integration with Flutter, using free JSON API: ht

Rafael Tozzo 1 Dec 7, 2022
Whats-App-Clone-UI-using-Flutter - WhatsApp Clone UI using Flutter

WhatsApp ?? Clone UI ?? using Flutter ?? Screenshots Links My Socials Youtube ch

AmirHossein Bayat 6 Dec 7, 2022
A simple recipe showing application made using Flutter , using card Views MaterialPageRoute etc.

recipes A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if this

Akash Srivastava 1 Oct 31, 2021
App-flutter-travel-app - Travel App Built Using Flutter

App-flutter-travel-app - Travel App Built Using Flutter

Sangvaleap Vanny 69 Dec 4, 2022
A Flutter app that demonstrates how to build a simple app using the SpaceX API created by the Very Good Ventures Team.

SpaceX Demo Developed with ?? by Very Good Ventures ?? A Flutter app that demonstrates how to build a simple app using the SpaceX API created by the V

Very Good Ventures 120 Dec 27, 2022