How to Change the Cursor Color of Text Input in Flutter

Text inputs in flutter are created using either TextField or TextFormField widget. In this flutter example, let’s check how to change the default color of cursor when using TextField widget.

If you don’t know how to add text input in flutter then please do read how to add text inputs in flutter.

Changing the color of text input’s cursor in flutter is pretty easy. You just need to use the attribute cursorColor as given below to define the color of the cursor.

TextField(cursorColor: Colors.white)

Following is the complete example of TextField with custom cursor color in Flutter.

import 'package:flutter/material.dart';

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

class TextInputExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Text Input Cursor Example',
      home: MyInput(),
    );
  }
}

// Define a custom Form widget.
class MyInput extends StatefulWidget {
  @override
  _MyInputState createState() => _MyInputState();
}

// Define a corresponding State class.
// This class holds the data related to the Form.

class _MyInputState extends State<MyInput> {

  // Create a text controller and use it to retrieve the current value
  // of the TextField.

  final myController = TextEditingController();

  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Text Input Cursor Color Tutorial'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Center(child: TextField(
          cursorColor: Colors.red,
          controller: myController,
        ),
      ),
      ),   
    );
  }
}

Following is the output of Flutter Cursor example.

I hope this Flutter tutorial will help you.

Leave a Comment

Your email address will not be published. Required fields are marked *