storing input textfield on flutter - flutter

Im implementing voice search on flutter. Here I have an icon for voice search and when clicked, a function is called and the resulting string is stored in String _text.
IconButton(
onPressed: (){
_listen();
})},
icon: Icon(_isListening ? Icons.mic : Icons.mic_none, ),
),
I also have a searchbar with a controller txt where the inputs are stored
TextField(
controller: txt,
decoration: InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: 'What are you looking for?',),
onSubmitted: (txt){}
my problem is how can i store the _text string to the searchbar when they use voice search?

Related

How to change suffixIcon to custom icon in text field in flutter

My requirement is need to use some different icon in place of suffixIcon in flutter but using suffixIcon there are in build icons like :
suffixIcon: IconButton(
//eye icon
color: Color(0xFF919191),
onPressed: () {
//for keyboard to hide
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
//for keyboard to hide
setState(() {
isHidePassword = !isHidePassword;
});
},
icon: Icon(isHidePassword
? Icons.visibility
: Icons.visibility_off)),,
How to use custom icon instead of visibility & visibility_off in suffixIcon in text field in flutter.
Attaching image for more understanding what i have tried using suffic icon is as below
what i need to achieve is
Any help is appreciated!
Use outlined Icon data for it.
Replace Icons.visibility with Icons.visibility_outlined and
Icons.visibility_off with Icons.visibility_off_outlined,
icon: Icon(
isHidePassword
? Icons.visibility_outlined
: Icons.visibility_off_outlined,
),
check visibility_outlined-constant
Try below code hope its help to you.
declare bool variable:
bool obsecureText = true;
Hide/Unhide Password function
void showPassword() {
setState(() {
obsecureText = !obsecureText;
});
}
Your Widget:
TextFormField(
obscureText: obsecureText,
decoration: InputDecoration(
suffixIcon: IconButton(
onPressed: showPassword,
icon: Icon(
obsecureText ? Icons.visibility_off : Icons.visibility,
),
),
prefixIcon: Icon(
Icons.vpn_key,
),
border: OutlineInputBorder(),
labelText: 'Password',
hintText: 'Enter Password Here',
),
),
Your result screen hide password ->
Your result screen display password->

Flutter TextFormField Obscure Password

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)
),

How to make TextField which has 2 hintText?

I have ui of text field from Figma.
There are 2 hint texts where 1st hintText is located at the beginning of textfield and other one is at the end.
As can be seen, trailing is hint text which is disappearing when user inputs number.
If you have any idea please share how to make this kind of text field)
Try this:
TextField(
textAlign: TextAlign.end,
decoration: InputDecoration(
prefixIcon: Padding(
padding: EdgeInsets.all(15), child: Text('Prefix')),
border: OutlineInputBorder(),
hintText: "Cym",
),
),
You can just use the trailing for the hint and can you onChanged method to handle changes.. Like you will be managing your text on the behavior of input. For example if the input is empty then you text will be something like "A" if you input something text will be changed.
TextFormField(
decoration: InputDecoration(
suffix: Text(text)
),
onChanged: (value) {
// handle or change your text here
if(value.isEmpty){
setState(() { text = "A"});
}
else{
setState(() { myState = "any text"});
}
},
)

Flutter error: W/IInputConnectionWrapper: getExtractedText on inactive InputConnection

I have a Flutter error:
W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
It occurs when I clear the text in the TextField from TextEditingController input.
This is my clear text function, it works:
_onClear() {
setState(() {
WidgetsBinding.instance
.addPostFrameCallback((_) => _textController.clear());
});
}
child: TextField(
maxLines: 2,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.done,
autofocus: false, //setting this to true,false, or removing has no effect
controller: _textController,
textAlign: TextAlign.left,
decoration: InputDecoration(
labelText: "Enter",
hintText: "Enter",
hintStyle: TextStyle(
fontStyle: FontStyle.italic, color:
Colors.blueGrey),
suffix: IconButton(
icon: Icon(Icons.cancel),
onPressed: _onClear,
),
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25.0)),
),
),
)), //textfield
The user can type in the textfield, click on an image (UrlLauncher), and return to the form and still see the same text input in the text field. Everything works the ways it should, and the app runs without other errors.
Is this error something I have to worry about, will I still be able to submit the final app to the store with an error, or does it have to be correct? I have no clue how to do that. Pardon my lack of knowledge, I am just trying to learn all this and am stumped. There appears to be no other answers to this question regarding Flutter.
If you are using PHP Script for Insert Data then may be you are missing quote like '$FiledName' .Beacause this worked for me after correct it.Thanks

How to update keyboardType in Flutter (in the app)?

In normal Android development, you can change the keyboard type dynamically but I can't seem to find a way to do so in Flutter.
TextInputType roomKeyboardType = TextInputType.text; // right on top of the build function
My custom form field:
Padding(
padding: const EdgeInsets.all(8.0),
child: FormBuilderTextField(
attribute: "room",
controller: widget.controllers[1],
keyboardType: roomKeyboardType,
decoration: InputDecoration(
filled: true,
labelText: "Room",
prefixIcon: Icon(Icons.location_on, color: Colors.black),
suffixIcon: IconButton(
icon: Icon(Icons.keyboard, color: Colors.black),
onPressed: () {
if (roomKeyboardType == TextInputType.text) {
roomKeyboardType = TextInputType.number;
return;
}
roomKeyboardType = TextInputType.text;
},
)),
validators: [
FormBuilderValidators.required(
errorText: "Please enter the room number",
),
],
),
),
Right now, I have something that looks like this with a keyboard IconButton on the end of the form field. When I click on it, I expect my keyboard type to change, yet when I do, nothing happens. It's supposed to toggle between text and number keyboard types and it only stays on text as that's what I have the variable initially set to. The variable I know is changing, it's just the fact that Flutter probably isn't remaking the widget so the keyboard stays the same. Is there any way to get around this and am I able to change the keyboardType dynamically?
you can change your keyboard type in TextFieldWidgets and many input widgets
like this:
TextField(
keyboardType: TextInputType.number,
hintText: '6 Digit-Code',
),