simple_setting 0.0.4+1 copy "simple_setting: ^0.0.4+1" to clipboard
simple_setting: ^0.0.4+1 copied to clipboard

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",
  };
}
  • If you use json file, skip Step 1

Step 2 #

Init language info and wrap your first widget by SettingProvider

void main() {
  SimpleSetting.init(languageData: Lang.vi);
  runApp(const MyApp().provider);
}
// for json
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SimpleSetting.init(languageData: await "path to asset json file".js());
  runApp(const MyApp().provider);
}

Step 3 #

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

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

// or use extension for Text widget
Text("title").obs()

Step 4 #

When you want to change language, use this statement

SimpleSetting.changeLanguage(Lang.en);

// for json
SimpleSetting.changeLanguage(await "path to asset json file".js());

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
});
  • If you need format a string with parameter, you can parse params to obs extension
class Lang {
  static const Map<String, String> vi = {
    "title": "Ví dụ :id",
  };

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

// and use
Text("title".obs(params: {"id": 1}));

// or
Text("title").obs(params: {"id": 1});

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 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: const MyHomePage(title: "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> {

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

Publisher

unverified uploader

A package for internationalizing flutter apps by simple way.

Repository (GitLab)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on simple_setting