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

The TextField could not set the selection state? #62654

Closed
1169886116 opened this issue Jul 31, 2020 · 16 comments
Closed

The TextField could not set the selection state? #62654

1169886116 opened this issue Jul 31, 2020 · 16 comments
Labels
in triage Presently being triaged by the triage team

Comments

@1169886116
Copy link

1169886116 commented Jul 31, 2020

//PDA扫描得到的值,取出来处理一下,在赋值给输入框,在设置选中状态无效
onChanged: (str) {
//将输入框的值处理
String newStr = str.split("#").first;
//赋值输入框新值
_controller.text = newStr;
//设置新值的选中状态
_controller.selection = TextSelection(
baseOffset: 0,
extentOffset: numberStr.length,
);
}

The TextField could not set the selection state?

@1169886116 1169886116 changed the title The TextField could not set the checked state? The TextField could not set the selection state? Jul 31, 2020
@pedromassangocode
Copy link

Hi @1169886116
Can you please provide your flutter doctor -v and a minimal complete reproducible code sample.
Thank you

@pedromassangocode pedromassangocode added in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds labels Jul 31, 2020
@reazon7
Copy link

reazon7 commented Aug 7, 2020

same issue...
change selection not working:
textController.selection = TextSelection.collapsed(offset: data.length);

Flutter (Channel stable, 1.20.1, on Microsoft Windows [Version 10.0.17134.1246], locale en-US)

@pedromassangocode
Copy link

Hi @reazon7
If you are facing this same issue please provide a minimal complete reproducible code sample to help us reproduce the issue.
Thank you.

@reazon7
Copy link

reazon7 commented Aug 10, 2020

Hi @reazon7
If you are facing this same issue please provide a minimal complete reproducible code sample to help us reproduce the issue.
Thank you.

in my case,, it's not TextField but TextFormField that has the problem.

here's minimal code

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Testing'),
        ),
        body: TextFormField(
          controller: _controller,
          onChanged: (value) {
            String newData = 'asdqwe123';
            _controller.value = TextEditingValue(
              text: newData,
              selection: TextSelection.collapsed(offset: newData.length),
            );
          },
        ),
      ),
    );
  }
}

this happended after I upgraded flutter version to stable 1.20.1 (in stable 1.17.5 it's fine)...

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.20.1, on Microsoft Windows [Version 10.0.17134.1246], locale en-US)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[!] Android Studio (version 3.3)
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code (version 1.47.3)
[√] Connected device (1 available)

@Omrankabalan
Copy link

Same happening to me after upgrading to stable 1.20.1

It was working perfectly in stable 1.17.5

@VivaThapelo
Copy link

VivaThapelo commented Aug 18, 2020

Same problem.

Flutter upgrade broke [Example]:

 _cardNumberController.text += " ";
_cardNumberController.selection = TextSelection.collapsed(offset: 5);

the offset is always 0(zero), it ignores the 5

 TextFormField(
                        maxLengthEnforced: true,
                        maxLength: 19,
                        keyboardType: TextInputType.number,
                        controller: _cardNumberController,
                        onChanged: (value) {
                          switch (value.length) {
                            case 4:
                              {
                                _cardNumberController.text += " ";
                                _cardNumberController.selection = TextSelection.collapsed(offset: 5);
                                break;
                              }
                            case 9:
                              {
                                _cardNumberController.text += " ";
                                _cardNumberController.selection =  TextSelection.collapsed(offset: 10);
                                break;
                              }
                            case 14:
                              {
                                _cardNumberController.text += " ";
                                _cardNumberController.selection = TextSelection.collapsed(offset: 15);
                                break;
                              }
                          }
                          return null;
                        },
                        decoration: InputDecoration(
                            hintText: '5324 9087 5432 1234',
                            labelText: 'Card Number'),
                      ),

Flutter Doctor:

C:\Users\vivat\AndroidStudioProjects\ecash_pay>flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 1.20.2, on Microsoft Windows [Version 10.0.19041.450], locale en-ZA)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.0)
[!] VS Code (version 1.47.2)
    X Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[√] Connected device (3 available)

@pedromassangocode
Copy link

Hi @katlehotech
For me to help you solve this issue I need you to provide a code that I can just copy/paste and run it. Please provide such code.
Thank you

@Omrankabalan
Copy link

@pedromassangocode There is complete reproducible code added by @reazon7 9 days ago!

@1169886116
Copy link
Author

import 'package:flutter/material.dart';

class TextFieldPage extends StatefulWidget {
@OverRide
_TextFieldPageState createState() => _TextFieldPageState();
}

class _TextFieldPageState extends State {
TextEditingController tfController = TextEditingController();
FocusNode tfNode = FocusNode();
@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("选中状态"),
),
body: Center(
child: Column(
children: [
TextField(
controller: tfController,
focusNode: tfNode,
decoration: InputDecoration(
labelText: "用户名",
hintText: "用户名",
prefixIcon: Icon(Icons.person),
),
onChanged: (result) {
print("输入: $result");
},
onSubmitted: (result) {
print("提交: $result");
//赋值
// tfController.text = result;
//设置选中状态
tfController.selection = TextSelection(
baseOffset: 0, //开始位置
extentOffset: result.length, //长度
);
},
)
],
),
),
);
}
}

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Aug 19, 2020
@1169886116
Copy link
Author

Clicking submit cannot set the content to selected mode

@1169886116
Copy link
Author

flutter SDK : [✓] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.15.6 19G2021,
locale zh-Hans-CN)

@pedromassangocode
Copy link

@pedromassangocode There is complete reproducible code added by @reazon7 9 days ago!

The code he shared does not reproduce the actual issue!

@pedromassangocode
Copy link

Hi @1169886116
Could you please run flutter upgrade --force.
If the issue persists, please provide your updated flutter doctor -v and a minimal reproducible code sample.
Thank you

@pedromassangocode pedromassangocode added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Aug 19, 2020
@VivaThapelo
Copy link

Just do a
> flutter downgrade
everything will work fine

@TahaTesser TahaTesser removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 9, 2020
@TahaTesser
Copy link
Member

Without additional information, we are unfortunately not sure how to resolve this issue.
We are therefore reluctantly going to close this bug for now.
Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away!
Thanks for your contribution.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
in triage Presently being triaged by the triage team
Projects
None yet
Development

No branches or pull requests

6 participants