Flutter : multi cursor of TextEditingController in an ExpansionTile widget - flutter

I Have couple of TextEditingControllers in an ExpansionTile.
When i click on one of them, I get multi cursor on all of the TextEditingController with it.
When i put a text in one the TextEditingController (or any action) : it affect all of the other Text in the same ExpansionTile widget.
widget build(){
...
...
ExpansionTile(
title: Container(
width: double.infinity,
child: Text("Parametre vitaux",
style: TextStyle(fontSize: 18),)),
trailing: Icon(Icons.arrow_drop_down, size: 32,
color: Colors.black,),
onExpansionChanged: (value) {
setState(() {
// isExpand=value;
});
},
children: [
listmesures()
]
)
}
Widget listmesures() {
return Container(child:
ListView(
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: [
TextFormField(
keyboardType: TextInputType.phone,
focusNode: f2,
controller: _poidsController,
style: GoogleFonts.lato(
fontSize: 18, fontWeight: FontWeight.bold),
decoration: InputDecoration(
contentPadding:
EdgeInsets.only(left: 20, top: 10, bottom: 10),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(90.0)),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.grey[350],
hintText: 'Poids Kg*',
hintStyle: GoogleFonts.lato(
color: Colors.black26,
fontSize: 18,
fontWeight: FontWeight.w800,
),
),
validator: (value) {
if (value.isEmpty) {
return 'entrez poids';
}
return null;
},
onFieldSubmitted: (String value) {
f2.unfocus();
FocusScope.of(context).requestFocus(f3);
},
textInputAction: TextInputAction.next,
),
SizedBox(
height: 10,
),
TextFormField(
keyboardType: TextInputType.phone,
focusNode: f2,
controller: _tempController,
style: GoogleFonts.lato(
fontSize: 18, fontWeight: FontWeight.bold),
decoration: InputDecoration(
contentPadding:
EdgeInsets.only(left: 20, top: 10, bottom: 10),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(90.0)),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.grey[350],
hintText: 'Temperature*',
hintStyle: GoogleFonts.lato(
color: Colors.black26,
fontSize: 18,
fontWeight: FontWeight.w800,
),
),
validator: (value) {
if (value.isEmpty) {
return 'entrez temperature';
}
return null;
},
onFieldSubmitted: (String value) {
f2.unfocus();
FocusScope.of(context).requestFocus(f3);
},
textInputAction: TextInputAction.next,
),
.....
],
));
}
ExpansionTile: It is a simple and useful widget. This widget lets you create a collapse or expansion view with features similar to ListTile. It is like a ListTile which will expand on tapping the title.
TextEditingController : Connect the TextEditingController to a text field.

I've solved using different focusNode.

$ flutter channel
Flutter channels:
stable
beta
master
first:
flutter channel stable
and
flutter upgrade
running in 2022
channel documentation
https://github.com/flutter/flutter/wiki/Flutter-build-release-channels

Related

How to limit the range of textfield in flutter?

I want to provide a limit to the textfield in flutter. For example, I want that the user can only type numbers between 0-5000. How can I achieve it?
child: TextField(
controller: sliderController,
onChanged: (value) {
setState(() {
amount = value;
sliderAmount = double.parse(value);
});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.fromLTRB(15, 65, 15, 0),
prefixText: '₹',
prefixStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 30,
),
),
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(6),
],
),

Flutter Cupertino Text Field Sizing Error

When a user double taps the text to bring up "Cut Copy Paste", there is a sizing error with the pop-up and the text is cut off. I can't find any questions around this that can help.
See Screenshot and code below
Padding(
padding: const EdgeInsets.only(left: 12.0),
child: CupertinoTextField(
textCapitalization: TextCapitalization.words,
focusNode: titleFocus,
autofocus: true,
controller: titleController,
keyboardType: TextInputType.multiline,
maxLines: null,
cursorColor: Theme.of(context).cursorColor,
onSubmitted: (text) {
titleFocus.unfocus();
FocusScope.of(context)
.requestFocus(contentFocus);
},
textInputAction: TextInputAction.next,
style: GoogleFonts.roboto(
textStyle: TextStyle(
//color: Color(0xff3a4759),
fontSize: 20,
color: Theme.of(context).accentColor,
fontWeight: FontWeight.w700),
),
placeholder: "Sermon Title",
placeholderStyle: TextStyle(
color: Colors.grey.shade400,
fontSize: 20,
fontFamily: 'Helvetica Neue',
fontWeight: FontWeight.w500),
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey.withOpacity(0.0),
),
),
),
),
Try below code hope its help to you
Declare one Controller
final TextEditingController _controller = new TextEditingController();
Your Widget :
Column(
children:[
SizedBox(
height:30,
),
Padding(
padding: const EdgeInsets.all( 16.0),
child: CupertinoTextField(
textCapitalization: TextCapitalization.words,
autofocus: true,
controller: _controller,
keyboardType: TextInputType.multiline,
maxLines: null,
cursorColor: Theme.of(context).cursorColor,
textInputAction: TextInputAction.next,
placeholder: "Sermon Title",
placeholderStyle: TextStyle(
color: Colors.grey.shade400,
fontSize: 20,
fontFamily: 'Helvetica Neue',
fontWeight: FontWeight.w500),
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey.withOpacity(0.0),
),
),
),
),
],
),
Your result screen ->

How can jump to next TextFormField when one is filled?

I am currently building the birthday page for my onboarding.
as you can see in the picture, i have created three textformfields in which the user can type in the day month and year(LengthLimitingTextInputFormatter = 2 for days and months, 4 for years).
How can i make it so when one textfield is filled with the required digits, e.g. 12 for Days, the focus will automatically jump to the next TextFormFiled month without the user clicking on anything besides the required digits.
thanks in advance!
birthday page of onboarding
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
dateComposition,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
),
SizedBox(height: 7),
Container(
margin: EdgeInsets.all(4),
width: dateComposition == "Year"? 73: 55,
child: TextFormField(
textAlign: TextAlign.center,
//Keyboardtype for numbers
keyboardType: TextInputType.number,
//only numbers can be typed in
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(dateComposition=="Year"? 4 : 2),
],
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
autofocus: true,
cursorColor: kPrimaryMagentaColor,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: kTextIconColorDarkBlue),
borderRadius: BorderRadius.circular(15),
),
hintText: dateCompositionHintText,
hintStyle: TextStyle(fontWeight: FontWeight.w600, fontSize: 18.0),
contentPadding: EdgeInsets.only(
left: 10,
right: 10,
top: 10,
bottom: 10,
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
color: kPrimaryMagentaColor,
width: 1.5,
),
),
),
),
),
],
);
}
}
`````
for doing so, you have to assign FocusNode() to your TextField(), then on the onSubmitted method of your TextField, you have to mySecondTextFieldFocusNode.requestFocus().
FocusNode textSecondFocusNode = new FocusNode();
TextFormField textFirst = new TextFormField(
onFieldSubmitted: (String value) {
FocusScope.of(context).requestFocus(textSecondFocusNode);
},
);
TextFormField textSecond = new TextFormField(
focusNode: textSecondFocusNode,
);
You could add the property textInputAction and set it to TextInputAction.next,
TextFormField(
textInputAction: TextInputAction.next,
...
)

how to check if textfield (keyboardType: TextInputType.number) contains number less than 50?

I have a simple TextField to get a number and checking if it contains a number less than 50.
Note : keyboardType: TextInputType.number
Here is my how im doing it:
Column(
children: [
TextField(
controller: textEditingController,
keyboardType: TextInputType.number,
style: TextStyle(color: Colors.white, fontSize: 20),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Number of Questions',
hintStyle: TextStyle(color: Colors.white, fontSize: 17),
),
),
InkWell(
onTap: () {
if (textEditingController.text == '50') {
print(textEditingController.text);
} else {
showInSnackBar();
}
},
child: Text(
'Start',
style: TextStyle(
color: Colors.black,
fontSize: 26,
fontWeight: FontWeight.bold,
letterSpacing: 2,
),
),
),
],
);
#override
void initState() {
textEditingController.addListener(_handleNumber);
}
void _handleNumber(){
if(textEditingController.text.isNotEmpty){
var num = int.parse(textEditingController.text);
if(num <= 50){....}
}
}
You can use the onchanged function callback like this:
Column(
children: [
TextField(
// controller: textEditingController,
keyboardType: TextInputType.number,
style: TextStyle(color: Colors.white, fontSize: 20),
onChanged: (value){
{
if (value == '50') {
print(value);
} else {
showInSnackBar();
}
},
},
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Number of Questions',
hintStyle: TextStyle(color: Colors.white, fontSize: 17),
),
),
],
),

How to emptying hint text when focus in TextFormField Flutter

I want to make the hint text in TextFormField disappear when the field focused. But the hint text won't disappear until I insert a character in the field. I did the following. How can I make the hint text disappear when the field focused? Thanks!
Container buildTextField(bool isObscure, TextEditingController textEditingController, String hintText, bool onlyNumber) {
return Container(
width: double.infinity,
height: 60,
color: MyColors.primary,
alignment: Alignment.center,
child: Padding(
padding: EdgeInsets.only(left: 15, right: 10),
child: TextFormField(
textAlign: TextAlign.center,
keyboardType: onlyNumber ? TextInputType.number : TextInputType.text,
decoration: InputDecoration(
hintText: hintText,
hintStyle: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
),
border: InputBorder.none,
),
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.none,
fontWeight: FontWeight.w600,
),
obscureText: isObscure,
controller: textEditingController,
),
),
);
}
You probably have to add a listener to listen to focus changes:
FocusNode myFocusNode = FocusNode();
myFocusNode.addListener( () {
setState((){});
}
And then, inside your InputDecoration you update the value:
hintText: myFocusNode.hasFocus ? hintText : ''
Use labelText instead,
TextFormField(
controller: fullNameController,
validator: (value) {
if (value.trim().isEmpty) {
return Lang.enterFullName;
}
return null;
},
decoration: InputDecoration(
labelText: Lang.fullName,
prefixIcon: Icon(Icons.person),
),
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
),