如何在flutter中更改光标颜色

问题描述 投票:0回答:7

亲爱的, 如果你不介意的话,我有两个关于 flutter 的问题。

1-如何更改光标的颜色,因为它默认为蓝色,我不喜欢它

2-无论屏幕尺寸如何,如何使文本位于屏幕底部。 ??

提前谢谢您。

enter image description here

flutter flutter-layout
7个回答
72
投票

 cursorColor: Colors.white,
放入 TextFormField


62
投票

这在 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(),
 );

27
投票

cursorColor
现已弃用,改为
ThemeData
(适用于 iOS 和 Android):

MaterialApp(
  title: "Rate your Colleagues",
  theme: ThemeData(
     textSelectionTheme: TextSelectionThemeData(
        cursorColor: darkPrimarySwatchColor,
        selectionColor: darkPrimarySwatchColor,
        selectionHandleColor: darkPrimarySwatchColor,
     ),
   ),
   home: SplashScreen(),
 );

22
投票

Flutter 已更新,现在 cursorColor 的使用方式如下:

ThemeData(
  ...
  textSelectionTheme: TextSelectionThemeData(
    cursorColor: Colors.blue, //thereby
  ),
),

16
投票

对于问题1,您可以在调用

cursorColor
时为
theme
属性设置
MaterialApp
,如下所示

new MaterialApp(
  title: "Flutter App",
  theme: ThemeData(
    cursorColor: Colors.red,
    home: HomeScreen(),
)

4
投票

我必须将

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


0
投票

你可以使用

 textSelectionTheme: const TextSelectionThemeData(
  cursorColor: Color(0xff0469a8),
  selectionHandleColor: Color(0xff0469a8),
),

在你的 ThemeData 中设置光标和处理颜色。

© www.soinside.com 2019 - 2024. All rights reserved.