How do I reset my reset the Currentstate of this application - flutter

Please, how do i reset my currentstate Navigator such that when user type in the text field, the error message goes off instead of remaining until they hit the SUBMIT button. I know it's the use of the unChanged function, but I cant really figure that out.
Please, kindly help out. I have also attached my code below.
Thank you.
class ForgotPwd extends StatefulWidget {
#override
_ForgotPwdState createState() => _ForgotPwdState();
}
class _ForgotPwdState extends State<ForgotPwd> {
final TextEditingController _popEditingController = TextEditingController();
final _formKey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Dialog(
child: Stack(
children: [
Container(
height: 270.0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
),
child: Padding(
padding: EdgeInsets.only(top: 40.0),
child: Form(
key: _formKey,
child: Column(children: [
Text(
'Forgot Password?',
style: TextStyle(
fontFamily: 'Work Sans',
fontSize: 18.0,
color: Color(0XFF25282B),
fontWeight: FontWeight.w600),
),
Padding(
padding:
EdgeInsets.only(left: 20.0, right: 20.0, top: 30.0),
child: TextFormField(
validator: (value) {
if (value.isEmpty == true) {
return 'Please enter email address';
} else {
return null;
}
},
controller: _popEditingController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
prefixIcon: Icon(FontAwesomeIcons.envelope),
labelStyle: TextStyle(fontSize: 15.0),
hintText: 'Enter your email address',
hintStyle: TextStyle(fontSize: 15.0),
border: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black))),
),
),
Padding(
padding: EdgeInsets.all(30.0),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(10.0)),
color: Color(0XFFC20370),
onPressed: () {
setState(() {
if (_formKey.currentState.validate()) {
Navigator.of(context)
.pop(_popEditingController.text.toString());
}
});
},
child: Text('SUBMIT',
style: TextStyle(
color: Colors.white,
fontFamily: 'Work Sans',
fontSize: 18.0,
fontWeight: FontWeight.w600)),
))
]),
),
),
),
],
),
);
}
}

You can call _formKey.currentState.validate() in the onChanged method of the textfield

Related

"Invalid value constant value "for TextEditingController in Flutter?

Was building UI with TextField() in Flutter, I defined the controller in state and tried to use that defined emailController and passwordController in TextField() but it says "Invalid constant value.". I tried to resolve it but didn't work. Here is the code for login_screen.dart
import 'package:flutter/material.dart';
import 'package:rider_app/routes/routing_constants.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
#override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final _validate = false;
// #override
// void dispose() {
// emailController.dispose();
// passwordController.dispose();
// super.dispose();
// }
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Column(
children: [
const SizedBox(
height: 40.0,
),
const Center(
child: Image(
image: AssetImage("lib/assets/images/logo.png"),
width: 200.0,
height: 200.0,
alignment: Alignment.center,
)),
const SizedBox(
height: 2.0,
),
const Text(
"Login as a Rider",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25.0, fontFamily: "Brand Bold"),
),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 30.0, vertical: 10.0),
child: Column(
children: [
const SizedBox(
height: 10.0,
),
const TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: "Email",
labelStyle: TextStyle(
fontSize: 15.0,
),
hintText: "Email address",
hintStyle: TextStyle(fontSize: 12.0, color: Colors.grey),
// focusedBorder: OutlineInputBorder(
// borderSide:
// BorderSide(width: 1.0, color: Colors.blueAccent),
// ),
// enabledBorder: OutlineInputBorder(
// borderSide: BorderSide(width: 1.0),
// ),
),
style: TextStyle(fontSize: 15.0),
controller: emailController,
),
const SizedBox(
height: 10.0,
),
const TextField(
controller: passwordController,
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
labelText: "Password",
labelStyle: TextStyle(fontSize: 15.0),
hintText: "Your password",
hintStyle: TextStyle(fontSize: 12.0, color: Colors.grey),
// focusedBorder: OutlineInputBorder(
// borderSide: BorderSide(width: 1.0, color: Colors.blueAccent),
),
// enabledBorder: OutlineInputBorder(
// borderSide: BorderSide(width: 1.0)
// )
// ),
obscureText: true,
style: TextStyle(fontSize: 15.0),
),
const SizedBox(
height: 45.0,
),
ElevatedButton(
onPressed: () {
debugPrint("Logged In");
Navigator.pushNamed(context, homeScreen);
},
child: const Padding(
padding: EdgeInsets.symmetric(
horizontal: 23, vertical: 11),
child: Text(
'Login',
style: TextStyle(
fontSize: 18,
fontFamily: "Brand Bold",
fontWeight: FontWeight.w500),
),
),
),
],
),
),
FlatButton(
onPressed: () {
Navigator.pushNamed(context, signUpScreen);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text(
"Don't have Account?",
style: TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "Brand-Regular"),
),
Text(
" Register Here.",
style: TextStyle(
fontFamily: "Brand-Regular",
fontWeight: FontWeight.w600),
)
],
))
],
),
),
);
}
}
The controller defination is okay but as I try to assign controller to the TextField() controller, the error is thrown. Screenshot attached herewith:
Have any idea ?? let me know, even suggetion is appreciated. Thank you!
GitHub Repo: Project Link Github
Remove the const before TextField, that should resolve the error.
Remove const keywords at the beginning of TextField widgets.
Try removing the const modifier from the TextField.
remove const from const TextField and you are good to go.
Your code will be like this:
TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: "Email",
labelStyle: TextStyle(
fontSize: 15.0,
),
Try removing the "const" before TextField, (or one of the top widgets in the widget tree), that should resolve the error.

Register screen check if textboxes are empty

Hello guys i am trying to check if the input fields for my register screen are empty and give an error message for each one if any is empty.
I added the text editing controllers and the booleans for if each box is empty or not.
Then on button press it checks if there is no text in any of the fields and should display the error message for each of the field.
Right now it tells me that the text provided in this "errorText: _emptyboxmail ? 'field cannot be empty' : null," is dead code for each one.
If i change the boolean to true then it shows that null is dead code in code i gave above.
How do i make this work without having dead code? The program runs as usual it just not show me the error message if i leave the boxes empty
import 'package:club_mark_2/styles/style.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'userPreferences/user_details_preference.dart';
class RegisterScreen extends StatefulWidget {
const RegisterScreen({Key? key}) : super(key: key);
#override
_RegisterScreenState createState() => _RegisterScreenState();
}
class _RegisterScreenState extends State<RegisterScreen> {
#override
Widget build(BuildContext context) {
final registerEmail = TextEditingController();
final registerPassword = TextEditingController();
final verifyPassword = TextEditingController();
final registerName = TextEditingController();
bool _emptyboxmail = false;
bool _emptyboxpass = false;
bool _emptyboxverify = false;
bool _emptyboxregistername = false;
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
#override
void dispose() {
// Clean up the controller when the widget is disposed.
registerEmail.dispose();
registerPassword.dispose();
verifyPassword.dispose();
registerName.dispose();
super.dispose();
}
return Scaffold(
backgroundColor: Colors.transparent,
body: SizedBox(
width: width,
height: height,
child: Stack(children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 15, top: 165, right: 15),
child: ListView(
children: [
Center(
child: Text(
'SIGN UP',
style: TextStyle(
color: Colors.white,
fontSize: 30.0,
fontWeight: FontWeight.bold,
fontFamily: 'Montserrat',
),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: TextFormField(
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
errorText: _emptyboxregistername
? 'field cannot be empty'
: null,
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.grey.withOpacity(0.5)),
borderRadius: BorderRadius.circular(20)),
hintText: 'Full Name',
hintStyle: TextStyle(color: kcMediumGreyColor),
labelStyle: TextStyle(color: Colors.white),
prefixIcon: const Icon(
Icons.person,
color: Colors.white,
),
),
controller: registerName,
),
),
SizedBox(
height: 5,
),
Container(
padding: EdgeInsets.all(8.0),
child: TextFormField(
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
errorText: _emptyboxmail ? 'field cannot be empty' : null,
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.grey.withOpacity(0.5)),
borderRadius: BorderRadius.circular(20),
),
hintText: 'E-mail',
hintStyle: TextStyle(color: kcMediumGreyColor),
labelStyle: TextStyle(color: Colors.white),
prefixIcon: const Icon(
Icons.email,
color: Colors.white,
),
),
controller: registerEmail,
),
),
SizedBox(
height: 5,
),
Container(
padding: EdgeInsets.all(8.0),
child: TextFormField(
style: TextStyle(color: Colors.white),
obscureText: true,
decoration: InputDecoration(
errorText: _emptyboxpass ? 'field cannot be empty' : null,
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.grey.withOpacity(0.5)),
borderRadius: BorderRadius.circular(20),
),
hintText: 'Password',
hintStyle: TextStyle(color: kcMediumGreyColor),
labelStyle: TextStyle(
color: Colors.white,
),
prefixIcon: const Icon(
Icons.lock,
color: Colors.white,
),
),
controller: registerPassword,
),
),
SizedBox(
height: 5,
),
//VERIFY PASSWORD TEXT BOX
Container(
padding: EdgeInsets.all(8.0),
child: TextFormField(
style: TextStyle(color: Colors.white),
obscureText: true,
decoration: InputDecoration(
errorText:
_emptyboxverify ? 'field cannot be empty' : null,
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.grey.withOpacity(0.5)),
borderRadius: BorderRadius.circular(20),
),
hintText: 'Verify Password',
hintStyle: TextStyle(color: kcMediumGreyColor),
labelStyle: TextStyle(color: Colors.white),
prefixIcon: const Icon(
Icons.lock,
color: Colors.white,
),
),
//TEXT EDITING CONTROLLER
controller: verifyPassword,
),
),
SizedBox(
height: 20,
),
SizedBox(
height: 60,
child: TextButton(
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
),
),
// THE BUTTON THAT CHECKS IF THE BOOLEANS ARE TRUE OR FALSE FOR THE TEXT BEING EMPTY
onPressed: () {
registerEmail.text.isEmpty
? _emptyboxmail = true
: _emptyboxmail = false;
registerName.text.isEmpty
? _emptyboxregistername = true
: _emptyboxregistername = false;
registerPassword.text.isEmpty
? _emptyboxpass = true
: _emptyboxpass = false;
verifyPassword.text.isEmpty
? _emptyboxverify = true
: _emptyboxverify = false;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UserDetailsPreference(),
),
);
},
child: Text('Sign Up'),
),
),
SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(8.0),
child: Text(
"━━━━━ OR SIGN UP WITH ━━━━━",
style: TextStyle(color: kcMediumGreyColor),
),
),
],
),
SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(),
child: ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.asset('lib/assets/images/googleIcon.png',
width: 50.0, height: 50.0),
),
),
),
SizedBox(
width: 30,
),
InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(),
child: ClipRRect(
// borderRadius: BorderRadius.circular(20.0),
child: Image.asset(
'lib/assets/images/facebookIcon.png',
width: 45.0,
height: 45.0),
),
),
),
],
),
SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(8.0),
child: Text(
"Already have an account?",
style: TextStyle(color: Colors.white),
),
),
Text(
"SIGN IN",
style: TextStyle(color: kcPrimaryColor),
),
],
),
],
),
),
]),
),
);
}
}
You have to move all your initializations and dispose method out of build method.
class _RegisterScreenState extends State<RegisterScreen> {
final registerEmail = TextEditingController();
final registerPassword = TextEditingController();
final verifyPassword = TextEditingController();
final registerName = TextEditingController();
bool _emptyboxmail = false;
bool _emptyboxpass = false;
bool _emptyboxverify = false;
bool _emptyboxregistername = false;
#override
void dispose() {
// Clean up the controller when the widget is disposed.
registerEmail.dispose();
registerPassword.dispose();
verifyPassword.dispose();
registerName.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Scaffold(
backgroundColor: Colors.transparent,
body: SizedBox(
width: width,
height: height,
// ... rest of your code
Create a Form with a GlobalKey
Add a TextFormField with validation logic
Create a button to validate and submit the form
https://flutter.dev/docs/cookbook/forms/validation

updating the text in the controller not working after converting the class from stateless to stateful

the code works fine when it's statelesswidget but after converting it to statefulwidget the textformfiled
stopped showing the text value from firestore.
when entering data the filed works just fine and the value stored in the firebase but when editing the the data the filed not showing the value to be edited and when entering the data in the filed new record is being stored in the firebase
class PostView extends StatefulWidget {
PostView({Key key, this.edittingPost}) : super(key: key);
final Post edittingPost;
#override
State<StatefulWidget> createState() {
return CreatePostView();
}
}
class CreatePostView extends State<PostView>{
final airlineController = TextEditingController();
final paxController = TextEditingController();
final infantController = TextEditingController();
final transitController = TextEditingController();
final dateController = TextEditingController();
Post edittingPost;
#override
Widget build(BuildContext context) {
return ViewModelBuilder<CreatePostViewModel>.reactive(
viewModelBuilder: () => CreatePostViewModel(),
onModelReady: (model) {
// update the text in the controller
airlineController.text = edittingPost?.airline ?? '';
paxController.text = edittingPost?.pax ?? '';
infantController.text = edittingPost?.infant ?? '';
transitController.text = edittingPost?.transit ?? '';
dateController.text = edittingPost?.date ?? '';
model.setEdittingPost(edittingPost);
},
builder: (context, model, child) => Scaffold(
floatingActionButton: FloatingActionButton(
child: !model.busy
? Icon(Icons.add)
: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.white),
),
onPressed: () {
if (!model.busy) {
model.addPost(date: dateController.text, airline: airlineController.text, pax: paxController.text,
infant: infantController.text, transit: transitController.text);
}
},
backgroundColor:
!model.busy ? Theme.of(context).primaryColor : Colors.grey[600],
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
verticalSpace(40),
Text(
'Create Post',
style: TextStyle(fontSize: 26),
),
verticalSpaceMedium,
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 55.0,
child: TextFormField(
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.calendar_today,
color: Colors.white,
),
hintText: 'Date of Flight',
hintStyle: kHintTextStyle),
controller: dateController,
onTap: () async{
DateTime date = DateTime(1900);
FocusScope.of(context).requestFocus(FocusNode());
date = await showDatePicker(
context: context,
initialDate:DateTime.now(),
firstDate:DateTime(1900),
lastDate: DateTime(2100));
dateController.text = date.toIso8601String();},),
),
verticalSpaceMedium,
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 55.0,
child: TextFormField(
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.airplanemode_active,
color: Colors.white,
),
hintText: 'Airline',
hintStyle: kHintTextStyle,
),
controller: airlineController,
)),
verticalSpaceMedium,
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 55.0,
child: TextFormField(
keyboardType: TextInputType.number,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.airline_seat_recline_extra,
color: Colors.white,
),
hintText: 'Pax',
hintStyle: kHintTextStyle,
),
controller: paxController,
)),
verticalSpaceMedium,
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 55.0,
child: TextFormField(
keyboardType: TextInputType.number,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.child_friendly,
color: Colors.white,
),
hintText: 'Infant',
hintStyle: kHintTextStyle,
),
controller: infantController,
)),
verticalSpaceMedium,
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 55.0,
child: TextFormField(
keyboardType: TextInputType.number,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.transit_enterexit,
color: Colors.white,
),
hintText: 'Transit',
hintStyle: kHintTextStyle,
),
controller: transitController,
)),
],
),
),
)),
);
}
}
If you want see change text from textfromField - you have to use setState() where you will asssigned new value to variable.

How can I eliminate this error : Incorrect use of ParentDataWidget

I didn't make use of Expanded widget but I don't know why I keep getting for this error.
Uncaught exception by widget library, Incorrect use of ParentDataWidget in four places I can't get where exactly the error is coming from. though it doesn't stop me from using the application but I feel it should be fixed. please can anyone help me?
This is my code below:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:erg_app/Anchors.dart';
import 'package:erg_app/api/api.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LogIn extends StatefulWidget {
#override
_LogInState createState() => _LogInState();
}
class _LogInState extends State<LogIn> {
bool _isLoading = false;
TextEditingController mailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
ScaffoldState scaffoldState;
_showMsg() {
final snackBar = SnackBar(
content: Text(
'Invalid Username or Password',
style: (TextStyle(fontSize: 18)),
),
backgroundColor: Colors.amber[900],
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
},
child: Scaffold(
key: _scaffoldKey,
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [0.0, 0.4, 0.9],
colors: [
Color(0XFF4CAF50),
Color(0xFF388E3C),
Color(0xFF075009),
],
),
),
child: ListView(
children: <Widget>[
/////////// background///////////
SizedBox(height: 30),
new Container(
width: 100.00,
height: 100.00,
decoration: new BoxDecoration(
image: new DecorationImage(
image: AssetImage('assets/images/icon.png'),
fit: BoxFit.contain,
),
)),
Column(
children: <Widget>[
Positioned(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Positioned(
left: 30,
top: 100,
child: Container(
margin: EdgeInsets.only(top: 50),
child: Center(
child: Text(
"Welcome",
style: TextStyle(
color: Colors.white,
fontSize: 23,
fontWeight: FontWeight.bold),
),
),
),
),
SizedBox(height: 30),
Card(
elevation: 4.0,
color: Colors.white,
margin: EdgeInsets.only(left: 20, right: 20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
child: Padding(
padding: const EdgeInsets.all(10.0),
// child: form(key: _formKey),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
///////////// Email//////////////
TextField(
style: TextStyle(color: Color(0xFF000000)),
controller: mailController,
cursorColor: Color(0xFF9b9b9b),
keyboardType: TextInputType.text,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.account_circle,
color: Colors.grey,
),
hintText: "Username",
hintStyle: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 15,
fontWeight: FontWeight.normal),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.green)),
),
),
/////////////// password////////////////////
TextField(
style: TextStyle(color: Color(0xFF000000)),
cursorColor: Color(0xFF9b9b9b),
controller: passwordController,
keyboardType: TextInputType.number,
obscureText: true,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.vpn_key,
color: Colors.grey,
),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.green)),
hintText: "Password",
hintStyle: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 15,
fontWeight: FontWeight.normal),
),
),
///////////// LogIn Botton///////////////////
Padding(
padding: const EdgeInsets.all(10.0),
child: FlatButton(
child: Padding(
padding: EdgeInsets.only(
top: 8,
bottom: 8,
left: 10,
right: 10),
child: Text(
_isLoading ? 'Loging...' : 'Login',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
color: Colors.green,
disabledColor: Colors.grey,
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(20.0)),
onPressed: _isLoading ? null : _login,
),
),
],
),
),
),
//////////// new account///////////////
Padding(
padding: const EdgeInsets.only(top: 20),
child: InkWell(
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => LogIn()));
},
child: Text(
'Forgot Your Password?',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
),
],
),
),
),
],
)
],
),
),
));
// Gesture ends here
}
}
this is the picture of the error message:
You have a Positioned widget inside Column widgets in different parts of your code.
A Positioned widget must be a descendant of a Stack, and the path from
the Positioned widget to its enclosing Stack must contain only
StatelessWidgets or StatefulWidgets
I pasted the above from Flutter docs and it says that a Positioned must be descendant of a Stack i.e you cannot have a position inside other Widgets aside from a Stack widget.
You should remove the Positioned widgets from your code. or wrap them with a Stack widget
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:erg_app/Anchors.dart';
import 'package:erg_app/api/api.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LogIn extends StatefulWidget {
#override
_LogInState createState() => _LogInState();
}
class _LogInState extends State<LogIn> {
bool _isLoading = false;
TextEditingController mailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
ScaffoldState scaffoldState;
_showMsg() {
final snackBar = SnackBar(
content: Text(
'Invalid Username or Password',
style: (TextStyle(fontSize: 18)),
),
backgroundColor: Colors.amber[900],
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
},
child: Scaffold(
key: _scaffoldKey,
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [0.0, 0.4, 0.9],
colors: [
Color(0XFF4CAF50),
Color(0xFF388E3C),
Color(0xFF075009),
],
),
),
child: ListView(
children: <Widget>[
/////////// background///////////
SizedBox(height: 30),
new Container(
width: 100.00,
height: 100.00,
decoration: new BoxDecoration(
image: new DecorationImage(
image: AssetImage('assets/images/icon.png'),
fit: BoxFit.contain,
),
)),
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 50),
child: Center(
child: Text(
"Welcome",
style: TextStyle(
color: Colors.white,
fontSize: 23,
fontWeight: FontWeight.bold),
),
),
),
SizedBox(height: 30),
Card(
elevation: 4.0,
color: Colors.white,
margin: EdgeInsets.only(left: 20, right: 20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
child: Padding(
padding: const EdgeInsets.all(10.0),
// child: form(key: _formKey),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
///////////// Email//////////////
TextField(
style: TextStyle(color: Color(0xFF000000)),
controller: mailController,
cursorColor: Color(0xFF9b9b9b),
keyboardType: TextInputType.text,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.account_circle,
color: Colors.grey,
),
hintText: "Username",
hintStyle: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 15,
fontWeight: FontWeight.normal),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.green)),
),
),
/////////////// password////////////////////
TextField(
style: TextStyle(color: Color(0xFF000000)),
cursorColor: Color(0xFF9b9b9b),
controller: passwordController,
keyboardType: TextInputType.number,
obscureText: true,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.vpn_key,
color: Colors.grey,
),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.green)),
hintText: "Password",
hintStyle: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 15,
fontWeight: FontWeight.normal),
),
),
///////////// LogIn Botton///////////////////
Padding(
padding: const EdgeInsets.all(10.0),
child: FlatButton(
child: Padding(
padding: EdgeInsets.only(
top: 8,
bottom: 8,
left: 10,
right: 10),
child: Text(
_isLoading ? 'Loging...' : 'Login',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
color: Colors.green,
disabledColor: Colors.grey,
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(20.0)),
onPressed: _isLoading ? null : _login,
),
),
],
),
),
),
//////////// new account///////////////
Padding(
padding: const EdgeInsets.only(top: 20),
child: InkWell(
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => LogIn()));
},
child: Text(
'Forgot Your Password?',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
),
],
),
),
],
)
],
),
),
));
// Gesture ends here
}
}

How can I disable texformfield input in flutter?

In my application, written in Flutter, I need an icon button to enable/disable input to it. I declared bool variables which are holding information to TextFormField to be enabled/disabled. But, it is not working as expected. It looks that it depends on type of keyboard, so until all characters keyboard is in use, it is not validated. So, if I disable field which allows only numeric keyboard, field input is not disabled until I switch to field with all characters kexboard. So, how can I disable input to TextFomField immediatelly?
`import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:aisscanning/data/database_helper.dart';
import 'package:aisscanning/data/inventoryitem.dart';
import 'package:aisscanning/data/masteritem.dart';
class InventoryScanning extends StatefulWidget {
InventoryScanning({Key key, this.title}) : super(key: key);
final String title;
#override
_InventoryScanningState createState() => new _InventoryScanningState();
}
class _InventoryScanningState extends State<InventoryScanning> {
static GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
final TextEditingController teSku = TextEditingController();
final TextEditingController teDesc = TextEditingController();
final TextEditingController teLoc = TextEditingController();
final TextEditingController teSubLoc = TextEditingController();
final TextEditingController teQty = TextEditingController();
final TextEditingController tePrice = TextEditingController();
bool lockLoc = true;
bool lockSubLoc = true;
bool lockQty = true;
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Item Scanning"),
backgroundColor: Colors.black,
),
resizeToAvoidBottomPadding: false,
body: Padding(
padding: const EdgeInsets.only(top: 12.0),
child: new Form(
key: _formKey,
autovalidate: true,
child: new ListView(
// padding: const EdgeInsets.symmetric(horizontal: 16.0),
padding: new EdgeInsets.all(8.0),
itemExtent: 60.0,
children: <Widget>[
new Row(
// Location
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: const Icon(
Icons.lock,
color: const Color(0xFF167F67),
size: 25.0,
),
onPressed: () {
if (lockLoc == false) {
lockLoc = true;
} else {
lockLoc = false;
}
},
),
Expanded(
child: TextFormField(
controller: teLoc,
keyboardType: TextInputType.number,
enabled: lockLoc,
style: new TextStyle(
color: const Color(0xFF0f2638),
fontFamily: 'ProximaNova',
fontStyle: FontStyle.normal,
fontSize: 20.0,
),
decoration: new InputDecoration(
hintText: 'Enter Location or press Search Icon',
labelText: 'Location',
fillColor: Colors.white,
contentPadding: new EdgeInsets.symmetric(
vertical: 15.0, horizontal: 10.0),
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
labelStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 20.0,
),
),
),
),
IconButton(
icon: const Icon(
Icons.search,
color: const Color(0xFF167F67),
size: 25.0,
),
onPressed: () {},
),
],
),
new Row(
// SubLocation
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: const Icon(
Icons.lock,
color: const Color(0xFF167F67),
size: 25.0,
),
onPressed: () {
if (lockSubLoc == false) {
lockSubLoc = true;
} else {
lockSubLoc = false;
} },
),
Expanded(
child: TextFormField(
controller: teSubLoc,
enabled: lockSubLoc,
style: new TextStyle(
color: const Color(0xFF0f2638),
fontFamily: 'ProximaNova',
fontStyle: FontStyle.normal,
fontSize: 20.0,
),
decoration: new InputDecoration(
hintText: 'Enter SubLocation',
labelText: 'SubLocation',
contentPadding: new EdgeInsets.symmetric(
vertical: 15.0, horizontal: 10.0),
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
labelStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 16.0,
),
),
),
),
IconButton(
icon: const Icon(
Icons.search,
color: const Color(0xFF167F67),
size: 25.0,
),
onPressed: () {},
),
],
),
new Row(
// QTY
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: const Icon(
Icons.lock,
color: const Color(0xFF167F67),
size: 25.0,
),
onPressed: () {
if (lockQty == false) {
lockQty = true;
} else {
lockQty = false;
}},
),
Expanded(
child: TextFormField(
controller: teQty,
textAlign: TextAlign.right,
keyboardType: TextInputType.number,
enabled: lockQty,
style: new TextStyle(
color: const Color(0xFF0f2638),
fontFamily: 'ProximaNova',
fontStyle: FontStyle.normal,
fontSize: 18.0,
),
decoration: new InputDecoration(
hintText: 'Enter Quantity',
labelText: 'Quantity',
contentPadding: new EdgeInsets.symmetric(
vertical: 15.0, horizontal: 10.0),
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(30.0),
borderSide: new BorderSide(),
),
labelStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 20.0,
),
),
),
),
],
),
new Row(
// Buttons
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new RaisedButton(
child: new Text("Save"),
onPressed: () {
addRecord();
},
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
color: Colors.green,
elevation: 4.0,
),
new RaisedButton(
child: new Text("Cancel"),
onPressed: () {},
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
color: Colors.red,
elevation: 4.0,
),
],
),
]),
),
),
);
}
`
When Ever we need to update our screen UI we need to call SetState(). so In Your Code in onPressed Method.
onPressed: () {
setState(() {
if (lockLoc == false) {
lockLoc = true;
} else {
lockLoc = false;
}
});
},