Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard or focusNode #22

Closed
klaszlo8207 opened this issue Mar 13, 2020 · 6 comments
Closed

Keyboard or focusNode #22

klaszlo8207 opened this issue Mar 13, 2020 · 6 comments

Comments

@klaszlo8207
Copy link

Is there any way to use nextFocus without a context? Because when my TextField in a widget that is opened via Get my keyboard is going to be crazy from this line:

onSubmitted: (_) => FocusScope.of(context).nextFocus(), // move focus to next
),

@jonataslaw
Copy link
Owner

Is there any way to use nextFocus without a context? Because when my TextField in a widget that is opened via Get my keyboard is going to be crazy from this line:

onSubmitted: (_) => FocusScope.of(context).nextFocus(), // move focus to next
),

You can retrieve the context of the last builder widget using Get.key.currentWidget from anywhere in your code.

onSubmitted: (_) => FocusScope.of(Get.key.currentContext).nextFocus(), // move focus to next
),

@klaszlo8207
Copy link
Author

klaszlo8207 commented Mar 17, 2020

Sorry but this is not working at all:

next Focus never call when I use with Get.key.currentContext

Also when I use this:

TextField(
        textInputAction: TextInputAction.next,
        controller: controller,
 onSubmitted: (s) {
          FocusScope.of(context).nextFocus();
          //FocusScope.of(Get.key.currentContext).nextFocus();
        },

then the probleme is:

First time it is working like a charm, But after I write something in the field and submit called setState or changeNotifier, the second time when click in the TextField my keyboard just pop up and immediatelly pop down (show and hide) and lose focus

Also I have to generate on the fly a lot of TextField and controllers for that and perhaps listeners to that...

Can you help me how to fix this?

Basically what I want:

Row1 [Textfield][Textfield][Textfield]
Row2 [Textfield][Textfield][Textfield]
Row3 [Button to add new Row]

ADD ->

Row1 [Textfield][Textfield][Textfield]
Row2 [Textfield][Textfield][Textfield]
Row3 [Textfield][Textfield][Textfield]
Row4 [Button to add new Row]

DEL ->

Row1 [Textfield][Textfield][Textfield]
Row2 [Textfield][Textfield][Textfield]
Row3 [Button to add new Row]

Also I have to save those values in somehow and refresh the UI, via setState or changeNotifier

I also find this BUG in the flutter app:

flutter/flutter#52342 (comment)

@klaszlo8207
Copy link
Author

klaszlo8207 commented Mar 17, 2020

Here is my textfield code

TextField(
        textInputAction: TextInputAction.next,
        controller: controller,
        maxLength: 3,
        maxLines: 1,
        /*
        onChanged: (s) {
          if (onChanged != null) onChanged(s);
        },*/
        /*
        onSubmitted: (s) {
          //FocusScope.of(Get.key.currentContext).nextFocus(); //THIS IS NOT WORKING AT ALL
          FocusScope.of(context).nextFocus(); //THIS WILL KILL MY FOCUS + KEYBOARD SECOND TIME
          //if (onSubmitted != null) onSubmitted(s);
        },*/
        textAlign: TextAlign.center,
        enableInteractiveSelection: !disableCopyPaste,
        keyboardType: TextInputType.number,
        inputFormatters: <TextInputFormatter>[WhitelistingTextInputFormatter.digitsOnly],
        cursorColor: Colors.black,
        decoration: InputDecoration(
          counterText: "",
          border: InputBorder.none,
        ),
        style: textStyleNormal,
      ))

and my listeners generating in realtime after build something like this:

 final textWeightControllerListener = () {
          final newText = textWeightController.text;
          textWeightController.value = textWeightController.value.copyWith(
            text: newText,
            selection: TextSelection(baseOffset: newText.length, extentOffset: newText.length),
            composing: TextRange.empty,
          );
          _updateSetFor(exercises[i], j, weight: textWeightController.text);
        };
        textWeightController.addListener(textWeightControllerListener);

@jonataslaw
Copy link
Owner

You are probably swapping some StatefulWidget for StatelessWidget, or inserting controllers inside build methods instead of inserting them at the beginning of a Stateful class.

@klaszlo8207
Copy link
Author

So basically I have to create all the TextEditingControllers in initState instead of build? That will work?

@jonataslaw
Copy link
Owner

So basically I have to create all the TextEditingControllers in initState instead of build? That will work?

Just like I told you in that comment:
#20 (comment)

Controllers can NEVER be inside builders, no matter if it’s the main build (context) widget, or inside a StreamBuilder, FutureBuilder, BottomSheet, because at each setState a new controller will be created.

You can never do this:

class Example extends StatelessWidget {
  const Example({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _editText = TextEditingController(); // NEVER DO THAT. Controllers should never be within builder methods.
    return TextField(
      controller: _editText,
    );
  }
}

You should ALWAYS do this:

class Example extends StatefulWidget { 
  // If you have any controller it is mandatory to use StatefulWidget.
  Example({Key key}) : super(key: key);

  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
 final TextEditingController _editText = TextEditingController(); // HERE

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _editText,
    );
  }
}

You can also create the controller in the mentioned place, and instantiate it in your initState, so it will only be called once, however, for code security reasons, I recommend doing it as above.

Note that this is not related to this library, and as much as I want to help you, it can mess up people who are looking for something in issues.
If you want to ask questions about the framework not related to this library, you can ask me on the messenger
https://www.facebook.com/jonny.borges.982

Reserve this field only for issues directly related to Get.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants