import 'package:flutter/material.dart';标签:20.0,const,labelText,text,5.23,TextEditingController,isNotEmpty From: https://www.cnblogs.com/kongxiangzeng/p/18246569
import '../Do/UserDao.dart';
class RegisterPage extends StatelessWidget {
RegisterPage({required Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
// 创建控制器
TextEditingController usernameController = TextEditingController();
TextEditingController passwordController = TextEditingController();
TextEditingController username2Controller = TextEditingController();
TextEditingController phoneNumberController = TextEditingController();
TextEditingController emailController = TextEditingController();
return Scaffold(
appBar: AppBar(
title: const Text('注册'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: ListView( // 使用ListView替换Column
children: <Widget>[
TextFormField(
controller: usernameController, // 将控制器传递给文本字段
decoration: InputDecoration(
labelText: '账号',
),
),
const SizedBox(height: 20.0),
TextFormField(
controller: passwordController, // 将控制器传递给文本字段
decoration: const InputDecoration(
labelText: '密码',
),
obscureText: true,
),
const SizedBox(height: 20.0),
TextFormField(
controller: username2Controller, // 将控制器传递给文本字段
decoration: const InputDecoration(
labelText: '用户名',
),
),
const SizedBox(height: 20.0),
TextFormField(
controller: phoneNumberController, // 将控制器传递给文本字段
decoration: const InputDecoration(
labelText: '手机号码',
),
keyboardType: TextInputType.phone,
),
const SizedBox(height: 20.0),
TextFormField(
controller: emailController, // 将控制器传递给文本字段
decoration: const InputDecoration(
labelText: '邮箱',
),
keyboardType: TextInputType.emailAddress,
),
const SizedBox(height: 20.0),
ElevatedButton(
onPressed: () {
// 获取用户输入的值
String username = usernameController.text.trim();
String password = passwordController.text.trim();
String username2 = username2Controller.text.trim();
String phoneNumber = phoneNumberController.text.trim();
String email = emailController.text.trim();
// 做简单的输入验证,你也可以根据需要增加更多的验证逻辑
if (username.isNotEmpty &&
password.isNotEmpty &&
username2.isNotEmpty &&
phoneNumber.isNotEmpty &&
email.isNotEmpty) {
// 调用后端注册函数
register(context, username, password, username2, phoneNumber, email);
} else {
// 提示用户输入完整的信息
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('请输入完整的信息'),
),
);
}
},
child: const Text('注册'),
),
],
),
),
);
}
}