Error ocures in onTap property while calling method - flutter

TextField(
textAlign: TextAlign.center,
obscureText: _obscureText,
onChanged: (value) {
//Do something with the user input.
password = value;
},
style: const TextStyle(color: Colors.black),
decoration: const InputDecoration(
hintText: 'Enter your Password.',
hintStyle: TextStyle(color: Colors.black26),
suffix: InkWell(
child: Icon(Icons.visibility),
onTap: _togglePasswordView,
//here is error in onTap
),
contentPadding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
This is all the code of the page, there is erron in onTap function in suffix. the error message shows that //Invalid constant value//
The Image of the error is here ,

decoration: const InputDecoration
You defined input decoration as constant but on error it wont be constant. Remove const

Related

No change when clicking show password icon

I am developing a mobile application with Flutter. I added a TextFormField to my app like this:
When I click on the icon, the process is running in the back-end. But there is not change in the view.
My codes:
TextFormField(
controller: _passwordController,
keyboardType: TextInputType.visiblePassword,
obscureText: SeePassword,
decoration: InputDecoration(
filled: true,
fillColor: Color.fromARGB(255, 232, 232, 232),
contentPadding: EdgeInsets.symmetric(vertical: 15),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
),
prefixIcon: Icon(Icons.lock, size: 20),
suffixIcon: GestureDetector(
child: SeePassword ? Icon(Icons.remove_red_eye) : Icon(Icons.highlight_off_sharp),
onTap: () {
SeePassword = !SeePassword;
print(SeePassword);
},
),
),
style: TextStyle(fontSize: 20, fontFamily: "Roboto Regular"),
),
How can I solve the problem?
onTap is missing setState to update the ui
onTap: () {
setState((){
SeePassword = !SeePassword;
});
},

Flutter Textformfield should be responsive to typing and error

I've often seen where fields are responsive when users are typing, giving realtime feedback. An example is when I'm typing confirm password or email, if the confirm password or email hasn't matched the password while typing it returns error by marking turning the border of the field red until it matches the correct input. I have written this code, how do I improve the code to be responsive as described.
Widget _buildConfirmPasswordTF() {
return Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
// Text('Password', style: kLabelStyle,),
SizedBox(height: 10.0),
Container(alignment: Alignment.centerLeft, decoration: kBoxDecorationStyle, height: 60.0, child: TextFormField(
validator: ( confirmPassword ){
if ( confirmPassword.trim() != _password.isValidPassword ) {
return null;
} else {
return 'Password doesn\'t match';
}
},
obscureText: true, style: TextStyle(color: Colors.white, fontFamily: 'OpenSans',),
decoration: InputDecoration(border: InputBorder.none, contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(Icons.lock, color: Colors.white,),
hintText: 'Enter Confirm Password',
hintStyle: kHintTextStyle,
errorBorder: OutlineInputBorder( borderSide: BorderSide( color: Colors.red ) ),
focusedErrorBorder: OutlineInputBorder( borderSide: BorderSide( color: Colors.red ) )
),
),
),
],
);
}
This is where I set the hintText
final kHintTextStyle = TextStyle(
color: Colors.white54,
fontFamily: 'OpenSans',
);
This is where I set the labelStyle
final kLabelStyle = TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontFamily: 'OpenSans',
);
This is where I set the border decoration
final kBoxDecorationStyle = BoxDecoration(
color: Color(0xFF6CA8F1),
borderRadius: BorderRadius.circular(10.0),
boxShadow: [
BoxShadow(
color: Colors.black12,
blurRadius: 6.0,
offset: Offset(0, 2),
),
],
);
you need autovalidateMode: AutovalidateMode.onUserInteraction, pass this in textformfield.
You can do that with a Form() providing it a key and a autoValidateMode to make sure the fields have value or that the value is something you except, you can add another field to confirm the passwork or email and compare the value of the field in the onChanged with the value of the other email field to make sure they match.
import 'package:email_validator/email_validator.dart';
final formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool isValid = false;
_emailController.addListener(
() {
//With this, you can "listen" all the changes on your text while
//you are typing on input
//use setState to rebuild the widget
if (EmailValidator.validate(_emailController.text)) {
setState(() {
isValid = true;
});
} else {
setState(() {
isValid = false;
});
}
},
);
Form(
key: formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
children: [
Padding(
padding: EdgeInsets.symmetric(
horizontal: size.width * 0.105),
child: TextFormField(
validator: (value) =>
!EmailValidator.validate(value)
? 'Enter a valid email'
: null,
keyboardType: TextInputType.emailAddress,
textAlign: TextAlign.center,
controller: _emailController,
decoration: kInputDecoration.copyWith(
hintText: 'Enter your email'),
),
),
SizedBox(
height: 18,
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: size.width * 0.105),
child: TextFormField(
obscureText: true,
validator: (value) =>
value.isEmpty ? 'Enter your password' : null,
keyboardType: TextInputType.text,
textAlign: TextAlign.center,
controller: _passwordController,
decoration: kInputDecoration.copyWith(
hintText: 'Enter your password'),
),
),
],
),
),

How to fix the cursor movement with onChange event of flutter?

The cursor is continuously moving to front while typing data in text field.
Before it is not there but once I wired onChange event, it is happening.
My issue:
My code:
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: descriptionController,
decoration: InputDecoration(
labelText: 'Any Details',
hintText: "e.g. Whatever details you want to save",
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
),
),
keyboardType: TextInputType.text,
onChanged: (String string){
setState(() {
if (string != null){
coinOrder.description = string;
}
});
},
),
)
you can use onFieldSubmitted
that will work too
enter image description here
You can use one string variable to store value so this issue will not accrue.
see the below code.
String? details;
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: descriptionController,
decoration: InputDecoration(
labelText: 'Any Details',
hintText: "e.g. Whatever details you want to save",
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
),
),
keyboardType: TextInputType.text,
onChanged: (String string){
setState(() {
if (string != null){
//coinOrder.description = string;
details=string;
}
});
},
),
)

Flutter TextFormField pointer overlapping the text

When using TextFormField in Flutter and having the form prefilled with text, when I tap somewhere on it in order to start editing the text, I get the little rain drop pointer right over the text:
I expect it to appear a little bit below the form. Something like this:
Here is the code:
Container(
height: 40.0,
child: TextFormField(
style: TextStyle(
fontSize: 17,
),
controller: serverAddressController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: new BorderRadius.circular(10.0),
),
),
onChanged: (value) {
serverAddress = value;
changesSaved = false;
},
textInputAction: TextInputAction.next,
onFieldSubmitted: (_) {
FocusScope.of(context)
.requestFocus(daysBackFocusNode);
},
),
),
How do I do it?
you've set Container height to low but didn't reduce content paddings
just add contentPadding property
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 16, right: 16),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16.0),
),
),

How i can put (true icon) while i writing email and verifying from it with flutter

How I can put the icon while typing the email to verifying from email regex and the strength of the password?
TextFormField(
controller: _emailController,
textAlign: TextAlign.end,
decoration: InputDecoration(
hintStyle: TextStyle(fontSize: 16),
hintText: "example#gmail.com",
fillColor: Colors.grey[200],
filled: true,
border: OutlineInputBorder(
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
borderRadius: BorderRadius.circular(14))),
onSaved: (String value) {
email = value;
},
validator: _validateEmail,
keyboardType: TextInputType.emailAddress,
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
child: Text(
"كلمة المرور",
textAlign: TextAlign.right,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
),
),
new TextFormField(
controller: _passwordController,
textAlign: TextAlign.end,
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
prefixIcon: new GestureDetector(
onTap: () {
setState(() {
_obscureText = !_obscureText;
});
},
child: Padding(
padding:
const EdgeInsets.fromLTRB(20, 10, 0, 0),
child: Icon(
_obscureText
? Icons.visibility
: Icons.visibility_off,
color: visi),
)),
hintStyle: TextStyle(fontSize: 16),
hintText: "",
fillColor: Colors.grey[200],
filled: true,
border: OutlineInputBorder(
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
borderRadius: BorderRadius.circular(14))),
onSaved: (String value) {
password = value;
},
validator: _validatePassword,
obscureText: !_obscureText,
),
For email, you can add a listener to your _emailController like:
var _myIcon = Icon.cancel;
void initState() {
super.initState();
// Start listening to changes.
_emailController.addListener(_checkEmail);
}
And then:
_checkEmail() {
bool emailValid = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+#[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(_emailController.text);
if(emailValid)
setState(() {
_myIcon=Icons.ok;
});
}
Now add a prefixIcon to your email field with _myIcon value.
For a password with :
Minimum 1 Upper case
Minimum 1 lowercase
Minimum 1 Numeric Number
Minimum 1 Special Character
Common Allow Character ( ! # # $ & * ~ )
and based on this answer you can use a Regex like this:
RegExp(r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!##\$&*~]).{8,}$').hasMatch(_passwordController.text);