How to add validation in dynamic textformfield - flutter

I want to add validation in dynamic textformfield....i have added validation as per my knowledge but its not working properly.....when i created 4-5 textformfield and erase data from previous field its doesn't show any error when i click on submit button......validation is working properly only for currrent textformfield .....its not working properly for previous textformfield ....how can i slove it???
class PurchaseTicket extends StatefulWidget {
#override
int eventID;
PurchaseTicket(this.eventID);
_PurchaseTicketState createState() => _PurchaseTicketState();
}
class _PurchaseTicketState extends State<PurchaseTicket> {
// List<GlobalKey<FormState>> _formKeys = [GlobalKey<FormState>()];
//final _formKey = GlobalKey<FormState>();
List<TextEditingController> _controllers = new List();
List<TextEditingController> _controllers1 = new List();
List<FocusNode> _FocusNode1 = new List();
List<FocusNode> _FocusNode2 = new List();
FocusNode myFocusNode1 = new FocusNode();
FocusNode myFocusNode2 = new FocusNode();
TextEditingController mobileController = TextEditingController();
TextEditingController nameController = TextEditingController();
ScrollController controller = ScrollController();
var _count = 0;
List<Map<String, dynamic>> _values;
_register(BuildContext context) async {
bool validname;
bool validmobile;
for (var i = 0; i <= _controllers.length - 1; i++) {
print(_controllers[i].text.runtimeType);
if (_controllers[i].text.trim() == "" || _controllers[i].text == null) {
validname = false;
} else {
validname = true;
}
}
for (var i = 0; i <= _controllers1.length - 1; i++) {
if (_controllers1[i].text.trim() == '') {
validmobile = false;
} else {
validmobile = true;
}
}
if (nameController.text.trim() == '' || nameController.text.length < 1) {
showToast('Please Enter Name', context: context);
} else if (mobileController.text.trim() == '' ||
mobileController.text.length < 10) {
showToast('Enter Valid Mobile Number', context: context);
} else if (validname == false) {
showToast('Please Enter Name', context: context);
} else if (validmobile == false) {
showToast('Enter Valid Mobile Number', context: context);
} else {
print(
"$validmobile $validname ${nameController.text} ${mobileController.text}");
participateRegister(nameController.text, mobileController.text,
_controllers, _controllers1, widget.eventID, context);
}
}
// else if (mobileController.text.isNotEmpty &&
// nameController.text.isNotEmpty) {
// for (int i = 0; i < _controllers.length; i++) {
// if (validmobile = false) {
// showToast('Please Enter Name', context: context);
// }
// }
// } else if (mobileController.text.isNotEmpty &&
// nameController.text.isNotEmpty) {
// for (int i = 0; i < _controllers1.length; i++) {
// if (validmobile == false) {
// showToast('Enter Valid Mobile Number', context: context);
// }
// }
// }
// }
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFFfbf0d4),
body: SingleChildScrollView(
child: Stack(children: [
Container(
height: MediaQuery.of(context).size.height * .50,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
image: DecorationImage(
//image: NetworkImage(widget.eventData.splitImage[0]),
image: ExactAssetImage("assets/images/Group 4.png"),
fit: BoxFit.cover,
),
),
),
Container(
margin: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.29,
),
height: MediaQuery.of(context).size.height * .7,
width: MediaQuery.of(context).size.width,
decoration: new BoxDecoration(
image: new DecorationImage(
image: ExactAssetImage("assets/images/body shape.png"),
fit: BoxFit.fill,
),
),
),
Container(
margin: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.03,
),
child: InkWell(
child: new IconButton(
icon: new Icon(
Icons.arrow_back,
size: 20,
),
color: Colors.white,
onPressed: () => {Navigator.pop(context)}),
),
),
Padding(
padding: const EdgeInsets.only(
left: 30,
right: 30,
),
child: Container(
margin: EdgeInsets.only(
top: MediaQuery.of(context).size.width * .5),
child: Column(
children: [
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
elevation: 10,
child: Container(
//color: Colors.white,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(
20.0) // <--- border radius here
),
),
child: Column(
children: [
SizedBox(
//height: 10,
),
Center(
child: Container(
width: MediaQuery.of(context).size.width * .71,
height:
MediaQuery.of(context).size.height * .085,
child: new TextFormField(
controller: nameController,
focusNode: myFocusNode1,
style: TextStyle(
height: 1.5,
fontWeight: FontWeight.w500,
color: const Color(0xFFa5a5a5),
fontSize: 12),
cursorColor: const Color(0xFFa5a5a5),
keyboardType: TextInputType.text,
decoration: InputDecoration(
fillColor: Colors.white,
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFFa5a5a5))),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xFFa5a5a5)),
),
border: UnderlineInputBorder(),
labelText: 'Name',
labelStyle: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w500,
color: myFocusNode1.hasFocus
? const Color(0xFFa5a5a5)
: const Color(0xFFa5a5a5))),
),
),
),
SizedBox(
// height: MediaQuery.of(context).size.height * .013,
),
Container(
width: MediaQuery.of(context).size.width * .71,
height: MediaQuery.of(context).size.height * .085,
child: new TextFormField(
maxLength: 10,
controller: mobileController,
focusNode: myFocusNode2,
style: TextStyle(
height: 1.5,
fontWeight: FontWeight.w500,
color: const Color(0xFFa5a5a5),
fontSize: 12),
cursorColor: const Color(0xFFa5a5a5),
keyboardType: TextInputType.number,
decoration: InputDecoration(
counterText: '',
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFFa5a5a5))),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFFa5a5a5)),
),
fillColor: Colors.white,
border: UnderlineInputBorder(),
labelText: 'Mobile No',
labelStyle: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w500,
color: myFocusNode2.hasFocus
? const Color(0xFFa5a5a5)
: const Color(0xFFa5a5a5))),
validator: (value) {
if (value.length < 10 && value.isEmpty) {
return "Atleast 10 digit required";
}
return null;
},
),
),
SizedBox(
height: 10,
),
],
),
),
),
ListView.builder(
physics: const BouncingScrollPhysics(),
shrinkWrap: true,
itemCount: _count,
itemBuilder: (context, index) {
print(index);
return _row(index);
},
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
child: Icon(
Icons.add,
color: Colors.red,
size: 35,
),
backgroundColor: Color(0xFF9a7210),
onPressed: () async {
_controllers.add(new TextEditingController());
_controllers1.add(new TextEditingController());
_FocusNode1.add(new FocusNode());
_FocusNode2.add(new FocusNode());
// _formKeys.add(new GlobalKey<FormState>());
setState(() {
_count++;
});
},
),
],
),
SizedBox(
height: 20,
),
Center(
child: GestureDetector(
onTap: () {
_register(context);
},
child: Container(
height: 70,
width: 260,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
const Color(0xFFb48919),
const Color(0xFF9a7210),
],
),
border: Border.all(color: Colors.white, width: 4),
borderRadius: BorderRadius.all(Radius.circular(
70.0) // <--- border radius here
),
),
child: Center(
child: Text("Submit",
style: TextStyle(
fontWeight: FontWeight.bold,
letterSpacing: 1,
fontSize: 30,
color: Colors.white)),
),
),
),
),
SizedBox(
height: 30,
)
],
),
),
),
]),
));
}
_row(int index) {
return Column(
children: [
Row(
children: [
Expanded(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
elevation: 10,
child: Container(
//color: Colors.white,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(
20.0) // <--- border radius here
),
),
child: Column(
children: [
SizedBox(
// height: 10,
),
Center(
child: Container(
width: MediaQuery.of(context).size.width * .71,
height: MediaQuery.of(context).size.height * .085,
child: new TextFormField(
autofocus: false,
controller: _controllers[index],
focusNode: _FocusNode1[index],
style: TextStyle(
height: 1.5,
fontWeight: FontWeight.w500,
color: const Color(0xFFa5a5a5),
fontSize: 12),
cursorColor: const Color(0xFFa5a5a5),
keyboardType: TextInputType.text,
decoration: InputDecoration(
fillColor: Colors.white,
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFFa5a5a5))),
focusedBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Color(0xFFa5a5a5)),
),
border: UnderlineInputBorder(),
labelText: 'Name',
labelStyle: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w500,
color: _FocusNode1[index].hasFocus
? const Color(0xFFa5a5a5)
: const Color(0xFFa5a5a5))),
),
),
),
SizedBox(
//height: MediaQuery.of(context).size.height * .013,
),
Container(
width: MediaQuery.of(context).size.width * .71,
height: MediaQuery.of(context).size.height * .085,
child: new TextFormField(
maxLength: 10,
autofocus: false,
controller: _controllers1[index],
focusNode: _FocusNode2[index],
style: TextStyle(
height: 1.5,
fontWeight: FontWeight.w500,
color: const Color(0xFFa5a5a5),
fontSize: 12),
cursorColor: const Color(0xFFa5a5a5),
keyboardType: TextInputType.number,
decoration: InputDecoration(
counterText: '',
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFFa5a5a5))),
focusedBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: const Color(0xFFa5a5a5)),
),
fillColor: Colors.white,
border: UnderlineInputBorder(),
labelText: 'Mobile No',
labelStyle: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w500,
color: _FocusNode2[index].hasFocus
? const Color(0xFFa5a5a5)
: const Color(0xFFa5a5a5))),
),
),
SizedBox(
height: 10,
),
],
),
),
),
),
],
),
SizedBox(
height: 30,
)
],
);
}
}

Use Form , Here is the Example For you:
class MyApp extends StatefulWidget {
#override
State<StatefulWidget> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
GlobalKey<FormState> formKey = new GlobalKey();
String formFieldValue;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Form(
key: formKey,
child: Column(
children: [
TextFormField(
validator: (input) {
if (input.isEmpty) {
return 'Please type something';
}
return null;
},
onSaved: (input) => formFieldValue = input,
),
RaisedButton(
onPressed: submitForm,
child: Text(
'Submit'
),
)
],
),
)
);
}
submitForm() {
final formState = formKey.currentState;
if (formState.validate()) {
formState.save();
// then do something
}
}
}

Please follow below tutorial which Flutter team suggest to use for validation.
You can just put all text fields in a form and validate the all fields anytime.
https://flutter.dev/docs/cookbook/forms/validation

Related

How do I make the Circle Avatar's location static so that it does not change while entering text into a form?

This code of Forget password and I use Stack with 2 Containers and circle Avatar above of them.
At the beginning of the program, everything is fine, but when I fill out the form. Circle avatar goes down. How do I fix that?
Is the use of margin the reason for this, but how do I put the circuit avatar in the middle?
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:google_fonts/google_fonts.dart';
import 'Login.dart';
class Restartpassword extends StatelessWidget {
const Restartpassword({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
color: Colors.deepPurple.withOpacity(0.2),
width: double.infinity,
child: Center(
child: Stack(
children: [
Container(
margin: EdgeInsets.symmetric(horizontal: 20),
height: 400,
),
Center(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
height: 300,
decoration: BoxDecoration(
border: Border.all(
width: 1,
color: Colors.grey
)
),
child: Column(
mainAxisAlignment:MainAxisAlignment.center ,
children: [
Container(
//----------------------------------------------------
decoration: BoxDecoration(
color: Colors.deepPurple.withOpacity(0.1),
border: Border(
left: BorderSide(color: Colors.deepPurple,width: 10),
),
),
//----------------------------------------------------
width: 300,
height: 50,
//----------
child:TextFormField(
style: GoogleFonts.pacifico(
color: Colors.purple,
fontSize: 20.0,
),
//----------------------------------
keyboardType:TextInputType.text ,
//----------------------------------
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
//-----------------------------------
decoration: InputDecoration(
prefixIcon: Icon(Icons.lock_open_sharp,color:Colors.purple),
border: InputBorder.none,
hintText: '***New Password***',
hintStyle: GoogleFonts.pacifico(
color: Colors.black54,
fontSize: 15,
fontWeight: FontWeight.bold
),
fillColor: Colors.black12.withOpacity(0.2),
filled: true,
//-----------------------------------
),
)),
SizedBox(
height: 10,
),
Container(
//----------------------------------------------------
decoration: BoxDecoration(
color: Colors.deepPurple.withOpacity(0.1),
border: Border(
left: BorderSide(color: Colors.deepPurple,width: 10),
),
),
//----------------------------------------------------
width: 300,
height: 50,
//----------
child:TextFormField(
style: GoogleFonts.pacifico(
color: Colors.purple,
fontSize: 20.0,
),
//----------------------------------
keyboardType:TextInputType.text ,
//----------------------------------
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
//-----------------------------------
decoration: InputDecoration(
prefixIcon: Icon(Icons.lock_outline,color:Colors.purple),
border: InputBorder.none,
hintText: '***Confirm Password***',
hintStyle: GoogleFonts.pacifico(
color: Colors.black54,
fontSize: 15,
fontWeight: FontWeight.bold
),
fillColor: Colors.black12.withOpacity(0.2),
filled: true,
//-----------------------------------
),
)),
SizedBox(
height: 10,
),
Container(
width:130 ,
height: 45,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15)
),
child: MaterialButton(
minWidth: 130,
hoverColor: Colors.purple,
color: Colors.green,
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => loginScreen())) ;
},
child: Text('Restart '
,style: GoogleFonts.pacifico(
color: Colors.white,
fontSize: 20.0,
),
)),
)
],
),
),
),
Container(
height: 80,
width: 80,
margin: EdgeInsetsDirectional.only(
start: 165,
top: 210
),
child: CircleAvatar(
backgroundColor:Colors.white.withOpacity(0.3),
radius: 30,
child: Icon(Icons.lock,size: 60,
color: Colors.purple,),
),
),
],
),
),
)
),
);
}
}
Move the Stack to the Container with the fields and then use Transform.translate to move the icon outside the border. This way the avatar circle is going to be fixed relative to the container.
Check the result (also live on DartPad)
Here's the code:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: Restartpassword(),
debugShowCheckedModeBanner: false,
);
}
}
class Restartpassword extends StatelessWidget {
const Restartpassword({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
color: Colors.deepPurple.withOpacity(0.2),
width: double.infinity,
child: Center(
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 40),
padding: const EdgeInsets.symmetric(horizontal: 20),
height: 300,
decoration:
BoxDecoration(border: Border.all(width: 1, color: Colors.grey)),
child: Stack(
children: [
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
//----------------------------------------------------
decoration: BoxDecoration(
color: Colors.deepPurple.withOpacity(0.1),
border: const Border(
left: BorderSide(
color: Colors.deepPurple, width: 10),
),
),
//----------------------------------------------------
width: 300,
height: 50,
//----------
child: TextFormField(
style: const TextStyle(
color: Colors.purple,
fontSize: 20.0,
),
//----------------------------------
keyboardType: TextInputType.text,
//----------------------------------
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
//-----------------------------------
decoration: InputDecoration(
prefixIcon: const Icon(Icons.lock_open_sharp,
color: Colors.purple),
border: InputBorder.none,
hintText: '***New Password***',
hintStyle: const TextStyle(
color: Colors.black54,
fontSize: 15,
fontWeight: FontWeight.bold),
fillColor: Colors.black12.withOpacity(0.2),
filled: true,
//-----------------------------------
),
)),
const SizedBox(
height: 10,
),
Container(
//----------------------------------------------------
decoration: BoxDecoration(
color: Colors.deepPurple.withOpacity(0.1),
border: const Border(
left: BorderSide(
color: Colors.deepPurple, width: 10),
),
),
//----------------------------------------------------
width: 300,
height: 50,
//----------
child: TextFormField(
style: const TextStyle(
color: Colors.purple,
fontSize: 20.0,
),
//----------------------------------
keyboardType: TextInputType.text,
//----------------------------------
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
//-----------------------------------
decoration: InputDecoration(
prefixIcon: const Icon(Icons.lock_outline,
color: Colors.purple),
border: InputBorder.none,
hintText: '***Confirm Password***',
hintStyle: const TextStyle(
color: Colors.black54,
fontSize: 15,
fontWeight: FontWeight.bold),
fillColor: Colors.black12.withOpacity(0.2),
filled: true,
//-----------------------------------
),
)),
const SizedBox(
height: 10,
),
Container(
width: 130,
height: 45,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15)),
child: MaterialButton(
minWidth: 130,
hoverColor: Colors.purple,
color: Colors.green,
onPressed: () {
// Navigator.push(context, MaterialPageRoute(builder: (context) => loginScreen())) ;
},
child: const Text(
'Restart ',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
)),
)
],
),
),
Transform.translate(
offset: const Offset(0, -40),
child: Align(
alignment: Alignment.topCenter,
child: SizedBox(
height: 80,
width: 80,
// margin: EdgeInsetsDirectional.only(start: 165, top: 210),
child: CircleAvatar(
backgroundColor: Colors.white.withOpacity(0.3),
radius: 30,
child: const Icon(
Icons.lock,
size: 60,
color: Colors.purple,
),
),
),
),
),
],
),
),
),
)),
);
}
}

The key [GlobalObjectKey int#a1467] was used by multiple widgets

I have project that have very long sheet code on each page without Bloc or other code separation.
I have this code, that must open dialog with location peeker in one page:
showDialog(
context: context,
builder: (BuildContext context) {
return LocationPickerDialog(_address);
}).then....
After clicking, a bug occurs. In project no GlobalKeys.In other page this code don't bug.
The key [GlobalObjectKey int#a1467] was used by multiple widgets. The parents of those widgets were different widgets that both had the following description:
Builder(dependencies: [MediaQuery])
A GlobalKey can only be specified on one widget at a time in the widget tree.
how to understand why this is happening?
What widget is causing this?
UPDATE:
class LocationPickerDialog extends StatefulWidget {
var retrieveAddress;
LocationPickerDialog(this.retrieveAddress);
#override
_LocationPickerDialogState createState() =>
new _LocationPickerDialogState(retrieveAddress);
}
class _LocationPickerDialogState extends State<LocationPickerDialog> {
var retrieveAddress;
_LocationPickerDialogState(this.retrieveAddress);
final searchLinkHolder = TextEditingController();
final addressHolder = TextEditingController();
var searchLink;
var _address;
var Address = "";
List<String> placeList = [];
var Locationlist = <beanLocation>[];
#override
void initState() {
super.initState();
}
getSuggestions(String query) {
if (query.isNotEmpty) {
API.getPlaceSearchAPI(query).then((response) {
setState(() {
Iterable list = json.decode(response.body)['features'];
Locationlist =
list.map((model) => beanLocation.fromJson(model)).toList();
});
});
setState(() {
placeList.clear();
for (int i = 0; i < Locationlist.length; i++) {
Constant_Class.printMessage(
"Locationlist => $i" + Locationlist[i].name.toString());
placeList.add(Locationlist[i].name);
}
});
} else {
Constant_Class.printMessage("Locationlist => 123");
}
}
#override
Widget build(BuildContext context) {
return ScreenUtilInit(
// designSize: Constant_Class.screenSize(context),
builder: (_) => Scaffold(
backgroundColor: Colors.transparent,
resizeToAvoidBottomInset: false,
body: Stack(
children: [
GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Container(
color: Colors.black.withOpacity(0.5),
),
),
Container(
margin: EdgeInsets.only(right: 20, left: 20, bottom: 20, top: 20),
child: Container(
decoration: BoxDecoration(
borderRadius: new BorderRadius.all(const Radius.circular(30)),
color: Colors.white,
),
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Align(
alignment: Alignment.topRight,
child: InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
margin:
EdgeInsets.only(right: 10, top: 5, bottom: 5),
height: 35,
width: 35,
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: ColorsApp.colorSelectedTab),
child: Center(
child: Icon(
Icons.close_rounded,
color: Colors.white,
),
)),
),
),
Container(
margin: EdgeInsets.all(5),
padding: EdgeInsets.all(12),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius:
new BorderRadius.all(const Radius.circular(30)),
border: Border.all(color: Colors.black, width: 1.0)),
child: TextFormField(
controller: searchLinkHolder,
textAlignVertical: TextAlignVertical.center,
textInputAction: TextInputAction.done,
cursorColor: Colors.black,
style: TextStyle(
fontFamily: Constant_Class.appFontFamily,
color: Colors.black),
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.only(top: 3),
filled: false,
//fillColor: Theme.of(context).cardColor,
labelStyle: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.black),
hintStyle: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.grey),
hintText: 'google_meet'.tr()),
keyboardType: TextInputType.text,
onSaved: (val) {
searchLink = val;
},
onChanged: (val) {
searchLink = val;
},
onFieldSubmitted: (value) {
// if(value != null && value.trim() != ""){
// Navigator.of(context).pop(value);
// }
setState(() {
searchLink = value;
});
if (value != null && Uri.parse(value).host != "") {
Constant_Class.locationLatitude = "";
Constant_Class.locationLongitude = "";
Constant_Class.printMessage(value);
Navigator.of(context).pop(value);
} else {
Constant_Class.ToastMessage(
'Please enter valid url');
}
},
),
),
SizedBox(
height: 10,
),
_textWithRightLeftLine('or'.tr()),
SizedBox(
height: 10,
),
Container(
margin: EdgeInsets.all(5),
padding: EdgeInsets.all(12),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius:
new BorderRadius.all(const Radius.circular(30)),
border: Border.all(color: Colors.black, width: 1.0)),
child: TextFormField(
controller: addressHolder,
textAlignVertical: TextAlignVertical.center,
textInputAction: TextInputAction.done,
cursorColor: Colors.black,
style: TextStyle(
fontFamily: Constant_Class.appFontFamily,
color: Colors.black),
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.only(top: 3),
filled: false,
//fillColor: Theme.of(context).cardColor,
labelStyle: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.black),
hintStyle: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.grey),
hintText: 'search_location'.tr()),
keyboardType: TextInputType.text,
onSaved: (val) {
_address = val;
getSuggestions(val.toString());
},
onChanged: (val) {
_address = val;
getSuggestions(val.toString());
},
onFieldSubmitted: (value) {
Constant_Class.locationLatitude = "";
Constant_Class.locationLongitude = "";
setState(() {
_address = value;
});
if (searchLink != null &&
Uri.parse(searchLink).host != "") {
Constant_Class.printMessage(searchLink);
Navigator.of(context).pop(searchLink);
} else {
Navigator.of(context).pop(_address);
}
},
),
),
Expanded(
child: ListView.separated(
physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
shrinkWrap: true,
itemCount: placeList.length,
separatorBuilder: (BuildContext context, int index) =>
Divider(),
itemBuilder: (BuildContext context, int index) {
return ListTile(
dense: true,
title: InkWell(
onTap: () {
Constant_Class.locationLatitude =
Locationlist[index].latitude;
Constant_Class.locationLongitude =
Locationlist[index].longitude;
Navigator.of(context)
.pop(placeList[index].toString());
},
// child: Text(placeList[index].toString()),
child: Text(
placeList[index].toString(),
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontWeight: FontWeight.w500,
color: Colors.black,
fontSize: 16),
),
),
);
},
),
),
],
),
),
),
],
),
),
);
}
Widget _textWithRightLeftLine(var title) {
return Container(
child: Column(
children: [
Row(
children: [
Expanded(
child: new Container(
margin: const EdgeInsets.only(left: 0.0, right: 10.0),
child: Container(
color: ColorsApp.colorTextLight,
height: 1,
)),
),
Text(
title,
style: TextStyle(
color: ColorsApp.colorTextDark,
fontFamily: Constant_Class.appFontFamily),
),
Expanded(
child: new Container(
margin: const EdgeInsets.only(left: 10.0, right: 0.0),
child: Container(
color: ColorsApp.colorTextLight,
height: 1,
)),
),
],
),
],
),
);
}
}
https://drive.google.com/file/d/1KRidmPbZJeobB6FCG2TZmn1ENFvkAojj/view?usp=sharing
I found what it was. in this screen and in the dialog root widget was ScreenUtilInit from the flutter_screenutil package. I deleted this from one page and the problem was solved.

mutation graphql using flutter forms not working

trying to use mutation on forms but got this error while addins runmutation and queryresult result in builder ().all i get is dead code in vscode
The argument type 'Widget Function(MultiSourceResult Function(Map<String, dynamic>, {Object? optimisticResult}), QueryResult<Object?>)' can't be assigned to the parameter type 'Widget Function(MultiSourceResult<Object?> Function(Map<String, dynamic>, {Object? optimisticResult}), QueryResult
import 'dart:io';
import 'package:country_code_picker/country_code_picker.dart';
import 'package:project/API/Api.dart';
import 'package:project/screens/form_info.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import '../API/querys.dart';
import '../API/mutations.dart';
class Sign_Number extends StatefulWidget {
Sign_Number({Key? key}) : super(key: key);
#override
State<Sign_Number> createState() => _Sign_NumberState();
}
class _Sign_NumberState extends State<Sign_Number> {
var PhoneNumber = "";
void _onCountryChange(CountryCode countryCode) {
this.PhoneNumber = countryCode.toString();
print("new country selected: " + countryCode.toString());
}
TextEditingController phoneNumberhere = TextEditingController();
TextEditingController password = TextEditingController();
void initState() {
super.initState();
_onCountryChange(CountryCode());
this.PhoneNumber = "+20";
}
var _formKey1 = new GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return GraphQLProvider(
client: client,
child: MaterialApp(
home: Container(
height: double.infinity,
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
stops: [0.5, 0.5],
colors: <Color>[Color.fromRGBO(0, 37, 43, 1), Colors.white],
tileMode:
TileMode.clamp, // clamp the gradient over the canvas
),
),
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.transparent,
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Container(Image(
// height: MediaQuery.of(context).size.height *0.5,
// width: 250,
// image: AssetImage('assets/images/2.jpg')),
// ),
Container(
height: 300,
width: 300,
child: const Image(
image: AssetImage(
'assets/images/2.jpg',
),
fit: BoxFit.cover,
),
),
const SizedBox(
//Use of SizedBox
height: 30,
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Row(
children: [
const SizedBox(
width: 47,
),
const Text("Enter mobile number for login",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold)),
],
)),
const SizedBox(
//Use of SizedBox
height: 20,
),
Mutation(
options: new MutationOptions(
document: gql(insertUser()),
/// Tell the GraphQL client to fetch the data from
/// the network only and don't cache it
fetchPolicy: FetchPolicy.noCache,
/// Whenever the [Form] closes, this tells the previous [route]
/// whether it needs to rebuild itself or not
onCompleted: (data) =>
Navigator.pop(context, data != null),
),
builder: (QueryResult, RunMutation) {
return Form(
key: _formKey1,
child: Container(
height: 100,
padding: const EdgeInsets.symmetric(
horizontal: 30),
child: Column(children: [
TextFormField(
controller: phoneNumberhere,
autocorrect: true,
keyboardType:
TextInputType.number,
onFieldSubmitted: (value) {},
validator: (value) {
if (value!.isEmpty) {
return 'Enter a valid Phone Number!';
}
return null;
},
decoration: InputDecoration(
prefixIcon: CountryCodePicker(
onChanged: _onCountryChange,
// Initial selection and favorite can be one of code ('IT') OR dial_code('+39')
initialSelection: '+20',
favorite: ['+20', 'EG'],
textStyle: const TextStyle(
color: Colors.black),
showFlag: true,
),
contentPadding:
const EdgeInsets.all(10),
hintText:
'Enter valid phone number...',
hintStyle: const TextStyle(
color: Colors.black),
filled: true,
fillColor: const Color.fromRGBO(
227, 227, 226, 0.2),
enabledBorder:
const OutlineInputBorder(
borderRadius:
BorderRadius.all(
Radius.circular(
12.0)),
borderSide: BorderSide(
color: Colors.white,
width: 2),
),
focusedBorder:
const OutlineInputBorder(
borderRadius:
BorderRadius.all(
Radius.circular(
10.0)),
borderSide: BorderSide(
color: Colors.white),
),
),
),
const SizedBox(
//Use of SizedBox
height: 10,
),
SizedBox(
height: 50,
child: TextFormField(
autofocus: false,
obscureText: true,
controller: password,
autocorrect: true,
keyboardType: TextInputType
.visiblePassword,
onFieldSubmitted: (value) {},
validator: (value) {
if (value!.isEmpty) {
return 'Enter a valid password!';
}
return null;
},
decoration: InputDecoration(
contentPadding:
const EdgeInsets.all(10),
hintText: 'Enter Password.',
hintStyle: const TextStyle(
color: Colors.black),
filled: true,
fillColor:
const Color.fromRGBO(
227, 227, 226, 0.2),
enabledBorder:
const OutlineInputBorder(
borderRadius:
BorderRadius.all(
Radius.circular(
12.0)),
borderSide: BorderSide(
color: Colors.white,
width: 2),
),
focusedBorder:
const OutlineInputBorder(
borderRadius:
BorderRadius.all(
Radius.circular(
10.0)),
borderSide: BorderSide(
color: Colors.white),
),
),
),
),
const SizedBox(
//Use of SizedBox
height: 5,
),
const Text(
"forgot your password?",
style: TextStyle(
color: Color.fromRGBO(
218, 218, 218, 1),
fontSize: 10),
textAlign: TextAlign.left,
),
])));
const SizedBox(
//Use of SizedBox
height: 50,
);
FlatButton(
height: 50,
minWidth: 300,
onPressed: () {
if (_formKey1.currentState!.validate()) {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) =>
MyCustomForm()));
// If the form is valid, display a snackbar. In the real world,
// you'd often call a server or save the information in a database.
ScaffoldMessenger.of(context)
.showSnackBar(
SnackBar(
content:
Text(phoneNumberhere.text)),
);
}
},
color: const Color.fromRGBO(0, 168, 165, 1),
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(10)),
child: const Text(
"Continue",
style: TextStyle(
color: Colors.white, fontSize: 19),
),
);
},
),
],
)
])))));
}
}```

how to make layout center vertical in flutter like in android

Like in android for layout we apply center vertical same way i m trying in flutter to make center vertical. so that entire widget view will be moved to center vertical. How to achieve this in flutter.
I have tried same using sizebox. How to make layout center vertical in flutter like in android.
Code as below:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:login_app/sso.dart';
import 'package:flutter_svg/flutter_svg.dart';
class Otp extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return OtpPage();
}
}
class OtpPage extends State<Otp> {
String assetNameArrow = 'images/ic_back.svg';
String assertOtp = 'images/otp_sms.svg';
int backspaceClick = 0;
String otpError = "";
bool isHideOTP = true;
bool isTextFiledFocus = false;
String otp1 = "";
String otp2 = "";
String otp3 = "";
String otp4 = "";
String otp5 = "";
String otp6 = "";
FocusNode focusNodeOtp1 = FocusNode();
FocusNode focusNodeOtp2 = FocusNode();
FocusNode focusNodeOtp3 = FocusNode();
FocusNode focusNodeOtp4 = FocusNode();
FocusNode focusNodeOtp5 = FocusNode();
FocusNode focusNodeOtp6 = FocusNode();
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
resizeToAvoidBottomInset:
false, //to avoid floating container on keyboard, use this
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: SafeArea(
child: Column(children: [
Padding(
padding: const EdgeInsets.only(left: 20, top: 20, bottom: 20),
child: Align(
alignment: Alignment.topLeft,
child: SvgPicture.asset(
assetNameArrow,
width: 8,
height: 19,
alignment: Alignment.topLeft,
),
),
),
SizedBox(
width: 70.0,
height: 70.0,
),
Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 16),
child: Align(
alignment: Alignment.topLeft,
child: SvgPicture.asset(
assertOtp,
width: 61,
height: 61,
alignment: Alignment.topLeft,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 16.0, right: 16.0),
child: Container(
child: Text('We need to make\nsure its you',
maxLines: 2,
style: TextStyle(
fontFamily: 'Circular_Std_Bold',
fontSize: 24.0,
color: Color(0xFF2E2E2E),
)),
),
),
),
],
),
),
Padding(
padding:
const EdgeInsets.only(left: 90.0, right: 16.0, top: 20.0),
child: Text(
'Please enter the OTP (One-Time Password) sent to your registered mobile number (ending in 6380). Do not share your OTP with anyone else.',
style: TextStyle(
fontFamily: 'Soleil_Regular',
fontSize: 14.0,
fontStyle: FontStyle.normal,
color: Color(0xFF2E2E2E),
)),
),
SizedBox(
width: 60.0,
height: 60.0,
),
Container(
// first
padding: EdgeInsets.all(16.0),
child: Row(children: [
Container(
margin: EdgeInsets.only(left: 16, right: 5),
//first
width: 46.0,
height: 48.0,
child: TextField(
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
textAlign: TextAlign.center,
// obscureText: isHideOTP,
enableSuggestions: false,
autocorrect: false,
autofocus: true,
focusNode: focusNodeOtp1,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide:
BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide:
BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp1 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp2.requestFocus();
}
},
),
),
Container(
margin: EdgeInsets.only(left: 5, right: 5),
// secound
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey ==
LogicalKeyboardKey.backspace &&
otp2 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp1.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp2,
// obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp2 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp3.requestFocus();
}
},
)),
),
Container(
// Third
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey ==
LogicalKeyboardKey.backspace &&
otp3 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp2.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp3,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp3 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp4.requestFocus();
}
},
)),
),
Container(
margin: EdgeInsets.only(left: 5, right: 5),
// Fourth
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey ==
LogicalKeyboardKey.backspace &&
otp4 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp3.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp4,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp4 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp5.requestFocus();
}
},
)),
),
Container(
// Fifth
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey ==
LogicalKeyboardKey.backspace &&
otp5 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp4.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp5,
// obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp5 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp6.requestFocus();
}
},
)),
),
Container(
// Sixth
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey ==
LogicalKeyboardKey.backspace &&
otp6 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp5.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp6,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp6 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp6.unfocus();
}
if (!otp1.isEmpty &&
!otp2.isEmpty &&
!otp3.isEmpty &&
!otp4.isEmpty &&
!otp5.isEmpty &&
!otp6.isEmpty) {
isTextFiledFocus = true;
} else {
isTextFiledFocus = false;
}
},
)),
),
/*IconButton(
onPressed: () {
setState(() {
isHideOTP = !isHideOTP;
});
},
icon:
Icon(isHideOTP ? Icons.visibility : Icons.visibility_off),
),*/
]),
),
Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Image.asset(
'images/Warning.png',
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
child: Text(
'OTP is valid for 5 minutes only.',
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular',
),
),
),
),
],
),
),
Container(
child: Padding(
padding: const EdgeInsets.only(top: 22),
child: GestureDetector(
onTap: () {},
child: Container(
child: Text(
'Did not get any OTP?',
style: TextStyle(
color: Color(0xFF0072D8),
fontSize: 16,
fontFamily: 'Circular_Std_Bold',
),
),
)),
),
),
SizedBox(
width: 125,
height: 125,
),
Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
decoration: new BoxDecoration(
color: Colors.blueAccent,
borderRadius: new BorderRadius.circular(10.0),
),
margin:
EdgeInsets.only(left: 16.0, right: 16.0, bottom: 16.0),
width: double.infinity,
height: 50.0,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
child: Text('Next ', style: TextStyle(fontSize: 16)),
onPressed: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new sso()),
);
},
color: isTextFiledFocus
? Color(0xFF0072D8)
: Color(0xFFC2C2C1),
textColor: Colors.white,
),
),
],
),
])),
)),
);
}
}
In above code,
I need to eliminate sizebox 70 because view should be center vertical & button should be bottom aligned so size box 120 also need to eliminated. how to fix this two problem in above code.
I m new to flutter i have tried above code with basic knowledge any help improving above code is appreciated.
Any help is appreciated!
Instead of using a Row(), you should have used a Stack(), with alignment: AlignmentDirectional.center.
Try This....
class Otp extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return OtpPage();
}
}
class OtpPage extends State<Otp> {
String assetNameArrow = 'images/ic_back.svg';
String assertOtp = 'images/otp_sms.svg';
int backspaceClick = 0;
String otpError = "";
bool isHideOTP = true;
bool isTextFiledFocus = false;
String otp1 = "";
String otp2 = "";
String otp3 = "";
String otp4 = "";
String otp5 = "";
String otp6 = "";
FocusNode focusNodeOtp1 = FocusNode();
FocusNode focusNodeOtp2 = FocusNode();
FocusNode focusNodeOtp3 = FocusNode();
FocusNode focusNodeOtp4 = FocusNode();
FocusNode focusNodeOtp5 = FocusNode();
FocusNode focusNodeOtp6 = FocusNode();
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
resizeToAvoidBottomInset: false, //to avoid floating container on keyboard, use this
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: SafeArea(
child: Column(children: [
Padding(
padding: const EdgeInsets.only(left: 20, top: 20, bottom: 20),
child: Align(
alignment: Alignment.topLeft,
child: SvgPicture.asset(
assetNameArrow,
width: 8,
height: 19,
alignment: Alignment.topLeft,
),
),
),
SizedBox(
width: 70.0,
height: 70.0,
),
Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 16),
child: Align(
alignment: Alignment.topLeft,
child: SvgPicture.asset(
assertOtp,
width: 61,
height: 61,
alignment: Alignment.topLeft,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 16.0, right: 16.0),
child: Container(
child: Text('We need to make\nsure its you',
maxLines: 2,
style: TextStyle(
fontFamily: 'Circular_Std_Bold',
fontSize: 24.0,
color: Color(0xFF2E2E2E),
)),
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(left: 90.0, right: 16.0, top: 20.0),
child: Text('Please enter the OTP (One-Time Password) sent to your registered mobile number (ending in 6380). Do not share your OTP with anyone else.',
style: TextStyle(
fontFamily: 'Soleil_Regular',
fontSize: 14.0,
fontStyle: FontStyle.normal,
color: Color(0xFF2E2E2E),
)),
),
SizedBox(
width: 60.0,
height: 60.0,
),
Container(
// first
padding: EdgeInsets.all(16.0),
child: Row(children: [
Container(
margin: EdgeInsets.only(left: 16, right: 5),
//first
width: 46.0,
height: 48.0,
child: TextField(
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
textAlign: TextAlign.center,
// obscureText: isHideOTP,
enableSuggestions: false,
autocorrect: false,
autofocus: true,
focusNode: focusNodeOtp1,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp1 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp2.requestFocus();
}
},
),
),
Container(
margin: EdgeInsets.only(left: 5, right: 5),
// secound
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey == LogicalKeyboardKey.backspace && otp2 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp1.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp2,
// obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp2 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp3.requestFocus();
}
},
)),
),
Container(
// Third
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey == LogicalKeyboardKey.backspace && otp3 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp2.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp3,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp3 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp4.requestFocus();
}
},
)),
),
Container(
margin: EdgeInsets.only(left: 5, right: 5),
// Fourth
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey == LogicalKeyboardKey.backspace && otp4 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp3.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp4,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp4 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp5.requestFocus();
}
},
)),
),
Container(
// Fifth
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey == LogicalKeyboardKey.backspace && otp5 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp4.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp5,
// obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp5 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp6.requestFocus();
}
},
)),
),
Container(
// Sixth
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {
if (event.logicalKey == LogicalKeyboardKey.backspace && otp6 == "") {
backspaceClick = backspaceClick + 1;
if (backspaceClick >= 2) {
focusNodeOtp5.requestFocus();
}
}
},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [
LengthLimitingTextInputFormatter(1),
],
focusNode: focusNodeOtp6,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp6 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp6.unfocus();
}
if (!otp1.isEmpty && !otp2.isEmpty && !otp3.isEmpty && !otp4.isEmpty && !otp5.isEmpty && !otp6.isEmpty) {
isTextFiledFocus = true;
} else {
isTextFiledFocus = false;
}
},
)),
),
/*IconButton(
onPressed: () {
setState(() {
isHideOTP = !isHideOTP;
});
},
icon:
Icon(isHideOTP ? Icons.visibility : Icons.visibility_off),
),*/
]),
),
Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Image.asset(
'images/Warning.png',
),
),
],
),
),
Stack(
alignment: AlignmentDirectional.center,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
child: Text(
'OTP is valid for 5 minutes only.',
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular',
),
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.only(top: 22),
child: GestureDetector(
onTap: () {},
child: Container(
child: Text(
'Did not get any OTP?',
style: TextStyle(
color: Color(0xFF0072D8),
fontSize: 16,
fontFamily: 'Circular_Std_Bold',
),
),
)),
),
),
],
),
Container(
child: Image.asset(
'images/Warning.png',
),
),
],
),
SizedBox(
width: 125,
height: 125,
),
Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
decoration: new BoxDecoration(
color: Colors.blueAccent,
borderRadius: new BorderRadius.circular(10.0),
),
margin: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 16.0),
width: double.infinity,
height: 50.0,
child: FlatButton(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
child: Text('Next ', style: TextStyle(fontSize: 16)),
onPressed: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
// Navigator.push(
// context,
// new MaterialPageRoute(
// builder: (context) => new sso()),
// );
},
color: isTextFiledFocus ? Color(0xFF0072D8) : Color(0xFFC2C2C1),
textColor: Colors.white,
),
),
],
),
])),
)),
);
}
}
You can take help from this Article and build whatever layout you want to build.
Flutter Layout Cheatsheat
That's pretty simple use column and inside column set the property of cross-axis
to center
something like this in the parent widget
Column(
crossAxisAlignment:CrossAxisAlignment.center,
children:[
//your rest of child widget goes here
]
)
You need to use
mainAxisAlignment: MainAxisAlignment.center, by default it is MainAxisAlignment.start.
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Center"),
],
),
Update
Separating MaterialApp, Better solution will be using Stack
class Otp extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return OtpPage();
}
}
class OtpPage extends State<Otp> {
String assetNameArrow = 'images/ic_back.svg';
String assertOtp = 'images/otp_sms.svg';
int backspaceClick = 0;
String otpError = "";
bool isHideOTP = true;
bool isTextFiledFocus = false;
String otp1 = "";
String otp2 = "";
String otp3 = "";
String otp4 = "";
String otp5 = "";
String otp6 = "";
FocusNode focusNodeOtp1 = FocusNode();
FocusNode focusNodeOtp2 = FocusNode();
FocusNode focusNodeOtp3 = FocusNode();
FocusNode focusNodeOtp4 = FocusNode();
FocusNode focusNodeOtp5 = FocusNode();
FocusNode focusNodeOtp6 = FocusNode();
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset:
false, //to avoid floating container on keyboard, use this
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
// crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(left: 20, top: 20, bottom: 20),
child: Align(
alignment: Alignment.topLeft,
child: Container(
color: Colors.deepOrange,
width: 8,
height: 19,
alignment: Alignment.topLeft,
),
),
),
SizedBox(
width: 70.0,
height: 70.0,
),
Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 16),
child: Align(
alignment: Alignment.topLeft,
child: Container(
color: Colors.deepPurple,
width: 61,
height: 61,
alignment: Alignment.topLeft,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 16.0, right: 16.0),
child: Container(
child: Text('We need to make\nsure its you',
maxLines: 2,
style: TextStyle(
fontFamily: 'Circular_Std_Bold',
fontSize: 24.0,
color: Color(0xFF2E2E2E),
)),
),
),
),
],
),
),
Padding(
padding:
const EdgeInsets.only(left: 90.0, right: 16.0, top: 20.0),
child: Text(
'Please enter the OTP (One-Time Password) sent to your registered mobile number (ending in 6380). Do not share your OTP with anyone else.',
style: TextStyle(
fontFamily: 'Soleil_Regular',
fontSize: 14.0,
fontStyle: FontStyle.normal,
color: Color(0xFF2E2E2E),
)),
),
SizedBox(
width: 60.0,
height: 60.0,
),
Container(
// first
padding: EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
margin: EdgeInsets.only(left: 16, right: 5),
//first
width: 46.0,
height: 48.0,
child: TextField(
textAlign: TextAlign.center,
// obscureText: isHideOTP,
enableSuggestions: false,
autocorrect: false,
autofocus: true,
focusNode: focusNodeOtp1,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp1 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp2.requestFocus();
}
},
),
),
Container(
margin: EdgeInsets.only(left: 5, right: 5),
// secound
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {},
child: TextField(
textAlign: TextAlign.center,
focusNode: focusNodeOtp2,
// obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp2 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp3.requestFocus();
}
},
)),
),
Container(
// Third
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [],
focusNode: focusNodeOtp3,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp3 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp4.requestFocus();
}
},
)),
),
Container(
margin: EdgeInsets.only(left: 5, right: 5),
// Fourth
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [],
focusNode: focusNodeOtp4,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp4 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp5.requestFocus();
}
},
)),
),
Container(
// Fifth
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [],
focusNode: focusNodeOtp5,
// obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp5 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp6.requestFocus();
}
},
)),
),
Container(
// Sixth
margin: EdgeInsets.only(left: 5, right: 5),
width: 46.0,
height: 48.0,
child: RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (event) {},
child: TextField(
textAlign: TextAlign.center,
inputFormatters: [],
focusNode: focusNodeOtp6,
//obscureText: isHideOTP,
autofocus: true,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xFF0072D8), width: 1.0),
),
),
onChanged: (value) {
setState(() {
otp6 = value;
otpError = "";
});
if (value.isNotEmpty) {
print('value is not null');
focusNodeOtp6.unfocus();
}
if (!otp1.isEmpty &&
!otp2.isEmpty &&
!otp3.isEmpty &&
!otp4.isEmpty &&
!otp5.isEmpty &&
!otp6.isEmpty) {
isTextFiledFocus = true;
} else {
isTextFiledFocus = false;
}
},
)),
),
/*IconButton(
onPressed: () {
setState(() {
isHideOTP = !isHideOTP;
});
},
icon:
Icon(isHideOTP ? Icons.visibility : Icons.visibility_off),
),*/
]),
),
Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 24,
height: 24,
color: Colors.green,
// child: Image.asset(
// 'images/Warning.png',
// ),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
child: Text(
'OTP is valid for 5 minutes only.',
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular',
),
),
),
),
],
),
),
Container(
child: Padding(
padding: const EdgeInsets.only(top: 22),
child: GestureDetector(
onTap: () {},
child: Container(
child: Text(
'Did not get any OTP?',
style: TextStyle(
color: Color(0xFF0072D8),
fontSize: 16,
fontFamily: 'Circular_Std_Bold',
),
),
)),
),
),
SizedBox(
width: 125,
height: 125,
),
],
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: Container(
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.circular(10.0),
),
margin: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 16.0),
width: double.infinity,
height: 50.0,
child: FlatButton(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
child: Text('Next ', style: TextStyle(fontSize: 16)),
onPressed: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
},
color: isTextFiledFocus ? Color(0xFF0072D8) : Color(0xFFC2C2C1),
textColor: Colors.white,
),
),
);
}
}
class Landing extends StatefulWidget {
Landing({Key? key}) : super(key: key);
#override
_LandingState createState() => _LandingState();
}
class _LandingState extends State<Landing> with SingleTickerProviderStateMixin {
final List<Widget> pages = [
Home(),
// Signin(),
Center(
child: Text("SignIn"),
)
];
late TabController _tabbarcontroller;
int _currentIndexNavBar = 0;
#override
void initState() {
super.initState();
_tabbarcontroller = new TabController(length: pages.length, vsync: this);
}
#override
void dispose() {
_tabbarcontroller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: TabBarView(
controller: _tabbarcontroller,
children: pages,
),
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
label: "A",
icon: Icon(
Icons.ac_unit,
),
),
BottomNavigationBarItem(
label: "B",
icon: Icon(
Icons.face,
),
),
],
currentIndex: _currentIndexNavBar,
onTap: (value) {
setState(() {
_tabbarcontroller.index = value;
_currentIndexNavBar = value;
});
},
));
}
}
class Parent extends StatefulWidget {
Parent({Key? key}) : super(key: key);
#override
_ParentState createState() => _ParentState();
}
class _ParentState extends State<Parent> {
#override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
height: 100,
width: 100,
color: Colors.red,
),
Expanded(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Home(),
]),
))
]);
}
}

Value from FormKey is always empty on Flutter

I have a simple Screen class dart and a controller for this Screen here is the form:
Container(
height: screenHeight * 0.8,
width: double.infinity,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image(
image: AssetImage('assets/images/cumpra_logo_dark.png'),
height: screenHeight * 0.08,
),
SizedBox(
height: screenHeight * 0.04,
),
Container(
width: double.infinity,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: _con.formKey,
child: Column(
children: <Widget>[
Container(
width: screenWidth * 0.7,
child: TextFormField(
style: TextStyle(
fontSize: screenHeight * 0.03,
color: Colors.black,
),
inputFormatters: [
UpperCaseTextFormatter(),
],
textAlign: TextAlign.center,
decoration: InputDecoration(
filled: true,
fillColor: Color(0XFFe0e0e0),
hintText: 'Código da Empresa',
contentPadding: const EdgeInsets.only(
left: 14.0,
bottom: 8.0,
top: 8.0),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.white),
borderRadius:
BorderRadius.circular(25.7),
),
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.white),
borderRadius:
BorderRadius.circular(25.7),
),
),
validator: (value) {
if (value.isEmpty) {
return 'Campo obrigatório';
}
if (value.length < 6) {
return 'Mínimo de 6 dígitos';
}
return null;
},
onChanged: (String text) {
_con.userDataTemp['pin'] = text;
},
),
),
SizedBox(
height: screenHeight * 0.04,
),
Container(
width: screenWidth * 0.7,
child: TextFormField(
keyboardType: TextInputType.number,
obscureText: true,
autofocus: false,
style: TextStyle(
fontSize: screenHeight * 0.03,
color: Colors.black,
),
textAlign: TextAlign.center,
decoration: InputDecoration(
filled: true,
fillColor: Color(0XFFe0e0e0),
hintText: 'Número de Acesso',
contentPadding: const EdgeInsets.only(
left: 14.0,
bottom: 8.0,
top: 8.0),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.white),
borderRadius:
BorderRadius.circular(25.7),
),
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.white),
borderRadius:
BorderRadius.circular(25.7),
),
),
validator: (value) {
if (value.isEmpty) {
return 'Campo obrigatório';
}
if (value.length < 6) {
return 'Mínimo de 6 dígitos';
}
return null;
},
onChanged: (String text) {
_con.userDataTemp['code'] = text;
},
),
),
SizedBox(
height: screenHeight * 0.06,
),
Container(
width: screenWidth * 0.7,
child: RaisedButton(
onPressed: _con.signIn,
child: Text('LOGAR'),
padding: EdgeInsets.symmetric(
horizontal: 10.0,
vertical: 12.0,
),
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(30.0),
),
color: ThemeColor.AppSecundaryColor,
textColor: Colors.white,
elevation: 5,
),
),
SizedBox(
height: 30.0,
),
],
),
),
],
),
),
),
],
),
)
I've already put the KEY here key: _con.formKey, _con reference from my controller here is the code:
LoginController(context) {
this.context = context;
formKey = new GlobalKey<FormState>();
this.scaffoldKey = new GlobalKey<ScaffoldState>();
utilService = Provider.of<UtilService>(context, listen: false);
sharedService = Provider.of<SharedService>(context, listen: false);
userService = Provider.of<UserService>(context, listen: false);
authService = Provider.of<AuthService>(context, listen: false);
}
Why always when i press the button "LOGAR" the code go to this validate:
validator: (value) {
if (value.isEmpty) {
return 'Campo obrigatório';
}
if (value.length < 6) {
return 'Mínimo de 6 dígitos';
}
return null;
},
onChanged: (String text) {
_con.userDataTemp['pin'] = text;
},
But always this value is EMPTY, what I'm missing?
To get the values out of the FormBuilder you must first save the form builder, to do so you must consider doing something like this.
Map json = formKey.currentState.saveAndValidate();