Search code examples
flutterinputsubmitnavigator

how to change method of custom widget ( input Text Widget )


This is my Customs Input Text Widget

```
               TextInputLayout(
                focusNode: focusNodeEmail,
                controller: emailController,
                label: AppLocalizations.of(context)
                    .translate("worker_id_hash"),
                iconData: Icons.email,
                errorText:
                    _validate ? 'Please Enter Valid Worker ID' : null,
                inputType: TextInputType.emailAddress,
                inputAction: TextInputAction.done,
                textCapitalization: false,
                onTextChange: () {},
              ),
```

how to ? : on keyboard button 'Done' click goto new page

```TextFormField(
    focusNode: widget.focusNode,
    controller: widget.controller,
    textInputAction: widget.inputAction,
    textCapitalization: widget.textCapitalization
        ? TextCapitalization.sentences
        : TextCapitalization.none,
    keyboardType: widget.inputType,
    enabled: widget.enable == null ? true : false,
    initialValue: widget.initialValue == null ? null : widget.initialValue,
    onChanged: (value) {
      widget.onTextChange();
      setState(() {
        widget.errorText = null;
      });
    },
    onEditingComplete: () {
      if (widget.inputAction == TextInputAction.done) {
        FocusScope.of(context).unfocus();
      } else {
        FocusScope.of(context).requestFocus(widget.nextFocusNode);
      }
    },```

this widget used in whole project so now how can I change onEditingComplete methos for navigate to new page


Solution

  • Based on my understanding of your question, you want to change the behaviour of pressing the onEditingComplete based on the current route this widget currently in. If that is true, I guess what are you looking for are Callbacks, which are in definition according to Wikipedia:

    is any executable code that is passed as an argument to other code; that other code is expected to call back (execute) the argument at a given time.

    In easier English, it means passing a function/method as a parameter to another function/method to call inside its body.

    So in your case, in the widget you are using across the whole app, you will need to add an extra attribute of type Function or VoidCallBack (depending on if your function/method does or doesn't return a value) and pass that attribute to the onEditingComplete of the TextFormField.