I'm new to flutter and Dart currently, I'm using flutter 2.03 and trying to build a multi-language app using the easy_localization package (3.0.0). At first, everything was alright when I try to change the app language from the setting page or the first page which is shown one time, the app translates the content and stays on the same page but yesterday the app started reloading when I change the app locale :
onChanged: (newValue) async {
if (newValue == 'English') {
await context.setLocale(Locale('en'));
} else if (newValue == 'Français') {
await context.setLocale(Locale('fr'));
} else if (newValue == 'العربية') {
await context.setLocale(Locale('ar'));
}
},
All I want is to make the app make hot reload and translate the page and stay on the same screen without reloading the whole app and back to Home Screen.
Main.dart
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:sg_user_dash/screens/homescreen.dart';
import 'package:sg_user_dash/screens/language.dart';
import 'package:shared_preferences/shared_preferences.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
runApp(EasyLocalization(
supportedLocales: [Locale('en'), Locale('fr'), Locale('ar')],
path: 'assets/translations',
fallbackLocale: Locale('en'),
child: MyApp()));
}
Future<String> nextdisplay() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool _seen = (prefs.getBool('seen') ?? false);
if (_seen) {
return "Homepage";
} else {
await prefs.setBool('seen', true);
return "walkthrough";
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Smart Government',
theme: ThemeData(),
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
home: FutureBuilder(
future: nextdisplay(),
builder: (context, AsyncSnapshot<String> snapshot) {
if (snapshot.hasData) {
if (snapshot.data == "walkthrough") {
return Language();
} else if (snapshot.data == "Homepage") {
return HomeScreen();
} else {
return Language();
}
}
return Center(
child: CupertinoActivityIndicator(),
);
}));
}
}
Thank you <3