0

I'm currently using internationalization from this link: https://medium.com/flutter-community/flutter-internationalization-the-easy-way-using-provider-and-json-c47caa4212b2

But I change the buttons to CupertinoSegmentedControl instead.

enter image description here

The problem is the segment keep losing state when I leave the page.

I tried using AutomaticKeepAliveClientMixin but still didn't work.

Any answer will be appreciated!

leemuljadi
  • 410
  • 1
  • 3
  • 14

1 Answers1

0

It's simple, you need to save/restore values, give shared_prefs a try

I'll do it using my plugin localize_and_translate

Here is a simple implementation for it

  1. You get the active language code with method it will return language code : en for example

    translator.currentLanguage();

2.You save to shared_prefs like following

import 'package:shared_preferences/shared_preferences.dart';

SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('code', translator.currentLanguage());

3. Retrive like following inside your widget tree

prefs.getString('code') == 'en' ? // widget for true : // widget for false,

Or you can simply use my plugin directly without shared_prefs

translator.currentLanguage() == 'en' ? // widget for true : // widget for false,
Mohamed Sayed
  • 844
  • 5
  • 15
  • Thanks for your answer, but I'm still struggling to create logic to save the value in shared_prefs based on the detected locale and show it on the groupValue from CupertinoSegmentedControl. Can you please elaborate with a more concrete implementation? – leemuljadi Feb 20 '20 at 22:16