Flutter: How to hide TextField text pointer (cursor) when use initial value text (Android)
Asked Answered
B

9

19

Use case: Messaging app you edit your message: keyboard, blinking cursor and initial text appears but pointer (cursor) is not

But on Flutter when you use initial text (or via textController) there are always pointer(cursor) which is not wanted

Example

enter image description here enter image description here

Steps to reproduce: run flutter create bug

edit main.dart to replace center text (line 100) to MyStatefulPage(),

class MyStatefulPage extends StatefulWidget {
  @override
  State<MyStatefulPage> createState() {
    return _MyStatefulPageState();
  }
}
class _MyStatefulPageState extends State<MyStatefulPage> {

  TextEditingController controller;

  @override
  void initState() {
    super.initState();
    controller = new TextEditingController();
    controller.text = 'My Initial Text';
  } 

  @override
  Widget build(BuildContext context) {
    return TextField(
      decoration: InputDecoration(
       border: InputBorder.none
      ),
      // showCursor: false,
      controller: controller,
      autofocus: true,
      maxLines: 8,
    );
  }
}

With that code when you open app keyboard will appear but so will pointer(cursor) I want to hide this cursor(pointer).

Note: it's only for Android.

Baziotes answered 19/5, 2020 at 10:45 Comment(2)
Hi, @philip did you resolve your issue?Grape
Hi @DMDev No, this question was created almost 2 years ago. Maybe some of the answers are correct but I haven't tested them if you will find a solution please post it here as well, Thanks!Baziotes
R
31

TextField set enableInteractiveSelection property to false can resolve this issue

Reaves answered 23/7, 2020 at 8:52 Comment(1)
Still, the pointer is visible below the cursorDepravity
B
6

To hide the blinking cursor

enter image description here

Add this line of code

showCursor: false

To hide the editing pointer

enter image description here

Add this line of code

enableInteractiveSelection: false
Bedrail answered 26/7, 2023 at 6:48 Comment(0)
G
3

in textformfield use showCursor: false

Grog answered 18/12, 2022 at 9:13 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Hoover
B
2

TextFormField cursorHeight: 0 and cursorWidth: 0 can hide the cursor.

Borage answered 21/1, 2022 at 21:30 Comment(0)
H
0

TextFormField(enableInteractiveSelection: false)

Heartburning answered 3/3, 2023 at 9:58 Comment(0)
G
0

readOnly: true, hide the cursor

Grenadine answered 21/7, 2023 at 23:7 Comment(0)
P
0

You may need to disable TextField. See the example below to disable TextField input in Flutter.

 TextField(
              decoration: InputDecoration(
                  labelText: "Your Name (Disabled)",
                  enabled: false //disabel this text field input
              )
          ),
Predate answered 28/11, 2023 at 13:19 Comment(0)
R
0

Remove the cursor and follow this solution all are working.

  1. showCursor: false
  2. cursorColor: Colors.transparent
  3. cursorWidth: 0
  4. cursorHeight: 0
Rubbing answered 27/6 at 4:29 Comment(0)
F
0

If you want to hide the cursor in a TextField when it has an initial value, there are several approaches you can take. Below are some solutions, including customizing cursor appearance globally via ThemeData.

1.Setting showCursor to false The most direct approach is to set the showCursor property of the TextField to false. This will completely hide the cursor.

TextField(
  controller: TextEditingController(text: 'Initial Text'),
  showCursor: false,
  // ... other properties
)
  1. Customizing Cursor Appearance If you want to maintain the cursor but modify its appearance, you can use the cursorColor, cursorWidth, and cursorHeight properties. Setting cursorWidth and cursorHeight to 0 will effectively hide the cursor.
TextField(
  controller: TextEditingController(text: 'Initial Text'),
  cursorColor: Colors.transparent,
  cursorWidth: 0,
  cursorHeight: 0,
  // ... other properties
)
  1. Using readOnly and onTap For a more interactive approach, you can set readOnly to true to prevent text editing and then handle the onTap event to allow editing when needed.
bool _isEditable = false;

TextField(
  controller: TextEditingController(text: 'Initial Text'),
  readOnly: !_isEditable,
  onTap: () {
    setState(() {
      _isEditable = true;
    });
  },
  // ... other properties
)

Customizing Cursor Appearance Globally

You can customize the cursor appearance globally using ThemeData.textSelectionTheme. This will affect all TextField widgets in your app.

MaterialApp(
  theme: ThemeData(
    textSelectionTheme: TextSelectionThemeData(
      cursorColor: Colors.YourColor,
      selectionColor: Colors.YourColor,
      selectionHandleColor: Colors.YourColor,
    ),
  ),
  // ... other properties
)

Example with ThemeData and showCursor Combining these techniques, you can achieve the desired cursor behavior for your TextField.

MaterialApp(
  theme: ThemeData(
    textSelectionTheme: TextSelectionThemeData(
      cursorColor: Colors.YourColor,
      selectionColor: Colors.YourColor,
      selectionHandleColor: Colors.YourColor,
    ),
  ),
  home: Scaffold(
    body: Center(
      child: TextField(
        controller: TextEditingController(text: 'Initial Text'),
        showCursor: false,
        // or customize cursor appearance:
        // cursorColor: Colors.transparent,
        // cursorWidth: 0,
        // cursorHeight: 0,
      ),
    ),
  ),
)

Choosing the Right Approach

  • Preventing cursor display completely: Use showCursor: false.
  • Customizing cursor appearance: Use cursorColor, cursorWidth, and cursorHeight.
  • Allowing editing on tap: Use readOnly and onTap.

By combining these techniques, you can achieve the desired cursor behavior for your TextField.

Firebug answered 29/7 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.