flutter_translate_preferences 1.1.0+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 75

flutter_translate_preferences #

A flutter_translate extension for automatically saving & restoring the selected locale.

[1.0.0] #

  • Initial release
  • Implemented the functionality to automatically save/restore the selected locale

[1.0.1] #

  • Fixed some issues related to async calls

[1.1.0] #

  • Implementation for the ITranslatePreferences interface

[1.1.0+1] #

example/lib/main.dart

import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:flutter_translate_preferences/flutter_translate_preferences.dart';

void main() async
{
  // Necessary since https://github.com/flutter/flutter/pull/38464 in order to access platform channels before runApp
  WidgetsFlutterBinding.ensureInitialized();

  var delegate = await LocalizationDelegate.create(
          fallbackLocale: 'en_US',
          supportedLocales: ['en_US', 'es', 'fa'],
          preferences: TranslatePreferences());

  runApp(LocalizedApp(delegate, MyApp()));
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    var localizationDelegate = LocalizedApp.of(context).delegate;

    return LocalizationProvider(
      state: LocalizationProvider.of(context).state,
      child: MaterialApp(
        title: 'Flutter Translate Demo',
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          localizationDelegate
        ],
        supportedLocales: localizationDelegate.supportedLocales,
        locale: localizationDelegate.currentLocale,
        theme: ThemeData(primarySwatch: Colors.blue),
        home: MyHomePage(),
        ),
      );
  }
}

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

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  int _counter = 0;

  void _decrementCounter() => setState(() => _counter--);

  void _incrementCounter() => setState(() => _counter++);

  @override
  Widget build(BuildContext context) {
    var localizationDelegate = LocalizedApp.of(context).delegate;

    return Scaffold(
      appBar: AppBar(
        title: Text(translate('app_bar.title')),
        ),
      body:  Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(translate('language.selected_message', args: {'language': translate('language.name.${localizationDelegate.currentLocale.languageCode}')})),
            Padding(
                    padding: EdgeInsets.only(top: 25, bottom: 160),
                    child: CupertinoButton.filled(
                      child: Text(translate('button.change_language')),
                      padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 36.0),
                      onPressed: () => _onActionSheetPress(context),
                      )
                    ),

            Padding(padding: const EdgeInsets.only(bottom: 10),
                            child: Text(translatePlural('plural.demo', _counter))
                    ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                IconButton(
                  icon: Icon(Icons.remove_circle),
                  iconSize: 48,
                  onPressed: _counter > 0 ? () => setState(() => _decrementCounter()) : null,
                  ),
                IconButton(
                  icon: Icon(Icons.add_circle),
                  color: Colors.blue,
                  iconSize: 48,
                  onPressed: () => setState(() => _incrementCounter()),
                  ),
              ],
              )

          ],
          ),
        ),
      );
  }

  void showDemoActionSheet({BuildContext context, Widget child}) {
    showCupertinoModalPopup<String>(
            context: context,
            builder: (BuildContext context) => child).then((String value)
            {
              changeLocale(context, value);
            });
  }

  void _onActionSheetPress(BuildContext context) {
    showDemoActionSheet(
      context: context,
      child: CupertinoActionSheet(
        title: Text(translate('language.selection.title')),
        message: Text(translate('language.selection.message')),
        actions: <Widget>[
          CupertinoActionSheetAction(
            child: Text(translate('language.name.en')),
            onPressed: () => Navigator.pop(context, 'en_US'),
            ),
          CupertinoActionSheetAction(
            child: Text(translate('language.name.es')),
            onPressed: () => Navigator.pop(context, 'es'),
            ),
          CupertinoActionSheetAction(
            child: Text(translate('language.name.fa')),
            onPressed: () => Navigator.pop(context, 'fa'),
            ),
        ],
        cancelButton: CupertinoActionSheetAction(
          child: Text(translate('button.cancel')),
          isDefaultAction: true,
          onPressed: () => Navigator.pop(context, null),
          ),
        ),
      );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_translate_preferences: ^1.1.0+1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_translate_preferences/flutter_translate_preferences.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Apr 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health issues and suggestions

Document public APIs. (-1 points)

5 out of 5 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/flutter_translate_preferences.dart.

Run flutter format to format lib/flutter_translate_preferences.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_translate ^1.4.0+2 1.6.0
shared_preferences ^0.5.3+5 0.5.6+3
Transitive dependencies
collection 1.14.11 1.14.12
flutter_device_locale 0.4.0
flutter_web_plugins 0.0.0
meta 1.1.8
plugin_platform_interface 1.0.2
shared_preferences_macos 0.0.1+6
shared_preferences_platform_interface 1.0.3
shared_preferences_web 0.1.2+4
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8