I made a simple login and signup UI, but when i updated my app with firebase connection, i found this bug. my textfield widget text is getting cleared from login page while not from signup page although i copied my signup page code from my login page, the problem only exists on login page.
here's my login page code
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:tutorial_two/widgets/widget_text_field.dart';
import 'package:velocity_x/velocity_x.dart';
import '../utils/auth_controller.dart';
import '../utils/routes.dart';
class LoginScreen extends StatelessWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var emailLoginController = TextEditingController();
var passwordLoginController = TextEditingController();
double h = MediaQuery.of(context).size.height;
return Scaffold(
body: Center(
child: SingleChildScrollView(
child: Column(
children: [
Image.asset(
"assets/images/flutter_logo_modified.png",
height: 200,
width: 200,
),
Container(
width: double.infinity,
margin: const EdgeInsets.only(left: 20, right: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Welcome",
style:
TextStyle(fontSize: 50, fontWeight: FontWeight.bold),
),
Text(
"We are glad to see you back here",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.grey[350],
),
),
const SizedBox(
height: 50,
),
TextFieldWidget(
hint: "Enter E-mail",
obscuretext: false,
prefixiconCode: "0xe22a",
prefixiconfamily: "MaterialIcons",
textFieldController: emailLoginController),
const SizedBox(
height: 20,
),
TextFieldWidget(
hint: "Enter Password",
obscuretext: true,
prefixiconCode: "0xf0050",
prefixiconfamily: "MaterialIcons",
textFieldController: passwordLoginController),
const SizedBox(
height: 30,
),
Row(
children: [
Expanded(
child: Container(),
),
Text(
"Forgot Paswword?",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.grey[500],
),
),
],
),
],
),
),
const SizedBox(
height: 30,
),
Container(
width: 140,
height: 50,
decoration: BoxDecoration(boxShadow: [
BoxShadow(
blurRadius: 10,
spreadRadius: 7,
offset: const Offset(1, 1),
color: Colors.grey.withOpacity(0.1),
),
], borderRadius: BorderRadius.circular(40)),
child: ElevatedButton(
onPressed: () {
AuthController.instance.login(
context,
emailLoginController.text.toString(),
passwordLoginController.text.toString(),
);
},
style: ButtonStyle(
shadowColor: MaterialStateProperty.all(
context.theme.scaffoldBackgroundColor),
),
child: const Text("Login"),
),
),
SizedBox(height: h * 0.08),
RichText(
text: TextSpan(children: <TextSpan>[
TextSpan(
text: 'Don\'t have an account? ',
style: TextStyle(
fontSize: 18,
color: Colors.grey[500],
fontFamily: GoogleFonts.syne().fontFamily,
),
),
TextSpan(
text: 'Create account',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
color: context.theme.cardColor,
fontFamily: GoogleFonts.syne().fontFamily,
),
recognizer: TapGestureRecognizer()
..onTap = () {
Navigator.pushReplacementNamed(
context, MyRoutes.signupRoute);
}),
]),
)
],
),
),
),
);
}
}
here's textfield widget that i made
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:velocity_x/velocity_x.dart';
class TextFieldWidget extends StatelessWidget {
final String hint;
final String label;
final String prefixiconCode;
final String prefixiconfamily;
final String suffixiconCode;
final String suffixiconfamily;
final TextEditingController textFieldController;
final bool obscuretext;
const TextFieldWidget(
{super.key,
this.hint = "",
this.label = "",
this.prefixiconCode = "",
this.prefixiconfamily = "MaterialIcons",
this.suffixiconCode = "",
this.suffixiconfamily = "",
required this.textFieldController,
this.obscuretext = false});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: context.theme.scaffoldBackgroundColor,
borderRadius: BorderRadius.circular(30),
boxShadow: [
BoxShadow(
blurRadius: 10,
spreadRadius: 7,
offset: const Offset(1, 1),
color: context.theme.canvasColor.withOpacity(0.3),
),
]),
child: TextField(
controller: textFieldController,
style: TextStyle(
color: context.theme.cardColor,
fontFamily: GoogleFonts.syne().fontFamily),
decoration: InputDecoration(
fillColor: context.theme.scaffoldBackgroundColor,
focusColor: context.theme.scaffoldBackgroundColor,
iconColor: context.theme.scaffoldBackgroundColor,
prefixIcon: Icon(
IconData(int.parse(prefixiconCode), fontFamily: prefixiconfamily),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: BorderSide(
color: context.theme.scaffoldBackgroundColor, width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: BorderSide(
color: context.theme.scaffoldBackgroundColor, width: 1.0),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
),
hintText: hint,
),
obscureText: obscuretext,
),
);
}
}
and signup page code, just in case it helps. i have already looked github and stack overflow solutions but none of them works for me..
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:tutorial_two/utils/auth_controller.dart';
import 'package:tutorial_two/utils/routes.dart';
import 'package:velocity_x/velocity_x.dart';
import '../widgets/widget_text_field.dart';
class SignupScreen extends StatelessWidget {
const SignupScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var emailController = TextEditingController();
var usernameController = TextEditingController();
var passwordController = TextEditingController();
return Scaffold(
body: Center(
child: SingleChildScrollView(
child: Column(
children: [
Image.asset(
"assets/images/flutter_logo_modified.png",
height: 200,
width: 200,
),
Container(
width: double.infinity,
margin: const EdgeInsets.only(left: 20, right: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Hello There",
style:
TextStyle(fontSize: 50, fontWeight: FontWeight.bold),
),
Text(
"We are glad to welcome you here",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.grey[350],
),
),
const SizedBox(
height: 50,
),
TextFieldWidget(
hint: "Enter E-mail",
obscuretext: false,
prefixiconCode: "0xe22a",
prefixiconfamily: "MaterialIcons",
textFieldController: emailController,
),
const SizedBox(
height: 20,
),
TextFieldWidget(
hint: "Enter Username",
obscuretext: false,
prefixiconCode: "0xf01f3",
prefixiconfamily: "MaterialIcons",
textFieldController: usernameController,
),
const SizedBox(
height: 20,
),
TextFieldWidget(
hint: "Enter Password",
obscuretext: true,
prefixiconCode: "0xf0050",
prefixiconfamily: "MaterialIcons",
textFieldController: passwordController,
),
const SizedBox(
height: 30,
),
Row(
children: [
Expanded(
child: Container(),
),
RichText(
text: TextSpan(children: <TextSpan>[
TextSpan(
text: 'Already have an account? ',
style: TextStyle(
fontSize: 18,
color: Colors.grey[500],
fontFamily: GoogleFonts.syne().fontFamily,
),
),
TextSpan(
text: 'Login',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
color: context.theme.cardColor,
fontFamily: GoogleFonts.syne().fontFamily,
),
recognizer: TapGestureRecognizer()
..onTap = () {
Navigator.pushReplacementNamed(
context, MyRoutes.loginRoute);
}),
]),
)
],
),
],
),
),
const SizedBox(
height: 30,
),
Container(
width: 140,
height: 50,
decoration: BoxDecoration(boxShadow: [
BoxShadow(
blurRadius: 10,
spreadRadius: 7,
offset: const Offset(1, 1),
color: Colors.grey.withOpacity(0.1),
),
], borderRadius: BorderRadius.circular(40)),
child: ElevatedButton(
onPressed: () {
AuthController.instance.register(
context,
emailController.text.toString(),
usernameController.text.toString(),
passwordController.text.toString(),
);
},
style: ButtonStyle(
shadowColor: MaterialStateProperty.all(
context.theme.scaffoldBackgroundColor),
),
child: const Text("Signup"),
),
),
],
),
),
),
);
}
}
output: https://youtu.be/UMKgY3Zatjw
CodePudding user response:
Please take TEXT EDITING CONTROLLER outside of the build method
class xyz extends StatefulWidget{
var emailController = TextEditingController();
var usernameController = TextEditingController();
var passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Container();
}
}