feat: 增加本地化支持
This commit is contained in:
parent
c927b36917
commit
d858805563
18
lib/app.dart
18
lib/app.dart
@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:starwork_flutter/flavors.dart';
|
import 'package:starwork_flutter/flavors.dart';
|
||||||
@ -17,6 +18,22 @@ class App extends StatelessWidget {
|
|||||||
splitScreenMode: true,
|
splitScreenMode: true,
|
||||||
builder: (_, child) {
|
builder: (_, child) {
|
||||||
return GetMaterialApp(
|
return GetMaterialApp(
|
||||||
|
theme: ThemeData(
|
||||||
|
textSelectionTheme: TextSelectionThemeData(
|
||||||
|
cursorColor: Colors.blue.shade200,
|
||||||
|
selectionColor: Colors.blue.shade100,
|
||||||
|
selectionHandleColor: Colors.blue.shade300,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
// 必须配置以下三个本地化代理
|
||||||
|
localizationsDelegates: const [
|
||||||
|
GlobalMaterialLocalizations.delegate, // 提供Material组件本地化字符串
|
||||||
|
GlobalWidgetsLocalizations.delegate, // 定义默认文本方向
|
||||||
|
GlobalCupertinoLocalizations.delegate, // 提供Cupertino组件本地化
|
||||||
|
],
|
||||||
|
supportedLocales: const [
|
||||||
|
Locale('zh', 'CN'), // ✅ 正确:简体中文
|
||||||
|
],
|
||||||
enableLog: true,
|
enableLog: true,
|
||||||
title: F.title,
|
title: F.title,
|
||||||
translations: AppI18n(),
|
translations: AppI18n(),
|
||||||
@ -26,7 +43,6 @@ class App extends StatelessWidget {
|
|||||||
// 必须设置 fallback 没有该语言时使用
|
// 必须设置 fallback 没有该语言时使用
|
||||||
getPages: AppPages.pages,
|
getPages: AppPages.pages,
|
||||||
initialRoute: AppRoutes.login,
|
initialRoute: AppRoutes.login,
|
||||||
// 默认首页
|
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,5 +1,17 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||||
|
import 'package:fluttertoast/fluttertoast.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
|
||||||
class BaseController extends GetxController{
|
class BaseController extends GetxController {
|
||||||
|
void showToast(String message) {
|
||||||
|
Fluttertoast.showToast(
|
||||||
|
msg: message,
|
||||||
|
toastLength: Toast.LENGTH_SHORT,
|
||||||
|
gravity: ToastGravity.CENTER,
|
||||||
|
backgroundColor: Colors.black54,
|
||||||
|
textColor: Colors.white,
|
||||||
|
fontSize: 14.0.sp,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,5 +1,37 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:fluttertoast/fluttertoast.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
import 'package:starwork_flutter/base/base_controller.dart';
|
import 'package:starwork_flutter/base/base_controller.dart';
|
||||||
|
|
||||||
class LoginController extends BaseController {
|
class LoginController extends BaseController {
|
||||||
|
int phoneNumberSize = 11;
|
||||||
|
|
||||||
|
TextEditingController phoneController = TextEditingController();
|
||||||
|
final isFormValid = false.obs;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onInit() {
|
||||||
|
super.onInit();
|
||||||
|
// 监听输入变化
|
||||||
|
phoneController.addListener(_validateForm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onClose() {
|
||||||
|
phoneController.removeListener(_validateForm);
|
||||||
|
phoneController.dispose();
|
||||||
|
super.onClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _validateForm() {
|
||||||
|
isFormValid.value = phoneController.text.length == phoneNumberSize;
|
||||||
|
debugPrint('isFormValid: ${isFormValid.value}');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取手机验证码
|
||||||
|
void requestPhoneCode() {
|
||||||
|
debugPrint("获取手机验证码");
|
||||||
|
showToast("获取手机验证码");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,9 @@ class LoginView extends GetView<LoginController> {
|
|||||||
},
|
},
|
||||||
child: Scaffold(
|
child: Scaffold(
|
||||||
body: SafeArea(
|
body: SafeArea(
|
||||||
child: _buildBody(),
|
child: SingleChildScrollView(
|
||||||
|
child: _buildBody(),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -24,6 +26,7 @@ class LoginView extends GetView<LoginController> {
|
|||||||
|
|
||||||
Widget _buildBody() {
|
Widget _buildBody() {
|
||||||
return Container(
|
return Container(
|
||||||
|
height: 1.sh,
|
||||||
margin: EdgeInsets.symmetric(vertical: 48.h),
|
margin: EdgeInsets.symmetric(vertical: 48.h),
|
||||||
padding: EdgeInsets.symmetric(horizontal: 32.w),
|
padding: EdgeInsets.symmetric(horizontal: 32.w),
|
||||||
child: Column(
|
child: Column(
|
||||||
@ -33,6 +36,10 @@ class LoginView extends GetView<LoginController> {
|
|||||||
height: 32.h,
|
height: 32.h,
|
||||||
),
|
),
|
||||||
_buildPhoneInputAndLoginButton(),
|
_buildPhoneInputAndLoginButton(),
|
||||||
|
SizedBox(
|
||||||
|
height: 32.h,
|
||||||
|
),
|
||||||
|
_buildPrivacyPolicy(),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -68,30 +75,87 @@ class LoginView extends GetView<LoginController> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_buildPhoneInputAndLoginButton() {
|
_buildPhoneInputAndLoginButton() {
|
||||||
return Column(
|
return Obx(
|
||||||
children: [
|
() => Column(
|
||||||
TextField(
|
children: [
|
||||||
keyboardType: TextInputType.phone,
|
TextField(
|
||||||
maxLength: 11,
|
controller: controller.phoneController,
|
||||||
decoration: InputDecoration(
|
keyboardType: TextInputType.phone,
|
||||||
counterText: '',
|
textInputAction: TextInputAction.done,
|
||||||
hintText: '请输入手机号码'.tr,
|
maxLength: controller.phoneNumberSize,
|
||||||
border: const UnderlineInputBorder(),
|
decoration: InputDecoration(
|
||||||
// 获取焦点时的边框
|
counterText: '',
|
||||||
focusedBorder: const UnderlineInputBorder(
|
hintText: '请输入手机号码'.tr,
|
||||||
borderSide: BorderSide(color: Colors.blue), // 🔥 你想要的颜色
|
border: const UnderlineInputBorder(),
|
||||||
|
// 获取焦点时的边框
|
||||||
|
focusedBorder: UnderlineInputBorder(
|
||||||
|
borderSide: BorderSide(
|
||||||
|
color: controller.isFormValid.value
|
||||||
|
? Colors.blue
|
||||||
|
: Colors.blue.withOpacity(0.5),
|
||||||
|
), //
|
||||||
|
// 🔥 你想要的颜色
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
SizedBox(
|
||||||
ElevatedButton(
|
height: 24.h,
|
||||||
onPressed: () {},
|
),
|
||||||
style: ButtonStyle(
|
ElevatedButton(
|
||||||
|
onPressed: controller.isFormValid.value
|
||||||
|
? controller.requestPhoneCode
|
||||||
|
: null,
|
||||||
|
style: ButtonStyle(
|
||||||
|
minimumSize:
|
||||||
|
MaterialStateProperty.all(Size(double.infinity, 44.h)),
|
||||||
|
shape: MaterialStateProperty.all(
|
||||||
|
RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(8.r),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
backgroundColor: MaterialStateProperty.all(
|
||||||
|
controller.isFormValid.value
|
||||||
|
? Colors.blue
|
||||||
|
: Colors.blue.withOpacity(0.5),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: Text(
|
||||||
|
'获取验证码'.tr,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 16.sp,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
height: 22.h,
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () {},
|
||||||
|
child: Text(
|
||||||
|
'密码登录'.tr,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 14.sp,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
color: Colors.grey,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
),
|
_buildPrivacyPolicy() {
|
||||||
child: Text(
|
return Row(
|
||||||
'获取验证码'.tr,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
),
|
children: [
|
||||||
)
|
Radio(
|
||||||
|
value: '1',
|
||||||
|
groupValue: 1,
|
||||||
|
onChanged: (value) {},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
21
pubspec.lock
21
pubspec.lock
@ -126,6 +126,11 @@ packages:
|
|||||||
url: "https://pub.flutter-io.cn"
|
url: "https://pub.flutter-io.cn"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.2"
|
version: "3.0.2"
|
||||||
|
flutter_localizations:
|
||||||
|
dependency: "direct main"
|
||||||
|
description: flutter
|
||||||
|
source: sdk
|
||||||
|
version: "0.0.0"
|
||||||
flutter_screenutil:
|
flutter_screenutil:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -144,6 +149,14 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
fluttertoast:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: fluttertoast
|
||||||
|
sha256: "95f349437aeebe524ef7d6c9bde3e6b4772717cf46a0eb6a3ceaddc740b297cc"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "8.2.8"
|
||||||
get:
|
get:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -160,6 +173,14 @@ packages:
|
|||||||
url: "https://pub.flutter-io.cn"
|
url: "https://pub.flutter-io.cn"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.5.4"
|
version: "4.5.4"
|
||||||
|
intl:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: intl
|
||||||
|
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "0.18.1"
|
||||||
io:
|
io:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@ -12,6 +12,9 @@ environment:
|
|||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
# 本地化支持
|
||||||
|
flutter_localizations:
|
||||||
|
sdk: flutter
|
||||||
# 脚手架
|
# 脚手架
|
||||||
get: ^4.7.2
|
get: ^4.7.2
|
||||||
# 权限申请
|
# 权限申请
|
||||||
@ -22,6 +25,8 @@ dependencies:
|
|||||||
shared_preferences: ^2.2.3
|
shared_preferences: ^2.2.3
|
||||||
# 屏幕适配
|
# 屏幕适配
|
||||||
flutter_screenutil: ^5.9.3
|
flutter_screenutil: ^5.9.3
|
||||||
|
# 提示
|
||||||
|
fluttertoast: ^8.2.8
|
||||||
|
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user