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

TextField is focused after second "click" #35694

Closed
YaTree opened this issue Jul 7, 2019 · 2 comments
Closed

TextField is focused after second "click" #35694

YaTree opened this issue Jul 7, 2019 · 2 comments
Labels
a: text input Entering text in a text field or keyboard related problems f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Comments

@YaTree
Copy link

YaTree commented Jul 7, 2019

Hello.

There is a list of TextFields on the screen. When a user enters a number the application will shift focus to another TextField. It is clear for me how to change focus, but I faced with the next issue, please look on the screenshot.

The first TextInput has autofocus: True, others - False.
When we open the screen the first TextInput as focused and keyboard is opening, but "underline" is not "blue".
Then I start to enter values and get the result as on the screenshot - the focus has been shifted, on the first TextInput I have "blinking" cursor, the second input has "blue" underling and "blinking" cursor. If I will continue - the first TextField will continue to be problematic.

if I will "click" a second time on the first TextField - the "underline" will become blue and "duplicated" cursor will disappear.

problem

This is the code for TextFields Row:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Code extends StatelessWidget {
  Code({int numberOfInputs, Function action}) {
    _number = numberOfInputs;

    for (var i = 0; i < numberOfInputs; i++) {
      _widgets.add(OneDigitInput(
        focusNodes: _focusNodes,
        index: i,
        validate: check,
      ));
      _focusNodes.add(FocusNode());
      _values.add('');
      _action = action;
    }
  }

  Function _action;
  int _number;
  List<Widget> _widgets = new List();
  List<FocusNode> _focusNodes = new List();
  List<String> _values = new List();

  List<Widget> getWidgets() {
    return _widgets;
  }

  setAction(Function action) {
    _action = action;
  }

  check(BuildContext context, int index, String newValue) {
    String concat = '';

    _values[index] = newValue;
    for (var j = 0; j < _values.length; j++) {
      String value = _values[j];
      concat += value;
    }

    if (concat.length == _number) {
      _action();
    } else {
      if (index < _number + 1) {
        _focusNodes[index].unfocus();
        FocusScope.of(context).requestFocus(_focusNodes[index + 1]);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: _widgets,
    );
  }
}

class OneDigitInput extends StatelessWidget {
  OneDigitInput({List<FocusNode> focusNodes, int index, Function validate}) {
    _focusNodes = focusNodes;
    _validate = validate;
    _index = index;
  }

  int _index;
  final _textFieldController = TextEditingController();

  Function _validate;
  List<FocusNode> _focusNodes;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      child: Container(
        width: 20,
        child: TextField(
          controller: _textFieldController,
          onChanged: (String value) {
            _validate(context, _index, _textFieldController.text);
          },
          autofocus: _index == 0 ? true : false,
          focusNode: _focusNodes[_index],
          textAlign: TextAlign.center,
          textInputAction: TextInputAction.unspecified,
          style: TextStyle(
            fontSize: 20,
          ),
          decoration: InputDecoration(contentPadding: EdgeInsets.all(1)),
          inputFormatters: [
            LengthLimitingTextInputFormatter(1),
          ],
          keyboardType: TextInputType.number,
        ),
      ),
    );
  }
}

@YaTree YaTree changed the title TextField is focused after two second "click" TextField is focused after second "click" Jul 7, 2019
@shihaohong shihaohong added a: text input Entering text in a text field or keyboard related problems f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels Jul 7, 2019
@TahaTesser
Copy link
Member

Looks like this issue is solved
i wasn't able to reproduce the issue as mention by you on latest dev channel

Code Sample

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: Code(
            numberOfInputs: 4,
            action: () {},
          ),
        ),
      ),
    );
  }
}

class Code extends StatelessWidget {
  Code({int numberOfInputs, Function action}) {
    _number = numberOfInputs;

    for (var i = 0; i < numberOfInputs; i++) {
      _widgets.add(OneDigitInput(
        focusNodes: _focusNodes,
        index: i,
        validate: check,
      ));
      _focusNodes.add(FocusNode());
      _values.add('');
      _action = action;
    }
  }

  Function _action;
  int _number;
  List<Widget> _widgets = new List();
  List<FocusNode> _focusNodes = new List();
  List<String> _values = new List();

  List<Widget> getWidgets() {
    return _widgets;
  }

  setAction(Function action) {
    _action = action;
  }

  check(BuildContext context, int index, String newValue) {
    String concat = '';

    _values[index] = newValue;
    for (var j = 0; j < _values.length; j++) {
      String value = _values[j];
      concat += value;
    }

    if (concat.length == _number) {
      _action();
    } else {
      if (index < _number + 1) {
        _focusNodes[index].unfocus();
        FocusScope.of(context).requestFocus(_focusNodes[index + 1]);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: _widgets,
    );
  }
}

class OneDigitInput extends StatelessWidget {
  OneDigitInput({List<FocusNode> focusNodes, int index, Function validate}) {
    _focusNodes = focusNodes;
    _validate = validate;
    _index = index;
  }

  int _index;
  final _textFieldController = TextEditingController();

  Function _validate;
  List<FocusNode> _focusNodes;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      child: Container(
        width: 20,
        child: TextField(
          controller: _textFieldController,
          onChanged: (String value) {
            _validate(context, _index, _textFieldController.text);
          },
          autofocus: _index == 0 ? true : false,
          focusNode: _focusNodes[_index],
          textAlign: TextAlign.center,
          textInputAction: TextInputAction.unspecified,
          style: TextStyle(
            fontSize: 20,
          ),
          decoration: InputDecoration(contentPadding: EdgeInsets.all(1)),
          inputFormatters: [
            LengthLimitingTextInputFormatter(1),
          ],
          keyboardType: TextInputType.number,
        ),
      ),
    );
  }
}

flutter doctor -v

[✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Mac OS X 10.15.4 19E266,
    locale en-GB)
    • Flutter version 1.12.13+hotfix.9 at /Users/taha/Code/flutter_stable
    • Framework revision f139b11009 (3 days ago), 2020-03-30 13:57:30 -0700
    • Engine revision af51afceb8
    • Dart version 2.7.2

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/taha/Code/sdk
    • Android NDK location not configured (optional; useful for native profiling
      support)
    • Platform android-29, build-tools 29.0.3
    • ANDROID_HOME = /Users/taha/Code/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.4, Build version 11E146
    • CocoaPods version 1.9.1

[✓] Android Studio (version 3.6)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 44.0.2
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build
      1.8.0_212-release-1586-b4-5784211)

[✓] VS Code (version 1.43.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.8.1

[✓] Connected device (1 available)
    • SM M305F • 32003c30dc19668f • android-arm64 • Android 10 (API 29)

• No issues found!

if you're still experiencing this problem, please comment below and I will reopen it
Thank you

@lock
Copy link

lock bot commented Apr 19, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@lock lock bot locked and limited conversation to collaborators Apr 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: text input Entering text in a text field or keyboard related problems f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

No branches or pull requests

3 participants