【问题标题】:How to change cursor color in flutter如何在颤动中更改光标颜色
【发布时间】:2019-09-23 07:18:23
【问题描述】:

亲爱的, 如果你不介意的话,我有 2 个关于颤振的问题。

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

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

提前谢谢你。

【问题讨论】:

  • 第二个问题我没看懂,你想用屏幕大小动态拉伸Text吗?您可以获取屏幕宽度的值并从那里开始,这是代码:MediaQuery.of(context).size.width
  • @mirkancal 我不想拉伸文本。我想把它放在屏幕的末尾

标签: flutter flutter-layout


【解决方案1】:

Flutter 已经更新,现在 cursorColor 是这样使用的:

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

【讨论】:

    【解决方案2】:

    对于问题1,您可以在调用MaterialApp 时为theme 设置cursorColor 属性,如下所示

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

    【讨论】:

    • 这对任何人都有效吗?我的仍然默认为蓝色,即使 Theme.of(context).cursorColor 是黑色...
    • @d0n.key 是的,您还可以在 TextField 中分配 cursorColor 属性。
    • CursorColor 已弃用。
    【解决方案3】:

    这在 iOS 和 Android 上都可以正常工作:

    TextField(cursorColor: Colors.white)
    

    但是,如果你喜欢在主题中设置它那么

    解决方案 1 - 原始答案,最近没有经过测试,而且似乎已被弃用:

    我找到了解决方案here

    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

    我找到了解决方案here

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

    【讨论】:

    • 你的第一个解决方案对我来说很好,但在主题中设置cursorColor 不起作用。
    • ThemeData 中的光标颜色已被弃用。任何其他解决方案。
    • 查看下面的 cmets,例如stackoverflow.com/a/65550345/9142902
    • 第一个解决方案已弃用,应使用textSelectionTheme
    【解决方案4】:

    cursorColor 现已弃用,ThemeData 改用它(适用于 iOS 和 android):

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

    【讨论】:

    • 是的,这应该是问题 #1 的公认答案。
    【解决方案5】:

    我必须将 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]),
    
    

    【讨论】:

    • useTextSelection 主题是textSelectionTheme 工作所必需的。谢谢!
    • 真正的mvp答案在这里! useTextSelectionTheme: true 成功了!
    • useTextSelection 主题不再需要
    【解决方案6】:

    cursorColor: Colors.white, 放入TextFormField 中

    【讨论】:

    • 此功能已弃用
    猜你喜欢
    • 2021-04-30
    • 2021-12-26
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 2022-08-15
    相关资源
    最近更新 更多