将
cursorColor: Colors.white,
放入 TextFormField
这在 iOS 和 Android 上都运行良好:
TextField(cursorColor: Colors.white)
但是,如果你想将其设置在主题中
解决方案 1 - 原始答案,最近未测试,而且似乎已被弃用:
我找到了解决方案在这里:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
MaterialApp(
title: "Rate your Colleagues",
theme: ThemeData(
// for iOS
cupertinoOverrideTheme: CupertinoThemeData(
primaryColor: Colors.red,
),
// for others(Android, Fuchsia)
cursorColor: Colors.red,
home: SplashScreen(),
);
...
解决方案 2 - 编辑后的答案 - 未经测试 - 请注明@i4guar
我找到了解决方案在这里:
MaterialApp(
title: "Rate your Colleagues",
theme: ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: darkPrimarySwatchColor,
selectionColor: darkPrimarySwatchColor,
selectionHandleColor: darkPrimarySwatchColor,
),
),
home: SplashScreen(),
);
cursorColor
现已弃用,改为 ThemeData
(适用于 iOS 和 Android):
MaterialApp(
title: "Rate your Colleagues",
theme: ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: darkPrimarySwatchColor,
selectionColor: darkPrimarySwatchColor,
selectionHandleColor: darkPrimarySwatchColor,
),
),
home: SplashScreen(),
);
Flutter 已更新,现在 cursorColor 的使用方式如下:
ThemeData(
...
textSelectionTheme: TextSelectionThemeData(
cursorColor: Colors.blue, //thereby
),
),
对于问题1,您可以在调用
cursorColor
时为theme
属性设置MaterialApp
,如下所示
new MaterialApp(
title: "Flutter App",
theme: ThemeData(
cursorColor: Colors.red,
home: HomeScreen(),
)
我必须将
useTextSelectionTheme
设置为 true
并为我的自定义深色主题设置 textSelectionTheme:
ThemeData _defaultDarkTheme = ThemeData.dark();
ThemeData _darkTheme = initializeDefaultLineHeight(ThemeData(
brightness: Brightness.dark,
// How to set cursor color for TextFormField
useTextSelectionTheme: true,
textSelectionTheme: _defaultDarkTheme.textSelectionTheme.copyWith(
cursorColor: Colors.grey[600]),
你可以使用
textSelectionTheme: const TextSelectionThemeData(
cursorColor: Color(0xff0469a8),
selectionHandleColor: Color(0xff0469a8),
),
在你的 ThemeData 中设置光标和处理颜色。