如何在本地更改选定的文本颜色 - flutter

Ham*_*mza 5 dart flutter flutter-layout

可以通过全局设置主题来更改文本选择颜色:

    theme: ThemeData.light().copyWith(
        accentColor: kPrimaryAccent,
        primaryColor: kPrimaryColor,
        textSelectionColor: kTextSelectionColor,
        textSelectionHandleColor: kPrimaryAccent)
Run Code Online (Sandbox Code Playgroud)

但是如何在单个文本字段中本地完成此操作呢?

rek*_*ire 6

由于Mobina答案中提到的属性已被弃用,因此您可以在 2022 年初执行以下操作:

Container(
  child: Theme(
    data: ThemeData(
      textSelectionTheme: const TextSelectionThemeData(
        cursorColor: Colors.yellow,
        selectionColor: Colors.green,
        selectionHandleColor: Colors.blue,
      )
    ),
    child: SelectableText(
      'this is a text',
    ),
  ),
),
Run Code Online (Sandbox Code Playgroud)

来源:TextSelectionTheme 迁移