这是我的代码 ---------> 编码区 <-------------
// My Code //
import 'dart:ui';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:form_field_validator/form_field_validator.dart';
import 'package:inventory_app/models/profiles.dart';
import 'package:inventory_app/pallete.dart';
import 'package:inventory_app/screens/screens.dart';
import 'package:inventory_app/widgets/widgets.dart';
class CreateNewAccount extends StatefulWidget {
const CreateNewAccount({super.key});
@override
State<CreateNewAccount> createState() => _CreateNewAccountState();
}
class _CreateNewAccountState extends State<CreateNewAccount> {
final formKey = GlobalKey<FormState>();
Profile profile = Profile();
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Stack(
children: [
BackgroundImage(image: 'assets/images/1167.png'),
Scaffold(
backgroundColor: Colors.transparent,
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: size.height * 0.1,
),
Stack(
children: [
Center(
child: ClipOval(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 3, sigmaY: 3),
child: CircleAvatar(
radius: size.width * 0.03,
backgroundColor: Colors.grey[400]?.withOpacity(0.4),
child: Icon(FontAwesomeIcons.user,
color: kWhite, size: size.width * 0.03),
),
),
),
),
Positioned(
top: size.height * 0.02,
left: size.width * 0.50,
child: Container(
height: size.height * 0.05,
width: size.width * 0.05,
decoration: BoxDecoration(
color: kBlue,
shape: BoxShape.circle,
border: Border.all(color: kWhite, width: 2)),
child: Icon(
FontAwesomeIcons.arrowUp,
color: kWhite,
),
),
),
],
),
SizedBox(
height: size.height * 0.1,
),
Form(
key: formKey,
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Container(
height: size.height * 0.08,
width: size.width * 0.3,
decoration: BoxDecoration(
color: Colors.green[200]?.withOpacity(0.5),
borderRadius: BorderRadius.circular(16),
),
child: Center(
child: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
prefixIcon: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20),
child: Icon(
FontAwesomeIcons.envelope,
size: 28,
color: kWhite,
),
),
hintText: 'Email-Register',
hintStyle: kBodyText,
),
style: kBodyText,
keyboardType: TextInputType.emailAddress,
onSaved: (String? emails) {
profile.emails = emails;
},
validator: MultiValidator([
RequiredValidator(
errorText: "Please Enter Your Email !"),
EmailValidator(
errorText:
"Please Enter It As The Email Form !"),
]),
textInputAction: TextInputAction.next,
),
),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Container(
height: size.height * 0.08,
width: size.width * 0.3,
decoration: BoxDecoration(
color: Colors.green[200]?.withOpacity(0.5),
borderRadius: BorderRadius.circular(16),
),
child: Center(
child: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
prefixIcon: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 20),
child: Icon(
FontAwesomeIcons.lock,
size: 28,
color: kWhite,
),
),
hintText: "Password-Register",
hintStyle: kBodyText,
),
obscureText: true,
style: kBodyText,
keyboardType: TextInputType.name,
onSaved: (String? passwords) {
profile.passwords = passwords;
},
validator: RequiredValidator(
errorText: "Please Enter Your Password !"),
textInputAction: TextInputAction.next,
)),
),
),
SizedBox(
height: 25,
),
Container(
height: size.height * 0.08,
width: size.width * 0.2,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Colors.blue[200],
),
child: FloatingActionButton(
onPressed: () async {
if (formKey.currentState!.validate()) {
formKey.currentState?.save();
try {
await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: profile.emails!,
password: profile.passwords!)
.then((value) {
formKey.currentState?.reset();
Fluttertoast.showToast(
msg: "Register Finished",
gravity: ToastGravity.CENTER,
);
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) {
return LoginScreen();
}));
});
} on FirebaseAuthException catch (e) {
print(e.code);
String? message;
if (e.code == 'email-already-in-use') {
message = "This Email Is Already Registered";
} else if (e.code == "weak-password") {
message =
"Password Should More Than 6 Letters";
} else {
message = e.message;
}
Fluttertoast.showToast(
msg: message!,
gravity: ToastGravity.CENTER,
);
}
}
},
backgroundColor: Colors.cyan[200]?.withOpacity(0.5),
child: Text(
"Register",
style: kBodyText,
),
),
),
SizedBox(
height: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Already have an account?", style: kBodyText),
GestureDetector(
onTap: () {
Navigator.pushNamed(context, '/');
},
child: Text(" Login",
style: kBodyText.copyWith(
color: kBlue, fontWeight: FontWeight.bold)),
),
],
),
SizedBox(
height: 40,
)
],
),
),
],
),
),
),
],
);
}
}
------------------------ 完成代码区 ---------------------- ----------------
最后,我想要将错误文本移至框中的指南或代码。 谢谢!!!!.
使用下面的代码。
Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
children: [
TextFormField(
decoration: InputDecoration(
border: commonBorder,
enabledBorder: commonBorder,
errorBorder: commonBorder,
focusedBorder: commonBorder,
),
validator: (val) {
if (val!.isEmpty) return 'Enter valid name'; // change your condition
},
),
height15,
TextFormField(
decoration: InputDecoration(
border: commonBorder,
enabledBorder: commonBorder,
errorBorder: commonBorder,
focusedBorder: commonBorder,
),
validator: (val) {
if (val!.isEmpty) return 'Enter valid email'; // change your condition
},
),
],
),
),
通用边境代码
OutlineInputBorder? commonBorder = OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: const BorderSide(color: Colors.grey),
);
单击按钮
if (_formKey.currentState!.validate()) {
//your navigation code
}
输出: