simple_setting 0.0.1 copy "simple_setting: ^0.0.1" to clipboard
simple_setting: ^0.0.1 copied to clipboard

outdated

A package for internationalizing flutter apps by simple way.

Simple Setting #

pub package

A package for internationalizing flutter apps by simple way.

Usage #

To use this plugin, add simple_setting as a dependency in your pubspec.yaml file.

Step 0 #

Import package

import 'package:simple_setting/simple_setting.dart';

Step 1 #

Define a class to store language map, ex:

class Lang {
  static const Map<String, String> vi = {
    "title": "Ví dụ",
  };

  static const Map<String, String> en = {
    "title": "Example",
  };
}

Step 2 #

Init language info

SimpleSetting.init(languageData: Lang.vi);

Step 3 #

Wrap your first widget by SettingProvider

void main() {
  runApp(const SettingProvider(child: MyApp()));
}

Step 4 #

Catch language setting change by SettingWidget and use language by string extension tr

SettingWidget(builder: (_, __, ___) {
  return MyHomePage(title: "title".tr);
}

Step 5 #

When you want to change language, use this statement

SimpleSetting.changeLanguage(context, Lang.en);

Note: #

If you need automatic change language follow by system language, you can parse langMap parameter to init function, ex:

SimpleSetting.init(langMap: {
    "en_US": Lang.en,
    "vi_VN": Lang.vi
});

Full example #

import 'package:flutter/material.dart';
import 'package:simple_setting/simple_setting.dart';

void main() {
  // SimpleSetting.init(langMap: {
  //   "en_US": Lang.en,
  //   "vi_VN": Lang.vi
  // });
  SimpleSetting.init(languageData: Lang.vi);
  runApp(const SettingProvider(child: MyApp()));
}

class Lang {
  static const Map<String, String> vi = {
    "title": "Ví dụ",
  };

  static const Map<String, String> en = {
    "title": "Example",
  };
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "title",
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SettingWidget(builder: (language, _, __) {
        return MyHomePage(title: "title".tr);
      }),
    );
  }
}

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> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // check current lang
          bool isVi = SettingData.lang == Lang.vi;
          SimpleSetting.changeLanguage(context, isVi ? Lang.en : Lang.vi);
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
6
likes
0
pub points
29%
popularity

Publisher

unverified uploader

A package for internationalizing flutter apps by simple way.

Repository (GitLab)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, flutter_bloc

More

Packages that depend on simple_setting