Hello,
Sorry for the late reply. Kindly check the code below as per your request and be aware that the code is extracted from my project if you have any problem dont' hesitate to post back.
Add the below to your pubspec.yaml
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl:
intl_translation:
scoped_model: ^1.0.1
Main.dart
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:myapp/translations.dart';
import 'package:scoped_model/scoped_model.dart';
class AppModel extends Model {
Locale _appLocale = Locale('ar'); //-> check this param to en to open the default app in English
Locale get appLocal => _appLocale ?? Locale("ar");
void changeDirection() {
if (_appLocale == Locale("ar")) {
_appLocale = Locale("en");
} else {
_appLocale = Locale("ar");
}
notifyListeners();
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<AppModel>(
builder: (context, child, model) => MaterialApp(
debugShowCheckedModeBanner: false,
locale: model.appLocal,
localizationsDelegates: [
const Translations(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('ar', ''),
const Locale('en', ''),
],
theme: new ThemeData(
primarySwatch: Colors.red,
accentColor: Colors.red,
backgroundColor: Colors.red,
),
home: new Center(TranslationBase.of(context).title),
));
}
}
class ScopeModelWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModel<AppModel>(model: AppModel(), child: MyApp());
}
}
void main() => runApp(new ScopeModelWrapper());
translations.dart is below
import 'dart:async';
import 'package:flutter/foundation.dart' show SynchronousFuture;
import 'package:flutter/material.dart';
class TranslationBase {
TranslationBase(this.locale);
final Locale locale;
static TranslationBase of(BuildContext context) {
return Localizations.of<TranslationBase>(context, TranslationBase);
}
static Map<String, Map<String, String>> _localizedValues = {
'en': {
'lang': 'الإصدار العربي',
'title': 'Flutter Team from Lebanon',
},
'ar': {
'lang': 'English Version',
'title': 'فريق فلاتر من لبنان',
},
};
String get lang {
return _localizedValues[locale.languageCode]['lang'];
}
String get title {
return _localizedValues[locale.languageCode]['title'];
}
}
class Translations extends LocalizationsDelegate<TranslationBase> {
const Translations();
@override
bool isSupported(Locale locale) => ['en', 'ar'].contains(locale.languageCode);
@override
Future<TranslationBase> load(Locale locale) {
// Returning a SynchronousFuture here because an async "load" operation
// isn't needed to produce an instance of DemoLocalizations.
return SynchronousFuture<TranslationBase>(TranslationBase(locale));
}
@override
bool shouldReload(Translations old) => false;
}
finally, in order to be able to switch between English Version and others use the below
new ScopedModelDescendant<AppModel>(
builder: (context, child, model) => MaterialButton(
height: 60.0,
// color: const Color.fromRGBO(119, 31, 17, 1.0),
child: new Text(
TranslationBase.of(context).lang,
style: new TextStyle(
color: Colors.black,
fontSize: 10.0,
fontWeight: FontWeight.w100,
letterSpacing: 0.3,
),
),
onPressed: () {
setState(() {
model.changeDirection();
});
},
)),
Best Regards
I love flutter