floca 0.1.6+16 floca: ^0.1.6+16 copied to clipboard
Library for Flutter app localization. Generates Dart code with string constants, ready to plug into MaterialApp.
import 'package:flutter/material.dart';
import 'generated_i18n.dart'; // file generated by floca
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// the [localeIndex] will determine what to pass as the [locale]
// argument to the [MaterialApp] constructor. It will allow us to switch
// between supported locales manually.
int localeIndex = -1; // -1 for default locale
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: localizationsDelegates,
supportedLocales: supportedLocales,
locale: localeIndex < 0 ? null : supportedLocales[localeIndex],
// the current [context] was made before we created the MaterialApp
// with localization. To use a localized context, we need a new builder
home: Builder(builder: (BuildContext context) {
// ok, this [context] knows the locale
return Scaffold(
appBar: AppBar(
title: Text(localeIndex < 0
? 'Default locale'
: 'Locale $localeIndex')),
body: Center(
child: Text(
context.i18n
.hello, // 'hello', or 'hola', or 'привет' depending on locale
style: Theme.of(context).textTheme.headline3)),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
// increase localeIndex and rebuild MaterialApp with
// other [locale] argument
if (++localeIndex >= supportedLocales.length) {
localeIndex = -1;
}
}),
child: Icon(Icons.g_translate),
));
}));
}
}