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

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.

Author

Bagirishya Rwema Dominique

You might also like...

A boilerplate project for Flutter using RiverPod, Dio, auto_route

Flutter Boilerplate A boilerplate project for Flutter using RiverPod, Dio, auto_route, Freezed and generated with very_good_cli. This is a very simple

Dec 9, 2022

Flutter theme demo using riverpod and shared preferences

flutter_theme_demo A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you start

Dec 27, 2021

Multiplayer tictactoe game made in Flutter with nodejs and socket.io and using riverpod for state management

Multiplayer tictactoe game made in Flutter with nodejs and socket.io and using riverpod for state management

TicTacToe Game A Multiplayer TicTacToe Game- Works on Android, iOS and Web. Features Create/Join Room Play Realtime Display Points Round & Game Winner

Oct 18, 2022

A Flutter application that allows interacting with Pokemons, built using Clean Archtitecture structure and riverpod as state management.

A Flutter application that allows interacting with Pokemons, built using Clean Archtitecture structure and riverpod as state management.

pokedex Welcome to the Pokedex project! Getting Started Instructions to run: This project uses flutter version 3.0.3, Make sure you have that version

Nov 22, 2022

A guide to using Riverpod in your project.

riverpod_explorer A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you starte

Dec 5, 2022

Complete Implementation of Google maps in Flutter using Riverpod

Complete Implementation of Google maps in Flutter using Riverpod

Complete Implementation of Google maps in Flutter using Riverpod (Google Maps ft. Auto Suggestions, GPS tracking, Nearby places, polylines, directions, Review, Contact Details & Photos of a place and much more..)

Dec 31, 2022

Home app - A dynamic flutter app which can be used to generate alerts, set alarms and send sms or call someone

Home app - A dynamic flutter app which can be used to generate alerts, set alarms and send sms or call someone

first_app A dynamic flutter app which can be used to generate alerts, set alarms

Apr 9, 2022

Flutter plugin for creating static & dynamic app shortcuts on the home screen.

Flutter plugin for creating static & dynamic app shortcuts on the home screen.

Flutter Shortcuts Show some ❤️ and ⭐ the repo Why use Flutter Shortcuts? Flutter Shortcuts Plugin is known for : Flutter Shortcuts Fast, performant &

Sep 26, 2022

Flutter plugin for creating static & dynamic app shortcuts on the home screen.

Flutter plugin for creating static & dynamic app shortcuts on the home screen.

Flutter Shortcuts Compatibility ✅ Android ❌ iOS (active issue: iOS support for quick actions) Show some ❤️ and ⭐ the repo Why use Flutter Shortcuts? F

Sep 26, 2022
Owner
Dominique Rwema Bagirishya
Software Developer
Dominique Rwema Bagirishya
Dynamic var - Dart dilinde dynamic ve var veri tiplerini anlamanızı kolaylaştıracak örnektir.

dynamic ve var arasındaki fark dynamic Tanımlanan değişkenin tipi daha sonra kod içerisinde değişebilir. Örneğin int olarak tanımlanan bir a değişkeni

Abdullah Sevmez 0 Jan 1, 2022
State Persistence - Persist state across app launches. By default this library store state as a local JSON file called `data.json` in the applications data directory. Maintainer: @slightfoot

State Persistence Persist state across app launches. By default this library store state as a local JSON file called data.json in the applications dat

Flutter Community 70 Sep 28, 2022
🌈 Repository for a compass project, basically an App for displaying bank transfers, with API requests, Flag persistence, Infinite Scroll, Error Handling, Unit Tests, Extract Sharing working with SOLID, BLoC and Designer Patterns.

?? Green Bank Aplicação desenvolvida em Flutter com intuito de trabalhar conexão com API, Gerenciamento de estado usando BLoC, Refatoração, Arquitetur

André Guerra Santos 28 Oct 7, 2022
Riverpod Messages Listener: A message notification listener for apps build with Riverpod

Riverpod Messages Listener Hello all, this is the home page of riverpod_messages package, a message notification listener for apps build with Riverpod

Fabrizio Tognetto 4 Dec 8, 2022
The app with The elm architecture demonstration made with ClojureDart on Flutter

clojure_dart_tea_example ClojureDart Flutter project with demonstration of: Graphql usage; managing business logic and state with TEA*-like MVU archit

Artur Dumchev 8 Dec 19, 2022
A basic demonstration for creating a Jardinains (Fancy Pong-Like) Game in Flutter

Jardinains Flutter! ?? Play Here - Web https://flutter-jardinains.web.app/#/ Demo Note: For optimal performance run the project in profile mode using

Paras Jain 60 Nov 12, 2022
Shopify Tag and Product Management App using Flutter and Riverpod State Management

Myshopify App A Simple Flutter Application project to get List of Tags, Products and Product Details from shopify https://shopicruit.myshopify.com/adm

Idowu Tomiwa 5 Nov 12, 2022
Fully Featured Chat App Using Firebase, RiverPod and much more.

LetsChat Fully Featured Chat App Using Firebase, RiverPod and much more. LetsChat app has a beautiful responsive UI. The app includes many features li

Mateen Mehmood 94 Dec 6, 2022
Starting template for a new Flutter project. Using clean architecture + Riverpod.

flutter_project_template_riverpod Installation Add Flutter to your machine Open this project folder with Terminal/CMD Ensure there's no cache/build le

Bahri Rizaldi 39 Dec 27, 2022