Skip to content

Instantly share code, notes, and snippets.

@prateek-code-22
Created June 3, 2021 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prateek-code-22/ae010f8f0f0574ad68b2962284548cf8 to your computer and use it in GitHub Desktop.
Save prateek-code-22/ae010f8f0f0574ad68b2962284548cf8 to your computer and use it in GitHub Desktop.
Clearing the text field after returning from home page.
import 'package:catalog_app/utils/route.dart';
import 'package:flutter/material.dart';
import 'package:velocity_x/velocity_x.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
bool changeButton = false;
String name = " ";
final _formKey = GlobalKey<FormState>();
movetoHome(BuildContext context) async {
if (_formKey.currentState.validate()) {
setState(() {
changeButton = true;
});
await Future.delayed(Duration(seconds: 1));
await Navigator.pushNamed(context, MyRoutes.homeRoute);
setState(() {
changeButton = false;
});
//checking the text field
if (userController.text != null) {
clearText();
}
}
}
//Initalizing the controllers for textfield
final userController = TextEditingController();
final passwordController = TextEditingController();
//clear function to clear the text
void clearText() {
userController.clear();
passwordController.clear();
}
@override
void dispose() {
// Clean up the controller when the widget is disposed.
userController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Material(
color: context.canvasColor,
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
children: [
Image.asset("assets/images/home.png", fit: BoxFit.cover),
SizedBox(
height: 20.0,
),
Text(
"Welcome $name!",
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
),
).p20(),
SizedBox(
height: 20.0,
),
Padding(
padding: EdgeInsets.symmetric(vertical: 16.0, horizontal: 32.0),
child: Column(
children: [
TextFormField(
//***CONTROLLER***
controller: userController,
decoration: InputDecoration(
hintText: "Enter the Username",
labelText: "Username"),
//check username==null
validator: (value) {
if (value.isEmpty) {
return "Username cannot be empty!";
}
return null;
},
onChanged: (value) {
name = value;
setState(() {});
},
),
TextFormField(
//Password controller */
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
hintText: "Enter the Password",
labelText: "Password"),
validator: (value) {
if (value.isEmpty) {
return "Password cannot be empty!";
} else if (value.length < 6) {
return "Password length must be of length 6";
}
return null;
},
),
],
),
),
SizedBox(
height: 30.0,
),
Material(
child: InkWell(
onTap: () => movetoHome(context),
child: AnimatedContainer(
width: changeButton ? 50 : 150.0,
height: 50.0,
alignment: Alignment.center,
duration: Duration(milliseconds: 1000),
child: changeButton
? Icon(
Icons.done_rounded,
color: Colors.white,
size: 30.0,
)
: Text(
"Login",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white),
),
decoration: BoxDecoration(
color: context.theme.buttonColor,
borderRadius:
BorderRadius.circular(changeButton ? 50 : 8),
),
),
),
),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment