How to retrieve text data from Text Widget in flutter - flutter

I am a beginner in flutter, how do i get or retrieve data from text widget. I have done some practice for example
Text("18:00"),
var data=Text("18:00");
print(data);
At the end instead of getting "18:00" i am getting "Text("17:05")" at the output.

define this
var data = "18:00";
Text("18:00"),
print(data);

To retrieve data fom a Text widget,
var data=Text("18:00");
print(data.data);
This will give you the output "18:00" as per your requirement.

what do you want to do? if you want to get an input from user you should use text field or forms by setting controller to it and get data from controller.
final myController = TextEditingController();
TextField(controller: myController),
var data=mycontroller.getText();
or if you want just get the text of text widget just set that text to a variable then pass it to widget.
String data="18:00"
Text(data);

Related

I want string to be a widget in flutter

I mean that I want to get scores code from api:
Well their is a widget called Text()
provide the String as it text then it will accept it
body: Text("any text")
Use Text widget with string text
body: Text(text),
If this is for learning purposes and you want to know how to use a widget as a parameter you can do the following:
Text text = Text("Something"); // This way you specify exactly the type `Text`
or
Widget text = Text("Somethig"); // This way you can associate any widget to text
or
var text = Text("Something"); // var can be used with any type
Then you can use whenever you want in your code as long as it require a Widget as a child whitout any need for casting.
For you to create a widget that you can associate values to it you can go for something like this:
Widget myText({String value}) => Text(value);
There are many approaches depending on your need, and I strongly advice you to follow some tutorials for beginners first like Angela Yu bootcamp or any others and also use Flutter documentation.
Try the following code:
body: Text(text),
String is not and will not be a widget. What you are probably looking for is the widget to display a string in your UI: Text.
In your exemple, you can correct it like this :
return Scaffold(
body: Text("Hello World")
);
See exemple on DartPad

is it possible to automatically refresh component when getx value changed flutter

I am using getx get: ^4.3.8 as the state management component in flutter, when I using this code to update the controller value:
var expand = controller.newTaskExpanded.value;
controller.newTaskExpanded(!expand);
I found the component did not refresh, then I have to invoke the getx refresh function like this:
controller.refresh();
this is the controller properties define:
class HomeController extends GetxController {
List<Widget> widgetsList = List.empty(growable: true);
List<Todo> tasks = List.empty(growable: true);
var newTaskExpanded = true.obs;
var completedTaskExpanded = false.obs;
}
is is possible to auto refresh the flutter component when controller properties value changed? should I invoke the refresh function every time change the value of getx controller?
Reactive programming with Get is as easy as using setState.
Let's imagine that you have a name variable and want that every time you change it, all widgets that use it are automatically changed.
This is your count variable:
var name = 'Jonatas Borges';
To make it observable, you just need to add ".obs" to the end of it:
var name = 'Jonatas Borges'.obs;
And in the UI, when you want to show that value and update the screen whenever the values changes, simply do this:
Obx(() => Text("${controller.name}"));
For more , refer: https://pub.dev/packages/get/example
wrap you UI with Obx builder like
appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))),
it will auto refresh the UI on value change
if you want to refresh the UI manually then use GetBuilder and when you need to update the UI just call controller.update();
As #Muhammad Sherad explained, your widget code should be wrapped with Obx() so that you widget can look for variable change and re-render the widget.

I have a stored List data and how can I localize it in flutter?

I can translate the text inside the widget using AppLocalizations.of(context)!.translated and it work fine
However, when I want to localize a stored List<> data which will be used in listView Builder, I am not able to do so, since it need (context).
My question is how to localize a stored list data(outside widget) and pass to list view builder?
Thanks for your help.
I think you should do 2 things
In case if you have a list in another file, then it should be static like this:
class Presets {
static var presetData = [];
}
after you loaded up your list, you can do these steps:
First,
Create Widgets as children like this:
List<Widget> presetsLoadUp() {
List<Widget> children = <Widget>[];
for(i < yourList.length){
children.add(Text(yourList[i]));
}
return children;
}
You can create your own unique widget inside the children.add method.
Secondly,
Create the list:
...
child: ListView(
children: presetsLoadUp(),
),
If anyone has similar problem, please take it for reference.
I just build another widget to solve this.
What I do is :
build another widget,
then put the list data inside and
return ListViewBuilder.
Everything is same,
just put your list data inside another widget can solve the problem

Animating Text widget content based on change of variable

How can I animate the change from a Text widget with a particular String to a new String which is dynamic, i.e., I don't know what it'll be beforehand ?
For example, let the widget be Text('a'). Later I want it to animate to Text('b'), maybe a after a button being clicked by a user.
Store the user input in a variable and display it in the Text widget. For example,
var input = value;
inside the widget you can use Text('$input') or directly Text(input) to display it

How to retrieve text in Text Field from firebase when it is editing? - Flutter

I added an edit feature to my app, but the content of those posts is more than 100 words, when the text field shows up, it will be empty, I don't want that, I want to show the text field with old content, so they can edit or add content to the post, Is there any way I can retrieve the data from firebase directly to textField?
You will have to use TextEditingController
final TextEditingController myTextController = new TextEditingController();
...
...
this.myTextController.text = firestoreData ?? ''; //incase the data is null, it will be an empty string
...
...
TextField(
controller: this.myTextController,
...
),