Im trying to do a register page and everything is working but the logic for the onpressed property. This is my code:
onPressed: _usernameController.text == "" || _emailController.text == "" || _passwordController.text == "" || _passController.text == "" ? null : () {
setState(() {
_isLoading = true;
});
register(_usernameController.text, _emailController.text, _passwordController.text, _passController.text);
}
the button stays null even if you fill all the required fields as if the condition remained true. I know the code is right because I have a similar logic in my login page, and also whenever I refresh with the fields already full it magically lights up the button and everything works. What could be the problem?
You can use Form widget. Form widget has onChange property also you can listen to all states. Form widget has other property, the key is validated and control your form state. Don't forget must be declared validator property.
Same example:
Full code: Form Validattor
Please make one method like below..
void _registration(){
//add register code here instead of write at onPressed event
}
and call this method in onPressed event like this...
onPressed: _registration
Related
I'm new in flutter and I'm trying to implement something like radio group with custom buttons in Android. For this I created StatefulWidget, which hold list of selectable buttons. For every button I was set press listener where I do something like this:
setState(() {
buttons.forEach((button) => button.isSelected = false);
buttons[selectedButtonIndex].isSelected = true;
});
And then my CustomButtonWidget changes color depending on the parameter isSelected .
All this works well. However, I have an additional requirement. I need my RadioGroupWidget to return the selected button type. For this I created a callback :
final ValueChanged<ButtonType> onChanged;
And now my button press listener looking like this:
onTap: () {
setState(() {
buttons.forEach((button) => button.isSelected = false);
buttons[selectedButtonIndex].isSelected = true;
});
onChanged(buttons[selectedButtonIndex].type);
}
Next I get this type of button in my other widget which is using RadioGroupWidget:
CustomRadioGroup(
onChanged: (value) {
setState(() {
buttonType= value;
});
}),
)
As you can see I call again setState. This is what leads to the problem. But I need to do this, because I need to update the state of another widget (for example let's call it InfoWidget) depending on the selected button.
After all these manipulations, the state of the InfoWidget is updated correctly, but the state of the selected button in the RadioGroupWidget does not change. I tried to debug this and I see that at first the parameter isSelected is set to true for the desired button, but after the state of the button I selected is not updated, because its parameter isSelected becomes false. And I don't understand why this is happening.
Please help, I am completely confused.
I'm giving my users the option to sent in an email. Once they enter their email address, I would like to simply add the text "Email has been sent to user#email.com".
I'm not quite sure on how to do this, and was hoping someone can point me in the right direction. Thanks in advance!
For this, to work you need to have a boolean value that triggers your Text widget using Visibility. Suppose the boolean variable is isEmailSent and set its initial value to be false.
bool isEmailSent = false;
Now the user has entered his/her email address and then on onTap you must change the state of the boolean value like this:
onTap:(){
setState((){
isEmailSent = true;
});
}
When the state has been changed then with the help of Visibility and Text widgets you could do something like this:
Visibility(
visible: isEmailSent,
child: Text(
"Email has been sent to user#email.com"
)
)
Now, whenever the isEmailSent is true your text will be visible else not.
EDIT
How would I change the user#email.com to the input?
Again you also need to have a String variable for this to work. This time the name of the variable will be emailId. Initially set its value as an empty string like this:
String emailId = "";
Then change the state of the variable using setState, like this:
setState((){
emailId = _textController.text; //where _textController is the controller of your input
});
Now inside your Text do something like this:
Text(
"Email has been sent to "+emailId
)
create onTap() as the user enters the email address. in onTap define AlertDialog that will give an popup dialog showing text "Email has been sent to user#email.com "
I have a checkbox that I would like to disable a field when it is unchecked. How would I do that? This it the current checkbox code I have so far...
child: CheckboxListTile(
title: Text("Auto Calculate Big Blind"),
value: _autoCalcBigBlind,
onChanged: (bool value) {
setState(() {
if (value == false) { //focus on the big blind text field
_bigBlindFocusNode.requestFocus();
} else {
//first check small blind is not null
if (_textEditControllerSmallBlind.text == null || _textEditControllerSmallBlind.text == '0'){
//if the field is null/0, can't double it, so focus on it
_smallBlindFocusNode.requestFocus();
}else{ //the big blind value = double the current small blind value. we know small blind field is not null
_textEditControllerBigBlind.text = (int.tryParse(_textEditControllerSmallBlind.text) * 2).toString();
**//at this point, i want a specific text input field disabled. how to do that?**
}
}
You need to toggle the value of the enabled property of TextFormField or TextField. If the enabled is false it would disable the TextFormField or TextField.
Create bool member such as bool _isTextFieldEnabled = false; in the State class of a StatefulWidget & set _isTextFieldEnabled to true when you want to disable the TextField by calling setState.
Checkout this DartPad sample I created to demonstrate this: DartPad
Scenario: I have number of children in Listview.builder. For every children there is button which invokes TTS(text to speech). I wanted when any one of child is pressed rest all children should be in Listview.builder un-clickable till pressed child finishes its TTS.
I got answer from google like absorb pointer, ignore pointer to solve this.
But i dont know how to implement above scenario using these widgets.
Store a bool in the State of the class enclosing the ListView.builder. This bool should store if one of the children is currently doing its TTS. If it is true you should set all of the onPressed(or equivalent) method to null to prevent other taps from triggering an action. Ex:
bool hasBeenClicked = false;
void yourTTSMethod() {
setState(() {
hasBeenClicked = true;
});
... //Do normal method body
setState(() {
hasBeenClicked = true;
});
}
//In build method with each `List` item:
GestureDetector(//Just for sample, use whatever click detector you're currently using
onTap: hasBeenClicked ? null : yourTTSMethod
)
i want to change variable values when the widget is loading the data from the web,
just want to do something like this:
_playid = _notes[1].id;
title = _notes[1].title;
but wherever I put it, i get an error,
I tried to put it in a set state inside listview builder, but no luck since I don't want it with onPress or on tap methods
could someone help, please?
i just found out,
simply we have to use setstate in fetch inside initstate
void initState() {
title = " ";
fetchNotes().then((value) {
setState(() {
_notes.addAll(value);
_playid = _notes[0].numb;
});
});
}