Flutter Simple Login Page - flutter

I am a flutter beginner. How to make a simple login page in flutter like this. I tried, but I get a lot of errors. Can anyone help to resolve this?
Please see here to find what I want
Thanks in advance!...............
My code:
Container(
width: 350,
child: TextField(
decoration: InputDecoration(
label Text: 'Email',
),
),
),
Container(
width: 350,
child: TextField(
obscureText: true,
decoration: InputDecoration(
label Text: 'Password',
suffixIcon: Icon(CupertinoIcons.eye_slash_fill,
size: 17),
),
),
),
Padding(
padding: EdgeInserts.fromLTRB(20,20,40,40),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text('Forget Password')
],
),
),
GestureDetector(
child: Container(
alignment: Alignment.center,
width: 250,
child: TextField(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
),
child: Padding(
padding: EdgeInsets.all(top: 12),
child: Text('LOGIN')
),
),
),
),
],
),
),
),
);
}
}

import 'package:flutter/material.dart';
class LoginScreen extends StatefulWidget {
#override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _formKey = GlobalKey<FormState>();
String _email, _password;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login Screen'),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
validator: (input) {
if (input.isEmpty) {
return 'Please enter an email';
}
return null;
},
onSaved: (input) => _email = input,
decoration: InputDecoration(
labelText: 'Email',
),
),
TextFormField(
validator: (input) {
if (input.length < 6) {
return 'Your password needs to be at least 6 characters';
}
return null;
},
onSaved: (input) => _password = input,
decoration: InputDecoration(
labelText: 'Password',
),
obscureText: true,
),
RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
// This is where you can handle the login logic
}
},
child: Text('Submit'),
),
],
),
),
),
);
}
}
This is sample code of Login screen.
For get more design and code head to https://flutterawesome.com/tag/login-screen
there is lot of sample code available. And I think you can get better idea from it.

Related

TextFormField Validation Not Working Correctly

I am new to Flutter development, and I'm currently working on the sign in screen for my first full application. I apologize if this has already been asked, but I cannot figure out where my code is going wrong. I'm attempting to test basic form validation, just checking whether anything has been written into the form fields before I continue.
The code for my form is as follows:
class SignInForm extends StatefulWidget {
const SignInForm({super.key});
#override
SignInFormState createState() {
return SignInFormState();
}
}
class SignInFormState extends State<SignInForm> {
final _signInFormKey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Form(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Username',
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Please enter a valid username';
}
return null;
},
),
Padding(
padding: EdgeInsets.only(top: 20),
child: TextFormField(
obscureText: true,
enableSuggestions: false,
autocorrect: false,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Password',
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Please enter a valid password';
}
return null;
},
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Color(0xFF0b2240)),
),
onPressed: () {
if (_signInFormKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
}
},
child: const Text('Log In'),
),
],
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Color(0xFF0b2240)),
),
onPressed: () {
if (_signInFormKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
}
},
child: const Text('+ Sign Up'),
),
],
),
),
],
),
);
}
}
Thanks in advance for any help improving my code!
Your forgot the key for the form widget, that's why validation don't work.
#override
Widget build(BuildContext context) {
return Form(
key: _signInFormKey,
child: Column(
...
More infos here : https://docs.flutter.dev/cookbook/forms/validation

Add new text form field when button pressed?

I have screen which have username field,
Here, When i press the submit button add new TextFormField below the UserName field so how i can do this?
Code as Below.
return Container(
child: Form(
key: _formKey,
child: Column(
children: [
Container(
padding: const EdgeInsets.all(10),
child: Text("Reset Password"),
),
Container(
child: Center(
child: Expanded(
child: Text(
"Enter your username below to recieve password reset instruction",
textAlign: TextAlign.center,
maxLines: 2,
),
),
),
),
TextFormField(
controller: userNameController,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: "User Name",
),
focusNode: fnField1,
validator: (value) {
if (value?.isEmpty == true) {
return AppLocalizations.of(context)!.valEnterUserName;
}
return null;
},
),
Container(
constraints: const BoxConstraints(minWidth: double.infinity),
child: ElevatedButton(
onPressed: () async {},
child: Text("Submit"),
),
),
],
),
),
);
Please help me that how i can do this.
In such a case we can use a StatefulWidget to set a _displayNewTextField boolean to true once the button is pressed:
bool _displayNewTextField = false;
...
Container(
constraints: const BoxConstraints(minWidth: double.infinity),
child: ElevatedButton(
onPressed: () async {
setState(() {
_displayNewTextField = true;
});
},
child: const Text("Submit"),
),
),
And use a Visibility Widget to show the new TextField accordingly:
Visibility(
visible: _displayNewTextField,
child: TextFormField(
controller: newTextFieldController,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.text,
decoration: const InputDecoration(
labelText: "User Name",
),
),
),
A full example is reported below:
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _displayNewTextField = false;
TextEditingController newTextFieldController = TextEditingController();
TextEditingController userNameController = TextEditingController();
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Form(
child: Column(
children: [
Container(
padding: const EdgeInsets.all(10),
child: const Text("Reset Password"),
),
const Center(
child: Expanded(
child: Text(
"Enter your username below to recieve password reset instruction",
textAlign: TextAlign.center,
maxLines: 2,
),
),
),
TextFormField(
controller: userNameController,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.text,
decoration: const InputDecoration(
labelText: "User Name",
),
),
Visibility(
visible: _displayNewTextField,
child: TextFormField(
controller: newTextFieldController,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.text,
decoration: const InputDecoration(
labelText: "User Name",
),
),
),
Container(
constraints: const BoxConstraints(minWidth: double.infinity),
child: ElevatedButton(
onPressed: () async {
setState(() {
_displayNewTextField = true;
});
},
child: const Text("Submit"),
),
),
],
),
),
),
);
}
}

how to get save the value from textfromfield and pass it back to page?

import 'package:flutter/material.dart';
import 'package:mmitra/widgets/header.dart';
import 'home.dart';
class CreateAccount extends StatefulWidget {
#override
_CreateAccountState createState() => _CreateAccountState();
}
class _CreateAccountState extends State<CreateAccount> {
late String username;
final _key = Global Key<FormState>();
#override
Widget build(BuildContext parentContext) {
return Scaffold(
appBar: header(context, titleText: 'Create Account'),
body: ListView(children: [
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.only(top: 25),
child: Center(
child: Text(
'Create a username',
style: TextStyle(fontSize: 25.0),
),
),
),
Padding(
padding: EdgeInsets.all(16.0),
child: Form(
child: TextFormField(
key: _key,
// controller: myController,
onSaved: (val) => username = val!,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelStyle: TextStyle(fontSize: 15),
labelText: 'Username',
hintText: 'Must be at least 3 Characters'),
),
),
),
GestureDetector(
onTap: () {
_key.currentState!.save();
Navigator.pop(context, username
);
},
child: Container(
height: 50,
width: 300,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(7)),
child: Center(
child: Text(
'Submit',
style:
TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
),
),
),
],
),
),
]),
);
}
}
and I'm getting below shown errors:
Exception caught by gesture
The following Cast Error was thrown while handling a gesture
Null check operator used on a null value
I just want to get the textfromfield value and i want to pass it to the home.dart page in order to create the document in firebase collection
you must define a EditTextControll() ex:textController and set it to TextFormField to controller paramerter , and then get text as textController.text and pass with Navigatoer .
for pass wihtin first screen to two screen
Firstly /
TextEditingController controller = TextEditingController();
Secoundly /
body: Form(
child: TextFormField(
controller: controller,
onTap: (){
//here SettingsScreen() is example
// name is paramerter in the secound screen
Navigator.push(context,
MaterialPageRoute(builder: (context)=>SettingsScreen(name:controller.text),),
);
},
),
),
You added key in TextFormField
Form(
child: TextFormField(
key: _key,
// controller: myController,
onSaved: (val) => username = val!,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelStyle: TextStyle(fontSize: 15),
labelText: 'Username',
hintText: 'Must be at least 3 Characters'),
),
),
),
Remove it from TextFormField and Add it in Form
Just Like:
Form(
key: _key,
child: TextFormField(
// controller: myController,
onSaved: (val) => username = val!,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelStyle: TextStyle(fontSize: 15),
labelText: 'Username',
hintText: 'Must be at least 3 Characters'),
),
),
),
This will Solve your Issue.

How to validate formfield

Am a newbie though. I followed a tutorial for my code below which is a register form. How can i validate each input field since it's just one widget which is reused for the entire input field.
............................................................................................................................................................
Code
import 'package:flutter/material.dart';
import 'package:flutterlogindesign/utils/color.dart';
import 'package:flutterlogindesign/widgets/btn_widget.dart';
import 'package:flutterlogindesign/widgets/herder_container.dart';
class RegPage extends StatefulWidget {
#override
_RegPageState createState() => _RegPageState();
}
class _RegPageState extends State<RegPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.only(bottom: 30),
child: Column(
children: <Widget>[
HeaderContainer("Register"),
Expanded(
flex: 1,
child: Container(
margin: EdgeInsets.only(left: 20, right: 20, top: 30),
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
_textInput(hint: "Fullname", icon: Icons.person),
_textInput(hint: "Email", icon: Icons.email),
_textInput(hint: "Phone Number", icon: Icons.call),
_textInput(hint: "Password", icon: Icons.vpn_key),
Expanded(
child: Center(
child: ButtonWidget(
btnText: "REGISTER",
onClick: () {
Navigator.pop(context);
},
),
),
),
RichText(
text: TextSpan(children: [
TextSpan(
text: "Already a member ? ",
style: TextStyle(color: Colors.black)),
TextSpan(
text: "Login",
style: TextStyle(color: orangeColors)),
]),
)
],
),
),
)
],
),
),
);
}
Widget _textInput({controller, hint, icon}) {
return SingleChildScrollView(
child: Container(
margin: EdgeInsets.only(top: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.white,
),
padding: EdgeInsets.only(left: 10),
child: TextFormField(
controller: controller,
decoration: InputDecoration(
border: InputBorder.none,
hintText: hint,
prefixIcon: Icon(icon),
),
),
),
);
}
}
Use Form Widget like the following
final _formKey = GlobalKey<FormState>();
bool autoValidate = false;
String phone;
child:Form( key: _formKey,
autovalidateMode: autoValidate ? AutovalidateMode.always : AutovalidateMode.disabled,
child: Column(
children: [
AppTextFormField(
initialValue: phone,
onChanged: (value) {
phone = value;
},
validator: (value) {
if (value.isEmpty) {
return 'Enter Phone number';
}
},
),
PrimaryButton(
text: 'Go',
onTap: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
} else {
autoValidate = true;
}
},
),
],
),
),

How to make wrap content for ListView

I need to make dialog with form input fields and when i use with column content is fit to screen but when i try to type values it hiding the below input fields and submit button.Show to solve this issue i only know one soluction that replacing column with listview and works it allow me to scrool but the content is not fit to screen. How to make the content in listview fit to sceen?
Here is my full code
import 'package:devaayanam/Confi.dart';
import 'package:flutter/material.dart';
class BookPujaDialogContent extends StatefulWidget {
BookPujaDialogContent({Key key}) : super(key: key);
#override
_BookPujaDialogContentState createState() => _BookPujaDialogContentState();
}
class _BookPujaDialogContentState extends State<BookPujaDialogContent> {
final _formKey =GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Container(
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: Confi.TEMPLENAME
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter '+Confi.TEMPLENAME;
}
return null;
},
),
TextFormField(
decoration: InputDecoration(
labelText: Confi.DEITY
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter '+Confi.DEITY;
}
return null;
},
),
TextFormField(
decoration: InputDecoration(
labelText: Confi.BN
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter '+Confi.BN;
}
return null;
},
),
TextFormField(
decoration: InputDecoration(
labelText: Confi.STAR
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter '+Confi.STAR;
}
return null;
},
),
Container(
margin: EdgeInsets.only(top: 20),
child: RaisedButton(
onPressed: () {
// Validate returns true if the form is valid, or false
// otherwise.
if (_formKey.currentState.validate()) {
// If the form is valid, display a Snackbar.
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
}
},
textColor: Colors.white,
padding: const EdgeInsets.all(0.0),
shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
child: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: <Color>[
Color(0xFF0D47A1),
Color(0xFF1976D2),
Color(0xFF42A5F5),
],
),
borderRadius: BorderRadius.all(Radius.circular(10.0))
),
width: MediaQuery.of(context).size.width,
height: 40,
// padding: const EdgeInsets.fromLTRB(45, 10, 45, 10),
child: Center(
child: Text(
Confi.BOOKNOW,
style: TextStyle(fontSize: 16)
),
),
),
),
)
],
),
),
);
}
}
Set shirinkWrap: true in ListView.
Dialog(
child: Form(
key: _formKey,
child: ListView(
shrinkWrap: true, //TODO: Use this
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: Confi.TEMPLENAME),
validator: (value) {
if (value.isEmpty) {
return 'Please enter ' + Confi.TEMPLENAME;
}
return null;
},
),
....
....