app_local 1.0.0 app_local: ^1.0.0 copied to clipboard
Easily localize your app to multiple languages by using json file in assets folder and you can change the language without close the app
import 'package:flutter/material.dart';
import 'package:app_local/app_local.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Locales.init(localeNames: ['en', 'ar', 'fa'],localPath: "assets/a/"); // get last saved language
// remove await async (to get system language as default)
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LocaleBuilder(
builder: (locale) => MaterialApp(
title: 'Flutter Locales',
localizationsDelegates: Locales.delegates,
supportedLocales: Locales.supportedLocales,
locale: locale,
theme: ThemeData(primarySwatch: Colors.blue),
home: HomeScreen(),
),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: LocaleText('localization')),
body: Center(
child: LocaleText('welcome', style: TextStyle(fontSize: 32)),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (_) => SettingScreen()));
},
child: Icon(Icons.settings),
),
);
}
}
class SettingScreen extends StatelessWidget {
const SettingScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(Locales.string(context, 'setting'))),
// with extension
// appBar: AppBar(title: Text(context.localeString('setting'))),
body: Column(
children: [
ListTile(
onTap: () => Locales.change(context, 'en'),
title: LocaleText('english'),
),
ListTile(
onTap: () => Locales.change(context, 'ar'),
title: LocaleText('Arabic'),
),
// to change language with Extension
ListTile(
onTap: () => context.changeLocale('fa'),
title: LocaleText('farsi'),
),
Text('Current Locale: ' +
Locales.currentLocale(context)!.languageCode),
// Text('Current Locale: ' + context.currentLocale.languageCode), // with Extension
],
),
);
}
}