Skip to content

Instantly share code, notes, and snippets.

@ankesh-kumar
Created May 14, 2020 09:28
Show Gist options
  • Save ankesh-kumar/07b6f78e2200d2dd9a8a23b9f97a1901 to your computer and use it in GitHub Desktop.
Save ankesh-kumar/07b6f78e2200d2dd9a8a23b9f97a1901 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../hexColor.dart';
class FormValidation extends StatefulWidget{
FormValidation({Key key,this.title}) : super(key: key);
final String title;
@override
_FormValidationState createState() => _FormValidationState();
}
class _FormValidationState extends State<FormValidation> with SingleTickerProviderStateMixin{
bool isLoading=true;
bool isSubmitLoading=false;
bool _isPressed = false;
int _state = 0;
double _width = double.infinity;
Animation _animation;
final _formKeys= GlobalKey<FormState>();
bool _autoValidate = false;
List<DropdownMenuItem<String>> dropdownItem=[];
var userNameController = new TextEditingController();
var addressController = new TextEditingController();
var mobileNumberController = new TextEditingController();
var response_data="";
initState() {
super.initState();
}
dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(
appBar: AppBar(
backgroundColor: HexColor('#79c14e'),
leading: new IconButton(icon: new Icon(Icons.arrow_back),),
title: Text('Form Validation'),
),
body:
SingleChildScrollView(
child: Column(
children: <Widget>[
Card(
child: Form(
key: _formKeys,
autovalidate: _autoValidate,
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('Create User',
style: TextStyle(fontSize: 20),),
],
),
),
Divider( color: Colors.black,thickness: 2, indent: 0,
endIndent: 0,),
Container(
margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
padding: EdgeInsets.all(10),
child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('User Name :',style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold)),
TextFormField(validator: validateInputField, keyboardType: TextInputType.text,maxLength: 30,
controller: userNameController,
),
],
)
,
),
Container(
margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
padding: EdgeInsets.all(10),
child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Address :',style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold)),
TextFormField(validator: validateInputField, keyboardType: TextInputType.text,maxLength: 30,
controller: addressController,
),
],
)
,
),
Container(
margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
padding: EdgeInsets.all(10),
child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Mobile Number :',style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold)),
TextFormField(validator: validateInputField,
keyboardType: TextInputType.number,
maxLength: 10,
controller: mobileNumberController,
),
],
)
,
),
Container(
margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
padding: EdgeInsets.all(10),
child:
Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
(isSubmitLoading==true)? CircularProgressIndicator():
RaisedButton(
child: buildSignButtonChild(),
onPressed: (){},
onHighlightChanged: (isPressed)
{
setState(() {
_isPressed=isPressed;
if(_state==0){
animateButton();
}
});
},
color: HexColor('#79c14e'),textColor: HexColor('#FFFFFF'),),
],
)
,
),
],
),
),
),
],
),
)
),
);
}
void animateButton() {
double initialWidth = 100.0;
var controller =
AnimationController(duration: Duration(milliseconds: 300), vsync: this);
_animation = Tween(begin: 0.0, end: 1.0).animate(controller)
..addListener(() {
setState(() {
_width = initialWidth - ((initialWidth - 48.0) * _animation.value);
});
});
controller.forward();
setState(() {
_state = 1;
submitData();
});
}
Widget buildSignButtonChild(){
if(_state==0){
return Text('Submit');
}
else if(_state==1){
return SizedBox(
height: 36.0,
width: 36.0,
child: CircularProgressIndicator(
value: null,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white)));
}
else
{
return Icon(Icons.check, color: Colors.white);
}
}
void submitData(){
if(_formKeys.currentState.validate()){
isSubmitLoading=true;
isSubmitLoading=true;
/**
* submit your data
*/
}
else
{
setState(() {
_isPressed=false;
_state = 0;
_autoValidate = true;
});
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Must enter value for all required fields')));
}
}
String validateInputField(String value) {
if (value.isEmpty)
return 'must enter value';
else
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment