如何在颤动中更改 TextField 光标颜色

时间:2021-01-26 07:30:32

标签: flutter colors textfield

我在 AppBar() 中使用 TextField 小部件

有一个问题, 如您所见,当文本字段聚焦时,我无法设置光标颜色

通常,文本字段光标在聚焦时会闪烁。

enter image description here

我设置了光标颜色属性, appbar、textfield 中的每个颜色属性 但它不起作用 甚至文本字段提示文本也不起作用。

            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(),
                  ),

                ),

你能告诉我如何解决这个问题吗??

3 个答案:

答案 0 :(得分:3)

您可以为您的解决方案更改特定的文本字段光标颜色:

TextField(cursorColor: Colors.white)

但是如果您想为所有项目更改它,那么您可以查看此here

答案 1 :(得分:0)

能否在main.dart页面的materialapp中添加。您是否会停止应用程序并再次运行它。

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

答案 2 :(得分:0)

有两种方法可以做到这一点

第一种方法

直接为单个 TextFieldTextFormField 指定颜色

TextFormField(
  cursorColor: Colors.green,)

第二种方法

使用 TextSelectionThemeData

在整个应用程序中指定光标颜色
MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.green,
        textSelectionTheme: TextSelectionThemeData(
          cursorColor: Colors.green
        ),
      ),
      home: LoginScreen(),
    )

输出:

enter image description here

相关问题