feat: 增加本地化支持

This commit is contained in:
liyi 2025-08-29 09:16:51 +08:00
parent c927b36917
commit d858805563
6 changed files with 176 additions and 26 deletions

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:starwork_flutter/flavors.dart';
@ -17,6 +18,22 @@ class App extends StatelessWidget {
splitScreenMode: true,
builder: (_, child) {
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,
title: F.title,
translations: AppI18n(),
@ -26,7 +43,6 @@ class App extends StatelessWidget {
// fallback 使
getPages: AppPages.pages,
initialRoute: AppRoutes.login,
//
debugShowCheckedModeBanner: false,
);
},

View File

@ -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';
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,
);
}
}

View File

@ -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';
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("获取手机验证码");
}
}

View File

@ -16,7 +16,9 @@ class LoginView extends GetView<LoginController> {
},
child: Scaffold(
body: SafeArea(
child: _buildBody(),
child: SingleChildScrollView(
child: _buildBody(),
),
),
),
);
@ -24,6 +26,7 @@ class LoginView extends GetView<LoginController> {
Widget _buildBody() {
return Container(
height: 1.sh,
margin: EdgeInsets.symmetric(vertical: 48.h),
padding: EdgeInsets.symmetric(horizontal: 32.w),
child: Column(
@ -33,6 +36,10 @@ class LoginView extends GetView<LoginController> {
height: 32.h,
),
_buildPhoneInputAndLoginButton(),
SizedBox(
height: 32.h,
),
_buildPrivacyPolicy(),
],
),
);
@ -68,30 +75,87 @@ class LoginView extends GetView<LoginController> {
}
_buildPhoneInputAndLoginButton() {
return Column(
children: [
TextField(
keyboardType: TextInputType.phone,
maxLength: 11,
decoration: InputDecoration(
counterText: '',
hintText: '请输入手机号码'.tr,
border: const UnderlineInputBorder(),
//
focusedBorder: const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.blue), // 🔥
return Obx(
() => Column(
children: [
TextField(
controller: controller.phoneController,
keyboardType: TextInputType.phone,
textInputAction: TextInputAction.done,
maxLength: controller.phoneNumberSize,
decoration: InputDecoration(
counterText: '',
hintText: '请输入手机号码'.tr,
border: const UnderlineInputBorder(),
//
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: controller.isFormValid.value
? Colors.blue
: Colors.blue.withOpacity(0.5),
), //
// 🔥
),
),
),
),
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
SizedBox(
height: 24.h,
),
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,
),
),
)
],
),
);
}
),
child: Text(
'获取验证码'.tr,
),
)
_buildPrivacyPolicy() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Radio(
value: '1',
groupValue: 1,
onChanged: (value) {},
),
],
);
}

View File

@ -126,6 +126,11 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.2"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_screenutil:
dependency: "direct main"
description:
@ -144,6 +149,14 @@ packages:
description: flutter
source: sdk
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:
dependency: "direct main"
description:
@ -160,6 +173,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
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:
dependency: transitive
description:

View File

@ -12,6 +12,9 @@ environment:
dependencies:
flutter:
sdk: flutter
# 本地化支持
flutter_localizations:
sdk: flutter
# 脚手架
get: ^4.7.2
# 权限申请
@ -22,6 +25,8 @@ dependencies:
shared_preferences: ^2.2.3
# 屏幕适配
flutter_screenutil: ^5.9.3
# 提示
fluttertoast: ^8.2.8
dev_dependencies: