在buildMethod中分配TextEditingController的值 [英] Assign value of TextEditingController inside buildMethod
问题描述
我有一些疑问,哪些是创建TexEditingController的正确方法;
i have some questions about which is the correct way of creating TexEditingController;
假设我想创建一个带有固定文本的控制器,那么我可以这样做:
Assuming that i want to create a controller with a fixed text, so i can do like this :
TextEditingController bioEditorController = new TextEditingController(text:"dummy");
现在我的问题是这个
如果我使用有状态的小部件,则可以通过执行以下操作来创建此控制器广告并为其分配初始文本:
if i'm usign a stateful widget i can create this controller ad assign an initial text by doing this :
TextEditingController bioEditorController;
@override
void initState() {
bioEditorController = new TextEditingController(text: "dummy");
super.initState();
}
但是如果我不使用有状态的小部件,做这样的事情是否正确:
but if i'm not using a stateful widget is it correct to make something like this :
@override
Widget build(BuildContext context) {
TextEditingController bioEditorController =
new TextEditingController(text: controller.profile.value.bio);
我的意思是在build方法中创建此控制器是正确的,如果我确实喜欢它的话,但是我认为这可能不是执行此操作的最佳方法,另外请注意,我知道控制器也应该被丢弃....
What i mean is it correct to create this controller inside the build method, if i do like this it works , but i think that probably is not the best way of doing this things, also becauze i know that controller should also disposed....
我真的需要一些帮助来澄清这一点.谢谢
I really need some help about clarifying this. Thanks
推荐答案
您正确地认为在build方法中这样做并不理想.与 initState
相比,您对build方法运行多少次的控制较少.
You are correct in assuming that doing that in the build method is not ideal. You have way less control over how many times the build method runs vs initState
.
通常来说,在处理TextEditingControllers时需要一个有状态的小部件(或 hooks ),如果您使用 GetX 状态管理,那绝对好,最好不要仅仅因为您需要一个 TextEditingController
而使有状态窗口小部件麻烦.
And while it's generally true that you would need a stateful widget when dealing with TextEditingControllers (or hooks), if you use GetX state management it's absolutely fine, and preferred to not bother with a stateful widget just because you need a TextEditingController
.
这种方式的另一个好处是可以很容易地从应用程序的任何其他位置访问 TextEditingController
的值.
Another benefit of this way is very easy access to the value of the TextEditingController
from anywhere else in the app.
这是一个简单的例子.在该类中,您可以保留所有 TextEditingControllers
.
Here's a quick example. This is a class where you can keep all your TextEditingControllers
.
class TextController extends GetxController {
RxString textfieldString = ''.obs; // observable String
TextEditingController textController;
// this onInit replaces initState of a stateful widget
@override
onInit() {
super.onInit();
textController = TextEditingController(text: 'dummy');
// adding a listener that automatically updates the textfieldString with the textfield value
textController.addListener(() {
textfieldString.value = textController.text;
debugPrint(textfieldString.value);
});
}
}
在实际使用控制器之前或在任何时候初始化控制器.这是从该类调用 onInit
的时候.
Initialize the controller in your main or anytime before you actually use it. This is when the onInit
from that class is called.
Get.put(TextController()).textController;
这是 Page1
一个无状态的小部件,具有已初始化的 TextEditingController
Here's Page1
a stateless widget with an already initialized TextEditingController
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
final controller = Get.find<TextController>(); // finding same initialized controller
return Scaffold(
body: Center(
child: TextFormField(
controller: controller.textController, // using TextEditingConroller from GetX class
),
),
);
}
}
下面是一个简短示例,该示例显示了另一页上的文本小部件,只要用户在 TextFormField
And here's a quick example of a text widget on a different page automatically updating anytime the user types into the TextFormField
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
final controller =
Get.find<TextController>(); // finding same instance of controller
return Scaffold(
body: Center(
// this Obx widget rebuilds based on any updates
child: Obx(
() => Text(controller.textfieldString.value),
),
),
);
}
}
因此,无论您位于应用程序中的哪个位置,都可以访问该 TextFormField
的值,而不必使用有状态的小部件.不使用时, GetxController
将从内存中删除.
So no matter where you are in your app, you can access the value of that TextFormField
and you don't have to use a stateful widget. The GetxController
will be removed from memory when not in use.
这时,我唯一需要使用有状态窗口小部件的时间是当我需要 AutomaticKeepAliveClientMixin
时.
At this point the only time I ever need to use a stateful widget is when I need the AutomaticKeepAliveClientMixin
.
通过将 SingleGetTickerProviderMixin
添加到Getx类并在那里进行通常会使状态小部件混乱的所有事情,甚至可以使用GetX中的无状态小部件来制作动画.
Even animations can be done with stateless widgets in GetX by adding SingleGetTickerProviderMixin
to a Getx class and doing everything there that would normally clutter up your stateful widget.
这篇关于在buildMethod中分配TextEditingController的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!