SILENMUS
SILENMUS

Reputation: 947

how to change TextField cursor color in flutter

I'm using TextField widget in AppBar()

there is one problem , as you can see I cannot set cursor color when textfield focused

usually, textfield cursor blinks when it focused.

enter image description here

I set cursor color property , every color property in appbar, textfield but it doesn't work even textfield hint text doens't work too.

            appBar: AppBar(
              title: Card(
                margin: EdgeInsets.only(
                    top: common_gap * 1.5, bottom: common_gap * 1.5),
                child: TextField(
                  cursorColor: Constants.kPrimaryOrange,
                  controller: _controller,
                  focusNode: _focusNode,
                  onChanged: (value) {
                    setState(() {
                      _searchText = value;
                    });
                  },
                  decoration: InputDecoration(
                    prefixIcon: Icon(
                      Icons.search,
                      size: 20,
                    ),
                    suffixIcon: _controller.text.length != 0
                        ? IconButton(
                            icon: Icon(
                              Icons.cancel,
                              size: 20,
                              color: _searchText == ''
                                  ? Colors.transparent
                                  : Colors.black87,
                            ),
                            onPressed: () {
                              setState(() {
                                _controller.clear();
                                _searchText = '';
                                _focusNode.unfocus();
                              });
                            },
                          )
                        : Container(),
                  ),

                ),

can you tell me how to fix this ??

Upvotes: 31

Views: 38668

Answers (4)


  @override
  ThemeData appBarTheme(BuildContext context) {
    return Theme.of(context).copyWith(
      primaryColor: Colors.white,
      hintColor: Colors.white,
      textSelectionTheme: Theme.of(context).textSelectionTheme.copyWith(
            cursorColor: Colors.white, /// <--- THIS IS THE COLOR
          ),
      iconTheme: const IconThemeData(
        color: Colors.white,
      ),
      textTheme: const TextTheme(
        headline6: TextStyle(
          color: Colors.white,
          fontSize: 20,
        ),
      ),
    );
  }

Upvotes: 1

Jitesh Mohite
Jitesh Mohite

Reputation: 34180

There are two ways to do that

First Approach

Assign color directly to individual TextField or TextFormField

TextFormField(
  cursorColor: Colors.green,)

Second Approach

Assign cursor color throughout the application using TextSelectionThemeData

MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.green,
        textSelectionTheme: TextSelectionThemeData(
          cursorColor: Colors.green
        ),
      ),
      home: LoginScreen(),
    )

Output:

enter image description here

Upvotes: 30

shorol
shorol

Reputation: 990

You can change specific textfield cursor color for your solution:

TextField(cursorColor: Colors.white)

but if you want to change it for all out you project then you can check this here

Upvotes: 46

hasan karaman
hasan karaman

Reputation: 1430

Can you add it in the materialapp on the main.dart page. and would you stop the application and run it again.

MaterialApp(
 title: "App Name",
 theme: ThemeData(
  // for iOS
  cupertinoOverrideTheme: CupertinoThemeData(
    primaryColor: Constants.kPrimaryOrange,
  ),
   // for others Android
  cursorColor: Constants.kPrimaryOrange,
home: HomePage(),
 ),
);

Upvotes: 0

Related Questions