1,创建配置类
```
import 'package:get/get.dart';
class Messages extends Translations{
@override
Map<String,Map<String,String>> get keys => {
"zh_CN":{
"hello": '你好 世界',
},
"en_US":{
"hello": "Hello World"
},
"jp_JP":{
"hello": "こんにちは世界"
},
"ey_EY":{
"hello": "Здравствуй, мир. "
},
'de_DE':{
'hello':"Hello Weilt",
}
};
}
```
2,在main.dar中引入
```
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_app_getx/binding/searchbinding.dart';
import 'package:flutter_app_getx/binding/shopbinding.dart';
import 'package:flutter_app_getx/language/language.dart';
import 'package:flutter_app_getx/page/searchpage.dart';
import 'controller/controller.dart';
import 'package:flutter_app_getx/shoppage.dart';
import 'package:flutter_app_getx/tabsss.dart';
import 'package:flutter_app_getx/testpage.dart';
import 'package:get/get.dart';
import 'binding/binding.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
initialBinding: AllBindingController(),
translations: Messages(),
locale: Locale("zh", "CN"),
fallbackLocale: Locale("en", "US"),
getPages: [
GetPage(name: "/", page: () => TabPage()),
GetPage(
name: "/shop",
page: () => ShopControllerPage(),
binding: ShopBindingController(),
middlewares: []),
GetPage(
name: "/search",
page: () => SearchPage(),
binding: SearchBindingController(),
middlewares: []),
],
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late Timer _timer;
int _counter = 0;
//创建实例
ContainerPage counter = Get.put(ContainerPage());
@override
void initState() {
super.initState();
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
counter.int();
});
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text("Getx"),
),
body: ListView(
children: [
Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.blue,
alignment: Alignment.center,
child: Text("flutter detail dialog"),
),
InkWell(
onTap: () {
Get.defaultDialog(
title: "提示信息",
middleText: "您确定要删除吗",
confirm: ElevatedButton(
onPressed: () {
print("确定");
// Navigator.of(context).pop();
Get.back();
},
child: Text("确定"),
),
cancel: ElevatedButton(
onPressed: () {
print("取消");
// Navigator.of(context).pop();
Get.back();
},
child: Text("取消"),
),
);
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.green,
alignment: Alignment.center,
child: Text("getx detail dialog"),
),
),
Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.orange,
alignment: Alignment.center,
child: Text("getx detail dialog"),
),
InkWell(
onTap: () {
Get.bottomSheet(
Container(
color: Colors.white,
height: 200,
child: Column(
children: [
InkWell(
onTap: () {
Get.changeTheme(ThemeData.light());
Get.back();
},
child: ListTile(
leading: Icon(
Icons.wb_sunny_outlined,
color: Colors.black,
),
title: Text(
"白天模式",
style: TextStyle(color: Colors.black),
),
),
),
InkWell(
onTap: () {
Get.changeTheme(ThemeData.dark());
Get.back();
},
child: ListTile(
leading: Icon(
Icons.wb_sunny,
color: Colors.black,
),
title: Text(
"夜晚模式",
style: TextStyle(color: Colors.black),
),
),
),
],
),
),
);
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 100,
color: Colors.green,
alignment: Alignment.center,
child: Text("getxbotton"),
),
),
InkWell(
onTap: () {
counter.int();
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.green,
alignment: Alignment.center,
child: Column(
children: [
Obx(() => Text(counter.countesssssr.toString())),
Text("getController")
],
),
),
),
InkWell(
onTap: () {
// _timer.cancel();
//counter.int();
Get.to(TextPage());
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.orange,
alignment: Alignment.center,
child: Column(
children: [Text("TextPage")],
),
),
),
InkWell(
onTap: () {
// _timer.cancel();
//counter.int();
//Get.to(ShopControllerPage());
Get.toNamed("shop", arguments: "1111111");
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.green,
alignment: Alignment.center,
child: Column(
children: [Text("shopcontrolller")],
),
),
),
InkWell(
onTap: () {
// _timer.cancel();
//counter.int();
//Get.to(ShopControllerPage());
Get.toNamed("search", arguments: "2222");
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.orange,
alignment: Alignment.center,
child: Column(
children: [Text("searchcontrolller")],
),
),
),
Text("hello".tr),
InkWell(
onTap: () {
// _timer.cancel();
//counter.int();
//Get.to(ShopControllerPage());
// Get.toNamed("search", arguments: "2222");
var local = Locale("zh", "CN");
Get.updateLocale(local);
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.orange,
alignment: Alignment.center,
child: Column(
children: [Text("切换中文")],
),
),
),
InkWell(
onTap: () {
// _timer.cancel();
//counter.int();
//Get.to(ShopControllerPage());
// Get.toNamed("search", arguments: "2222");
var local = Locale("en","US");
Get.updateLocale(local);
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.orange,
alignment: Alignment.center,
child: Column(
children: [Text("切换英文")],
),
),
),
InkWell(
onTap: () {
// _timer.cancel();
//counter.int();
//Get.to(ShopControllerPage());
// Get.toNamed("search", arguments: "2222");
var local = Locale("jp","JP");
Get.updateLocale(local);
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.orange,
alignment: Alignment.center,
child: Column(
children: [Text("切换日语")],
),
),
),
InkWell(
onTap: () {
// _timer.cancel();
//counter.int();
//Get.to(ShopControllerPage());
// Get.toNamed("search", arguments: "2222");
var local = Locale("ey","EY");
Get.updateLocale(local);
},
child: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.orange,
alignment: Alignment.center,
child: Column(
children: [Text("切换俄语")],
),
),
),
],
),
);
}
}
```
3,需要哪里翻译的,就在配置类里面进行全局配置
```
import 'package:get/get.dart';
class Messages extends Translations{
@override
Map<String,Map<String,String>> get keys => {
"zh_CN":{
"hello": '你好 世界',
},
"en_US":{
"hello": "Hello World"
},
"jp_JP":{
"hello": "こんにちは世界"
},
"ey_EY":{
"hello": "Здравствуй, мир. "
},
'de_DE':{
'hello':"Hello Weilt",
}
};
}
```
```
```