klocalizations_flutter 0.0.8 copy "klocalizations_flutter: ^0.0.8" to clipboard
klocalizations_flutter: ^0.0.8 copied to clipboard

Wrapper around flutter_localizations, with some extra functionality and abstracting some of the common logic.

example/lib/main.dart

import 'package:example/http_loader.dart';
import 'package:flutter/material.dart';
import 'package:klocalizations_flutter/klocalizations_flutter.dart';

// Define you locales somewhere
const supportedLocales = [
  Locale('en', 'GB'),
  Locale('es', 'ES'),
  Locale('ar', 'AR'),
  Locale('gr', 'GR'),
];

void main() {
  runApp(
    KLocalizations.asChangeNotifier(
      locale: supportedLocales[0],
      defaultLocale: supportedLocales[0],
      supportedLocales: supportedLocales,
      loader: HttpLoader(),
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final klocalizations = KLocalizations.of(context);

    return MaterialApp(
      title: 'Flutter Demo',
      locale: klocalizations?.locale,
      supportedLocales: klocalizations?.supportedLocales ?? [],
      localizationsDelegates: [
        klocalizations?.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'home.title'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    var theme = Theme.of(context);
    var klocalizations = KLocalizations.of(context);

    return Scaffold(
      appBar: AppBar(
        title: LocalizedText(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            LocalizedText(
              'home.welcome',
              style: theme.textTheme.headline5,
            ),
            LanguageSelector(
              locale: klocalizations!.locale,
              supportedLocales: klocalizations.supportedLocales,
              onChange: (locale) {
                klocalizations
                    .setLocale(locale ?? klocalizations.defaultLocale);
              },
            ),
            LocalizedText.selectable(
              'home.counter',
              params: {
                'count': _counter,
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
3
likes
130
pub points
71%
popularity

Publisher

unverified uploader

Wrapper around flutter_localizations, with some extra functionality and abstracting some of the common logic.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, flutter_localizations, provider

More

Packages that depend on klocalizations_flutter