simple_setting

Creator: coderz1093

Last updated:

0 purchases

simple_setting Image
simple_setting Images

Languages

Categories

Add to Cart

Description:

simple setting

Simple Setting #

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';
copied to clipboard
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",
};
}
copied to clipboard

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);
}
copied to clipboard
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()
copied to clipboard
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());
copied to clipboard
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
});
copied to clipboard

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});

copied to clipboard
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.
);
}
}
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.