I'm building an application on flutter and i have a problem because of the list.
In flutter, I made an empty list, in this list filling by users. An assignment takes place when the user enters a value, but this always happens when a value is entered. If no value is entered, it reflects the previous value in the list. What I want is for the program to create an Alertdialog if no string value is entered in the TextField.
Here is my List (TaskData)
enter image description here
And my TextField
enter image description here
Try adding a errorText:
Full example
final _text = TextEditingController();
TextField(
controller: _text,
decoration: InputDecoration(
labelText: 'Enter the Value',
errorText: _validate ? 'Value Can\'t Be Empty' : null, //here
),
),
Inside a Button use:
bool _validate = false;
RaisedButton(
onPressed: () {
setState(() {
_text.text.isEmpty ? _validate = true : _validate = false;
});
},
child: Text('Submit')
textColor: Colors.white,
color: Colors.blueAccent,
),
try this in your addTask method
void addTask() {
if (name = '') showMyAlertDialog();
else {nameList.add(name);
notifyListeners();}
name = '';
}
Related
Whenever I am defining the obscureText property of the TextFormField a suffix icon is automatically added at the end but after pressing it nothing happen,
The prefix icon is defined by me but the suffix icon is added automatically.
How to make that default icon interactive so that the password will be shown or hidden accordingly without adding our own suffix icon?
TextFormField(
style: TextStyle(color: Colors.black),
obscureText: true,
decoration: InputDecoration(
hintText: "Enter Password",
labelText: "Password",
prefixIcon: Icon(Icons.lock),
),
),
suffixIcon: IconButton(
onPressed: () => ShowHideFuncion(),
icon: Icon(Icons.YourIcon),
),
you need to make a variable in your widget of type bool (e.g. obscure). You then change the obscureText parameter from being true to being the variable you made.
Then add a suffixIcon with an IconButton() (see more here). In the IconButton()'s onPressed parameter set the variable to be not equal to the variable. (e.g. obscure = !obscure). There is a possibility you might need to make your widget a stateless widget and call setState(() => obscure = !obscure) if it doesn't update automatically.
The final code should look something like this:
Icon icon = Icon(Icons.visibility);
bool obscure = true;
TextFormField(
style: TextStyle(color: Colors.black),
obscureText: obscure,
decoration: InputDecoration(
hintText: "Enter Password",
labelText: "Password",
suffixIcon: IconButton(
onPressed: () {
setState(() {
if (obscure == true) {
obscure = false;
icon = Icon(Icons.visibility_off);
} else {
obscure = true;
icon = Icon(Icons.visibility);
}
});
},
icon: icon
),
),
),
This is because you are not changing the state of the obsecuretext.
In order to do that please declare variable of boolean type in main class.
bool isPassword = false;
Now in obscureText use the variable which you declared in main class as below.
`obscureText = isPasswordVisible`
Now make a Icon button to change the state of the boolean value by using set state as below.
suffixIcon: IconButton(
onPressed: () {
setState(() {
isPasswordVisible = !isPasswordVisible
});
},
icon: isPasswordVisible ? Icon(Icons.any_icon_you want) : Icon(Icons.any_Icon_you_want_to_display_when_obsecure_is_false)
),
I am developing e com application, when user click on quantity label want to show dialog box for select how many quantity he want to buy.
I tried to wrap number picker inside Alert dialog. It show alert dialog, but the problem is not updating value when scroll
I can I use Number Picker for that?
Use a drop button combo widget. The DropdownButtonFormField holds a collection of YourClassView type objects held in the list listStatusMenuItems. The _currentStatusComboView variable holds the selected value. It is assigned on the onChange event. I use a restful call to load the listStatusMenuItems.
List<DropdownMenuItem> listStatusMenuItems = <DropdownMenuItem>[];
StatusComboView _currentStatusComboView;
_loadStatusCombo() {
Provider.of<Api>(context, listen: false)
.getYourClassViews()
.then((listView) {
setState(() {
listStatusMenuItems =
listView?.map<DropdownMenuItem<YourClassView>>((item) {
return DropdownMenuItem<StatusComboView>(
value: item, child: Text(item.displayValue));
}).toList();
});
});
#override
void initState() {
super.initState();
_loadStatusCombo();
}
DropdownButtonFormField<YourClassView>(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0),
),
),
filled: true,
hintStyle:
TextStyle(color: Colors.grey[800]),
hintText: "Select a Value",
fillColor: Colors.orange),
items: listStatusMenuItems,
isDense: true,
isExpanded: true,
value: this._currentStatusComboView,
validator: (value) =>
value == null ? 'field required' : null,
onChanged: (StatusComboView value) {
setState(() {
this._currentStatusComboView = value;
});
}),
I'm having a weird issue with a TextFormField, it cuts off the last input. If I type something like hello, it's gonna cut off the o and save hell. if I give it a space like hello , it's gonna save hello. Has anyone been through the same issue and know how to fix it?
here is my code:
String messageText;
_ChatScreenState() {
_formKey = GlobalKey<FormState>();
_scrollController = ScrollController();
messageText = "";
}
Widget messageTextField() {
return SizedBox(
width: deviceWidth * 0.55,
child: TextFormField(
style: TextStyle(color: Colors.white),
validator: (text) {
if (text.isEmpty) {
return 'Digite sua mensagem';
}
return null;
},
onChanged: (text) {
_formKey.currentState.save();
},
onSaved: (text) {
setState(() {
messageText = text;
});
},
cursorColor: Colors.white,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Digite uma mensagem',
hintStyle: TextStyle(color: Colors.white.withAlpha(100))),
autocorrect: false,
),
);
}
I do not think save() is intended to be used with onChanged.
If you place a button somewhere and perform _formKey.currentState.save(); within onPressed of the button, you should see the entire text value being saved.
I've made a program where the user can create an account, but I've placed some restrictions on the textfields (like for the First Name, it can only accept a string made of A-Z or a-z).
I want an "error text" to show under the textfield if the input is wrong (like if the user types "John1"), but I don't know how to do this. The simple way would be to use the onEditingComplete but this only works if the user taps on the keyboard the "done" key.
Is there a way to make the "error text" appear when the focus changes from one textfield to another?
You can use the validator property of the textfield to do this. It is recommended to encapsulate your fields in a Form() widget and then use the FormTextField() widget to implement your validations, this will enable an error to be shown at the bottom of the text field, if focus changes, or submission is attempted.
Example:
TextFormField(
onSaved: (String value) {},
validator: (String value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
)
The error will appear below in red fonts.
If you want to show error text whenever text/value changed you can do this.
In TextField 'onChanged' validate the user input using some regex and check for error ,if there is an error using TextField decoration 'errorText' property you can show required error.
Widget phoneNumberTextField() {
return Padding(
padding: EdgeInsets.fromLTRB(25, 10, 25, 10),
child: TextField(
style: TextStyle(color: Colors.white),
controller: _phoneNumberController,
maxLength: Constants.PhoneNumberTextFieldMaxLength,
onChanged: (value) {
_phoneNumberErrorText =
validatePhoneNumberTextField(_phoneNumberController.text);
_hasPhoneNumberError = _phoneNumberErrorText.isNotEmpty;
setState(() {});
},
keyboardType: TextInputType.number,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.green, width: 1.0),
),
errorText: _hasPhoneNumberError ? _phoneNumberErrorText : null),
),
);
}
String validatePhoneNumberTextField(String text) {
String errorText = '';
if (text.length < 1) {
errorText = 'Phone Number should not be empty';
} else if (text.length != 10) {
errorText = 'Please enter valid phone number.';
}
return errorText;
}
As displayed in the GIF below, the TextFormField i am using is entering the values backwards. I have set the TextDirection property to ltr as well but it did not change anything. Other TextFormFields dont seem to be having this issue. The entered text is being sent back to another screen using Navigator.pop and it is sent in the same backwards manned.
Weird TextFormField
Code for the textFormField causing the issue:
TextFormField(
// validator: (String value) {
// return value.isEmpty ? "task must have a name" : null;
// },
textDirection: TextDirection.ltr,
maxLength: 100,
controller: newcontroller, // Just an ordinary TextController
onChanged: (value) {
setState(() {
newcontroller.text = value;
});
},
decoration: InputDecoration(
errorText: _validate // Just a boolean value set to false by default
? 'Value Can\'t Be Empty' : null,
labelText: "name of task"
),
style: TextStyle(height: 1.2, fontSize: 20, color: Colors.black87)
)
You don't have to set text in newcontroller.text when onChanged is called.
text enter in your TextFormField is assigned by default to newcontroller.
You are getting this error because for this piece of code,
So, try to remove below code
setState(() {
newcontroller.text = value;
});
you can send whatever you want in Navigator.pop(context,whtever you want to pass)
TextFormField(
// validator: (String value) {
// return value.isEmpty ? "task must have a name" : null;
// },
textAlign: TextAlign.end,
maxLength: 100,
controller: newcontroller, // Just an ordinary TextController
onChanged: (value) {
print(value);
},
decoration: InputDecoration(
errorText:
_validate // Just a boolean value set to false by default
? 'Value Can\'t Be Empty'
: null,
labelText: "name of task"),
style:
TextStyle(height: 1.2, fontSize: 20, color: Colors.black87),
),
MaterialButton(
onPressed: () {
Navigator.pop(context, newcontroller.text);
},
child: Text("GO BACK!"),
),
replace onChanged with onEditingComplete.
onEditingComplete: (value) {
newController.text = value;
FocusScope.of(context).unfocus(); //use this to dismiss the screen keyboard
}
Just remove the onChanged function. There is no need for it.
onChanged: (val) {
if (val.isNotEmpty) {
setState(() {
// remember to not add your controller value in onchanged
_messageController.text = val;
isShowSendButton = true;
});
} else {
setState(() {
// remember to not add your controller value in onchanged
_messageController.text = val;
isShowSendButton = false;
});
}
Unfortunately, onEditingComplete doesn't always fire (i.e. on a focus change), so if you are editing a text variable you can still use onChanged as the most reliable way to update changes, but you should not use the onChanged value parameter, nor should you use setState (which results in the reversed text).
TextEditingController tec = TextEditingController(text: myText);
return TextField(
controller: tec,
onChanged: (value) {
myText = tec.text;
},
);