繁体   English   中英

如何在 TextField Flutter 中更改 cursor 颜色

[英]How to change cursor color in TextField Flutter

我面临的问题是我的 cursor 颜色在通过主题小部件包装 TextField 时没有改变。

这里的主要问题是指针颜色在直接包装时没有改变,在这个线程https://github.com/flutter/flutter/issues/74890

您可以做的是在您的 MaterialApp 小部件中添加主题

  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        textSelectionTheme: TextSelectionThemeData(
          selectionColor: Colors.green,
          cursorColor: Colors.green,
          selectionHandleColor: Colors.green,
        ),
      ),
      home: const HomeWidget(),
    );
  }

您的 TextField 将如下所示:

class _SimpleTextFieldState extends State<SimpleTextField> {
  Color focusColor = Colors.grey;

  @override
  Widget build(BuildContext context) {
    return Focus(
      onFocusChange: (hasFocus) {
        if (hasFocus) {
          focusColor = Colors.green;
        } else {
          focusColor = Colors.grey;
        }
        setState(() {});
      },
      child: TextField(
        decoration: InputDecoration(
          isDense: true,
          labelText: 'Text',
          labelStyle: TextStyle(
            color: focusColor,
            fontSize: fontSize15,
          ),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.grey),
          ),
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.green),
          ),
          border: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.green),
          ),
        ),
        style: TextStyle(
          color: Colors.white,
          fontSize: fontSize15,
        ),
      ),
    );
  }
}

不活跃

价格字段未激活

积极的

价格字段活跃

解决方案1:-

FYI:- Add "cursorColor" property in theme data

    import 'package:flutter/material.dart';
    
    MaterialApp(
      title: "Solution 1",
      theme: ThemeData(
        cursorColor: Colors.red,
      ),
      home: splashScreen(),
    );

解决方案2: -

 MaterialApp(
  title: "Solution 2",
  theme: ThemeData(
     textSelectionTheme: TextSelectionThemeData(
        cursorColor: darkPrimarySwatchColor,
        selectionColor: darkPrimarySwatchColor,
        selectionHandleColor: darkPrimarySwatchColor,
     ),
   ),
   home: splashScreen(),
 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM