【问题标题】:How to retrieve a TextEditingController inside a Controller layer with Getx?如何使用 Getx 在 Controller 层中检索 TextEditingController?
【发布时间】:2021-04-30 21:11:52
【问题描述】:

我的视图层中有这个语句

TextEditingController controllerDestino = TextEditingController();

我想恢复这个 controllerDestino,以便在我的 Controller 层中的方法中使用。

 statusUberNaoChamado() {
showBoxAdress = true;

changeMainButton("Chamar", Color(0xFF1ebbd8), () {
  callUber("I need pass the controller here");
});

update();}

提前感谢您的关注:)

【问题讨论】:

  • 你的意思是说你想把文本编辑控制器的值发送给callUber函数对吧?
  • 我有方法:callUber(TextEditingController 控制器){###}。所以我要在另一个方法中调用这个方法,我需要传递这个 TextEditingController 参数。问题是,我直接从 View 层调用的 callUber() 方法,其中 TextEditingController 被实例化,所以我可以轻松恢复。但是 statusUberNaoChamado () 方法在控制器层内,我想知道如何检索视图层中的 TextEditingController 实例

标签: flutter dart flutter-getx


【解决方案1】:

将 TextEditingController 定义/实例化为 GetxController 中的一个字段,用于控制表单/实现业务逻辑。

class DestinoFormControllerX extends GetxController {
  static DestinoFormControllerX get i => Get.find();
  final GlobalKey<FormBuilderState> key = GlobalKey<FormBuilderState>();

  // ↓ place the text editing controller inside your... controller :)
  var controllerDestino = TextEditingController();

并在 GetxController 中任何需要的地方使用 TextEditingController 值

  void resetForm() {
    key.currentState.reset();
    controllerDestino.text = '';
    focusNode.requestFocus();
  }

在您的视图层中,注入您的 GetxController,并获取文本编辑控制器并访问您需要的任何其他方法/字段。

class DestinoForm extends StatelessWidget {
  final void Function() submitHandler;

  DestinoForm({this.submitHandler});

  @override
  Widget build(BuildContext context) {
    final dcx = Get.put(DestinoFormControllerX());
    // ↑ inject GetxController, be careful to put *inside* build method

    return FormBuilder(
      key: dcx.key,
      child: Column(
        children: [
          FormBuilderTextField(
            name: 'destino',
            controller: dcx.controllerDestino,
            decoration: InputDecoration(
              labelText: 'Destino',
            ),

大多数表单都有重置和提交按钮。在那里您可以调用 GetxController 上的方法....

      actions: [
        FlatButton(
          child: Text('Reset'),
          onPressed: () => DestinoFormControllerX.i.resetForm(),
        ),

旁注

如果您要使用 Get.put() 在表单小部件中实例化/注入 GetxController,请在在表单小部件的 build 方法中执行此操作。

否则,您可能会让 TextEditingControllers 在不再安装在小部件树中的 StatefulWidget(文本字段)上调用 setState

════════ Exception caught by foundation library ════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for TextEditingController:
setState() called after dispose(): _FormBuilderTextFieldState#96390(lifecycle state: defunct, not mounted)

class DestinoForm extends StatelessWidget {
  final void Function() submitHandler;

  DestinoForm({this.submitHandler});

  @override
  Widget build(BuildContext context) {
    final dcx = Get.put(DestinoFormControllerX());
    // ↑ inject GetxController, be careful to put *inside* build method

不好

class DestinoForm extends StatelessWidget {
  final void Function() submitHandler;
  final dcx = Get.put(DestinoFormControllerX());
  // ↑ wrong place, DestinoFormControllerX gets linked to previous route

  DestinoForm({this.submitHandler});

  @override
  Widget build(BuildContext context) {

More detail on Github,提到正确注入/使用 GetX。

【讨论】:

  • 您在选择 GetX 进行状态管理方面有了一个良好的开端。这里链接了一些很好的视频,可以帮助您快速上手:github.com/jonataslaw/getx/wiki/…
猜你喜欢
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
  • 2022-08-04
  • 2015-10-14
  • 2016-07-15
  • 2021-10-10
  • 2019-07-06
  • 2021-07-30
相关资源
最近更新 更多