6.2 注册表单编写
实现步骤:
我们可以养成一个习惯,业务界面一开始就写 i18n 定义
第 1 步:i18n 多语言
lib/common/i18n/locale_keys.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| static const loginForgotPassword = 'login_forgot_password'; static const loginSignIn = 'login_sign_in'; static const loginOrText = 'login_or_text'; static const loginSignUp = 'login_sign_up';
static const registerTitle = 'register_title'; static const registerDesc = 'register_desc'; static const registerFormName = 'register_form_name'; static const registerFormEmail = 'register_form_email'; static const registerFormPhoneNumber = 'register_form_phone_number'; static const registerFormPassword = 'register_form_password'; static const registerFormFirstName = 'register_form_first_name'; static const registerFormLastName = 'register_form_last_name'; static const registerHaveAccount = 'register_have_account';
|
lib/common/i18n/locales/locale_en.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| LocaleKeys.loginForgotPassword: 'Forgot Password?', LocaleKeys.loginSignIn: 'Sign In', LocaleKeys.loginSignUp: 'Sign Up', LocaleKeys.loginOrText: '- OR -',
LocaleKeys.registerTitle: 'Register', LocaleKeys.registerDesc: 'Sign up to continue', LocaleKeys.registerFormName: 'User Name', LocaleKeys.registerFormEmail: 'Email', LocaleKeys.registerFormPhoneNumber: 'Phone number', LocaleKeys.registerFormPassword: 'Password', LocaleKeys.registerFormFirstName: 'First name', LocaleKeys.registerFormLastName: 'Last name', LocaleKeys.registerHaveAccount: 'Already have an account?',
|
lib/common/i18n/locales/locale_zh.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| LocaleKeys.loginForgotPassword: '忘记密码?', LocaleKeys.loginSignIn: '登 陆', LocaleKeys.loginSignUp: '注 册', LocaleKeys.loginOrText: '- 或者 -',
LocaleKeys.registerTitle: '欢迎', LocaleKeys.registerDesc: '注册新账号', LocaleKeys.registerFormName: '登录账号', LocaleKeys.registerFormEmail: '电子邮件', LocaleKeys.registerFormPhoneNumber: '电话号码', LocaleKeys.registerFormPassword: '密码', LocaleKeys.registerFormFirstName: '姓', LocaleKeys.registerFormLastName: '名', LocaleKeys.registerHaveAccount: '你有现成账号?',
|
第 2 步:抽取页面头部
这种放入业务组件目录lib/common/components
lib/common/components/page_title.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import 'package:flutter/material.dart'; import 'package:flutter_woo_commerce_getx_learn/common/index.dart';
class PageTitleWidget extends StatelessWidget { final String title;
final String desc;
const PageTitleWidget({ Key? key, required this.title, required this.desc, }) : super(key: key);
@override Widget build(BuildContext context) { return <Widget>[ TextWidget.title1(title), TextWidget.body2(desc), ] .toColumn( crossAxisAlignment: CrossAxisAlignment.start, ) .padding( left: 10, top: 10, bottom: 30, ); } }
|
第 3 步:注册界面
lib/pages/system/register/controller.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| TextEditingController userNameController = TextEditingController(text: "ducafecat5");
TextEditingController emailController = TextEditingController(text: "ducafecat5@gmail.com");
TextEditingController firstNameController = TextEditingController(text: "ducafe");
TextEditingController lastNameController = TextEditingController(text: "cat");
TextEditingController passwordController = TextEditingController(text: "12345678");
|
1 2 3 4 5 6 7 8 9 10
| @override void onClose() { super.onClose(); userNameController.dispose(); emailController.dispose(); firstNameController.dispose(); lastNameController.dispose(); passwordController.dispose(); }
|
1 2 3 4 5
| void onSignUp() {}
void onSignIn() {}
|
lib/pages/system/register/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| Widget _buildForm() { return Form( autovalidateMode: AutovalidateMode.onUserInteraction, child: <Widget>[ TextFormWidget( autofocus: true, controller: controller.userNameController, labelText: LocaleKeys.registerFormName.tr, ),
TextFormWidget( autofocus: true, keyboardType: TextInputType.emailAddress, controller: controller.emailController, labelText: LocaleKeys.registerFormEmail.tr, ),
TextFormWidget( autofocus: true, controller: controller.firstNameController, labelText: LocaleKeys.registerFormFirstName.tr, ),
TextFormWidget( autofocus: true, controller: controller.lastNameController, labelText: LocaleKeys.registerFormLastName.tr, ),
TextFormWidget( controller: controller.passwordController, labelText: LocaleKeys.registerFormPassword.tr, isObscure: true, ).paddingBottom(50),
_buildBtnSignUp(),
_buildTips(),
].toColumn(), ).paddingAll(AppSpace.card); }
|
1 2 3 4 5 6 7
| Widget _buildBtnSignUp() { return ButtonWidget.primary( LocaleKeys.loginSignUp.tr, onTap: controller.onSignUp, ).paddingBottom(AppSpace.listRow); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Widget _buildTips() { return <Widget>[ TextWidget.body2(LocaleKeys.registerHaveAccount.tr), ButtonWidget.text( LocaleKeys.loginSignIn.tr, onTap: controller.onSignIn, textSize: 12, textColor: AppColors.primary, ) ].toRow( mainAxisAlignment: MainAxisAlignment.center, ); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Widget _buildView() { return SingleChildScrollView( child: <Widget>[ PageTitleWidget( title: LocaleKeys.registerTitle.tr, desc: LocaleKeys.registerDesc.tr, ),
_buildForm().card(), ] .toColumn( crossAxisAlignment: CrossAxisAlignment.start, ) .paddingHorizontal(AppSpace.page), ); }
|
第 4 步:进入注册界面
lib/pages/system/splash/controller.dart
1 2 3
| _jumpToPage() { Get.offAllNamed(RouteNames.main); }
|
lib/pages/system/main/controller.dart
1 2 3 4 5 6 7
| @override void onReady() { super.onReady(); _initData();
Get.toNamed(RouteNames.systemRegister); }
|
最后:运行
提交代码到 git