onChanged method with setState of TextField does not update the result? - flutter

I am experimenting with flutter and I have a very simple code as follows:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Test extends StatefulWidget {
#override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
static double d = 0;
static TextEditingController editingController = TextEditingController();
#override
void initState() {
editingController.addListener(() {
setState(() {});
});
super.initState();
}
#override
void dispose() {
editingController.dispose();
super.dispose();
}
Calc calc = Calc(d: d, e: editingController);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(25),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
if (value.isNotEmpty) {
d = double.parse(value);
} else if (value.isEmpty) {
d = 0;
}
});
},
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'(^(\d{1,})\.?(\d{0,2}))'),
),
],
),
TextField(
keyboardType: TextInputType.number,
controller: editingController,
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'(^(\d{1,})\.?(\d{0,2}))'),
),
],
),
Text(
'First Text Field Value + 2 = \n${calc.dString()}',
style: TextStyle(fontSize: 30, color: Colors.purpleAccent),
),
Text(
calc.eString(),
style: TextStyle(fontSize: 30, color: Colors.deepOrangeAccent),
),
],
),
),
),
);
}
}
class Calc {
final double d;
final TextEditingController e;
Calc({this.d, this.e});
String dString() {
double result = d + 2;
return result.toStringAsFixed(0);
}
String eString() {
return e.text;
}
}
As we can see I am passing both the text fields' values into another class for some math and getting the results. These results are displayed using the Text widgets.
For the 1st TextField, I have used onChange method, and for the 2nd TextField, I have used TextEditingController.
I get return value for 2nd TextField from the Calc class, but not for the 1st TextField!
I think I am missing something basic and I did not find any solution so far. Can you please help me what am I missing here.

1st of all, you are creating just a single object of Calc,
yes as you can see your 2nd textField update perfectly because it's using TextEditingController but for the 1st one, it just call once and become 2 because of dString(), while on 1st run d becomes 0 passed on Calc.
if you want to use Calc to update text, you can simply put it inside build method like this , i dont suggest it, you can use callBackMethod to handle this or use another TextEditingController.
Hope you get it now
#override
Widget build(BuildContext context) {
Calc calc = Calc(d: d, e: editingController);
return Scaffold(
body: Center(

Your Calc Object is not being affect by setState() call. To run be able to get value of the calcobject, run it in you onChanged() function.

Related

TextField value is not getting updated in Flutter

class UserInputArea extends StatefulWidget {
#override
State<UserInputArea> createState() => _UserInputAreaState();
}
class _UserInputAreaState extends State<UserInputArea> {
#override
Widget build(BuildContext context) {
String convertedText='';
setState(() {
convertedText = Provider.of<UserText>(context, listen: true).convertedText;
print('convertedText :: $convertedText');
});
return Card(
elevation: 10,
child: Container(
padding: EdgeInsets.all(10),
child: TextField(
decoration: InputDecoration(hintText: convertedText.isNotEmpty ? convertedText : 'Enter text'),
keyboardType: TextInputType.multiline,
maxLines: 5,
onChanged: (value){
Provider.of<UserText>(context, listen: false).updateText(value);
},
),
),
);
}
}
Need to update hintText field whenever convertedText gets updated.
This update is happening only if screen refreshed somehow (In Appbar, if click on home-button-icon the data get updated in TextField), Using Provider package that should listen the changes and update the required feild, didnot work. So converted page to Stateful widget and addedd setState() & moved convertedText variable inside it. But still its not working, and not able to figure it out, what is exactly missing here? Anyhelp appreciated. Thanks in advance
Please use TextEditingController class
your code will be somthing like this
class UserInputArea extends StatefulWidget {
#override
State<UserInputArea> createState() => _UserInputAreaState();
}
class _UserInputAreaState extends State<UserInputArea> {
final TextEditingController nameController = TextEditingController();
#override
void initState() {
nameController.text = "test";
super.initState();
//Here you should write your func to change the controller value
Future.delayed(const Duration(seconds: 2), () {
nameController.text = 'test after chabging';
});
}
#override
Widget build(BuildContext context) {
return Card(
elevation: 10,
child: Container(
padding: EdgeInsets.all(10),
child: TextField(
controller: nameController,
decoration: InputDecoration(hintText: convertedText.isNotEmpty ? convertedText : 'Enter text'),
keyboardType: TextInputType.multiline,
maxLines: 5,
),
),
);
}
}
in the write it code above when you will enter the page the hint text will be test after 2 seconds the value will be "test after chabging" without any problem you do not need setState(() {}) I tired it and it works
I think that putting SetState() into the method and calling the method from onChanged could solve the issue. And moving it from Widget build. Something like this:
class UserInputArea extends StatefulWidget {
#override
State<UserInputArea> createState() => _UserInputAreaState();
}
class _UserInputAreaState extends State<UserInputArea> {
String convertedText='';
void _updateField() {
setState(() {
convertedText = Provider.of<UserText>(context, listen: true).convertedText;
print('convertedText :: $convertedText');
});
#override
Widget build(BuildContext context) {
return Card(
elevation: 10,
child: Container(
padding: EdgeInsets.all(10),
child: TextField(
decoration: InputDecoration(hintText: convertedText.isNotEmpty ? convertedText : 'Enter text'),
keyboardType: TextInputType.multiline,
maxLines: 5,
onChanged: (value){
Provider.of<UserText>(context, listen: false).updateText(value);
_updateField();
},
),
),
);
}
}

Using Dart / Flutter, how would I compare the values of two textfields to provide a time duration output?

Imagine I have 2 textfields that a user can input a 24 hour time in the format 1400 for example. Textfield 1 is the start time of a shift and textfield 2 is the end time. Ideally I'd like the duration between the two times to be displayed in a text widget when either textfields onSubmit is called or even if 1 of the textfields loses focus. I'm able to calculate and display the duration of 2 hard coded strings using the intl package with x.difference(y) but I'm struggling to get to this point with the textfields. Thanks for your help.
edit thinking about it after the initial post, the need for textfields isn't 100% required. the two times to compare could come from something like a datetime picker instead. what matters is that i've tried textfields, datetime picker and the time picker but can't arrive a a solution.
import 'package:flutter/material.dart';
class ActivityLog extends StatefulWidget {
#override
_ActivityLogState createState() => _ActivityLogState();
}
class _ActivityLogState extends State<ActivityLog> {
TextEditingController _controller1 = TextEditingController();
TextEditingController _controller2 = TextEditingController();
String duration = '0000';
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Activity Log'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _controller1,
maxLength: 4,
maxLines: 1,
keyboardType: TextInputType.datetime,
decoration: InputDecoration(
labelText: 'Start time',
hintText: 'hhmm',
counterText: '',
),
),
TextField(
controller: _controller2,
maxLength: 4,
maxLines: 1,
keyboardType: TextInputType.datetime,
decoration: InputDecoration(
labelText: 'Finish time',
hintText: 'hhmm',
counterText: '',
),
),
Text('Duration: $duration'),
/*
can't figure out how to get the input from the textfields in the format of
HHmm (hours and minutes) and calculate the duration which is to be displayed
in the text widget above
*/
],
),
),
);
}
}
See if below works for you. Clearly I am not doing any validation, but this could be the start and you can build from that. Here is alsp an image of how it looks here.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _controller1 = TextEditingController();
final _controller2 = TextEditingController();
var _result = "";
String addColon(String s) {
return s.substring(0, 2) + ":" + s.substring(2);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Column(
children: [
TextField(controller: _controller1),
TextField(controller: _controller2),
ElevatedButton(
onPressed: () {
var _d1 =
DateFormat.Hm().parse(addColon(_controller1.text));
var _d2 =
DateFormat.Hm().parse(addColon(_controller2.text));
setState(() {
_result =
'${_d2.difference(_d1).inSeconds.toString()} seconds';
});
},
child: const Text("Diff")),
const SizedBox(height: 50),
Text(_result),
],
),
),
),
);
}
}

Can't update list with setState() in Flutter

I have a list of objects that I can display in a ListView. Now I wanted to implement a search feature and only display the search result. When I try to do it using onChanged on TextField(or even Controller) it doesn't work. I tried to debug and he gets the list updated correctly but he doesn't update the Widget. But when I removed the onChanged and added a button and then called the same method that I was calling on onChanged everything worked.
The goal is to update the widget as the user writes in the text field.
I would be happy to get some help
My full code :
import 'package:flutter/material.dart';
import 'package:hello_fridge/single_ingredient_icon.dart';
import 'package:string_similarity/string_similarity.dart';
import 'entities/ingredient.dart';
class IngredientsContainer extends StatefulWidget {
const IngredientsContainer({Key? key}) : super(key: key);
#override
_IngredientsContainerState createState() => _IngredientsContainerState();
}
class _IngredientsContainerState extends State<IngredientsContainer> {
late List<Ingredient> ingredients;
final searchController = TextEditingController();
#override
void dispose() {
// Clean up the controller when the widget is disposed.
searchController.dispose();
super.dispose();
}
void updateResults(String newValue) {
if (newValue.isEmpty) {
ingredients = Ingredient.getDummyIngredients();
} else {
print("new Value = $newValue");
ingredients = this.ingredients.where((ing) {
double similarity =
StringSimilarity.compareTwoStrings(ing.name, newValue);
print("$similarity for ${ing.name}");
return similarity > 0.2;
}).toList();
ingredients.forEach((element) {
print("found ${element.name}");
});
}
setState(() {});
}
Widget _searchBar(List<Ingredient> ingredients) {
return Row(
children: <Widget>[
IconButton(
splashColor: Colors.grey,
icon: Icon(Icons.restaurant),
onPressed: null,
),
Expanded(
child: TextField(
controller: searchController,
onChanged: (newValue) {
updateResults(newValue);
},
cursorColor: Colors.black,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.go,
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 15),
hintText: "Search..."),
),
),
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: IconButton(
icon: Icon(
Icons.search,
color: Color(0xff9ccc65),
),
onPressed: () {
updateResults(searchController.text);
},
),
),
],
);
}
#override
void initState() {
this.ingredients = Ingredient.getDummyIngredients();
super.initState();
}
#override
Widget build(BuildContext context) {
return Material(
child: Column(children: [
Expanded(flex: 1, child: _searchBar(this.ingredients)),
Expanded(flex: 4, child: IngredientsGrid(this.ingredients))
]),
);
}
}
class IngredientsGrid extends StatelessWidget {
List<Ingredient> ingredients;
IngredientsGrid(this.ingredients);
List<Widget> _buildIngredients() {
return this.ingredients.map((ing) => SingleIngredientIcon(ing)).toList();
}
// const IngredientsGrid({
// Key? key,
// }) : super(key: key);
#override
Widget build(BuildContext context) {
this.ingredients.forEach((ing) => print(ing.name! + ","));
return ListView(
children: <Widget>[
GridView.count(
crossAxisCount: 4,
// physics: NeverScrollableScrollPhysics(),
// to disable GridView's scrolling
shrinkWrap: true,
// You won't see infinite size error
children: _buildIngredients()),
// ...... other list children.
],
);
}
}
Moreover, I keep getting this Warning :
"Changing the content within the composing region may cause the input method to behave strangely, and is therefore discouraged. See https://github.com/flutter/flutter/issues/78827 for more details".
Visiting the linked GitHub page wasn't helpful
The problem is that while you are correctly filtering the list but your TextController is not getting assigned any value.
So, no value is getting assigned to your TextField as the initial value and hence the list again filters to have the entire list.
To solve this just assign the TextController the newValue like this.
void updateResults(String newValue) {
if (newValue.isEmpty) {
ingredients = Ingredient.getDummyIngredients();
} else {
print("new Value = $newValue");
ingredients = this.ingredients.where((ing) {
double similarity =
StringSimilarity.compareTwoStrings(ing.name, newValue);
print("$similarity for ${ing.name}");
return similarity > 0.2;
}).toList();
ingredients.forEach((element) {
print("found ${element.name}");
});
}
// change
searchController = TextEditingController.fromValue(
TextEditingValue(
text: newValue,
),
);
setState(() {});
}
If it throws an error then remove final from the variable declaration, like this :
var searchController = TextEditingController();

flutter, How to copy subranges of multi-step input

When copying text from TextField, how should I copy the subrange in case of multi-step input (composing mode)?
For example, the following code
import 'package:flutter/material.dart';
class SampleWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(home: HomeWidget());
}
}
class HomeWidget extends StatelessWidget {
TextEditingController _controller1 = TextEditingController();
FocusNode _focusNode1 = FocusNode();
TextEditingController _controller2 = TextEditingController();
FocusNode _focusNode2 = FocusNode();
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
children: [
TextField(
focusNode: _focusNode1,
controller: _controller1,
),
TextField(
focusNode: _focusNode2,
controller: _controller2,
),
RaisedButton(
child: Text("copy text"),
onPressed: () {
if(_focusNode1.hasFocus) {
_controller2.text = _controller1.text;
_focusNode1.unfocus();
_focusNode2.requestFocus();
} else {
_controller1.text = _controller2.text;
_focusNode2.unfocus();
_focusNode1.requestFocus();
}
}),
],
),
),
),
);
}
}
void main() {
runApp(SampleWidget());
}
It is possible to copy text in English "abcd" in TextField.
but in case of multi-step input(composing mode), copying text is not possible.
Korean is a combinatorial language, that means characters such as '가', '구', and '거' can be written ordinarily 'ㄱ' + ‘ㅏ’, ‘ㄱ’+ ‘ㅜ’ and ‘ㄱ’ + ‘ㅓ’.
However, the code moves to the next character range just after 'ㄱ'.
(I want the code to keep until ‘ㄱ’ is combined with ‘ㅏ’)
How can I solve this problem?

Get Instance of controller attached to a TextField in a loop

I am trying to create a page with more than one TextField widgets in flutter. Number of textfields to display are determined at runtime. while creating those textfields a controller from the controller array is attached to each textfield.
Everything is working as expected. but doen't matter which textfield i click, i always get the callback of all the textfields in a single onChanged function.
Therefore, i am not able to detect that value of which textfield is changed.
Please refer to code for more insight.
In-Short, i just want to know how to get the instance of textfield controller which is currently active.
Could somebody please let me know what i am doing wrong.
Thanks
List<TextEditingController> _controllers = <TextEditingController>[];
int controllersAttached = -1;
#override
Widget build(BuildContext context) {
controllersAttached += 1;
for (int i = 0;i < fibProvider.quesTextList.length;i++) ...<dynamic>[
TextField(
autofocus: false,
cursorColor: const Color(0xFFD8D8D8),
maxLines: 1,
textAlign: TextAlign.center,
controller: _controllers[controllersAttached %fibProvider.numberOfBlanks],
onChanged: (String data) {
// here i can distinguish between the controllers depending upon the text
entered in them. but if there are 3 textfields and the same data is
entered in all the three then this code only returns the first
controller with the matched value.
final int index = _controllers.indexWhere(TextEditingController item) {
return data.compareTo(
item.text.toString()) == 0;
});
},
),
]
}
You can just pass i variable to onChanged callback inside collection-for:
class _MyHomePageState extends State<MyHomePage> {
final int _fields = 10;
List<TextEditingController> controllers;
#override
void initState() {
super.initState();
controllers = List.generate(_fields, (i) => TextEditingController());
}
#override
void dispose() {
controllers.forEach((c) => c.dispose());
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title)
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
for (int i = 0; i < _fields; i++)
TextField(
controller: controllers[i],
onChanged: (value) {
final controller = controllers[i];
print('Current field index is $i and new value is $value');
},
),
],
),
)
);
}
}