'textSelectionHandleColor' 已弃用,不应使用

'textSelectionHandleColor' is deprecated and shouldn't be used

在 flutter 中,我已经这样定义了我的自定义主题

ThemeData(
  ...
  textSelectionColor: Colors.black,
  textSelectionHandleColor: Colors.white,
),

今天dart更新到v1.23.0-4版本后0.pre,我的代码发现了这个问题

'textSelectionHandleColor' is deprecated and shouldn't be used. Use TextSelectionThemeData.selectionHandleColor instead. This feature was deprecated after v1.23.0-4.0.pre.. Try replacing the use of the deprecated member with the replacement.

但我对如何使用 TextSelectionThemeData 感到困惑。

有人知道怎么做吗?谢谢!

您必须使用 textSelectionTheme 属性并将其设置为 TextSelectionThemeData

import 'package:flutter/material.dart';


void main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
        title: 'Flutter Demo App',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          accentColor: Color(0xffBA379B).withOpacity(.6),
          primaryColor: Color(0xffBA379B),
          textSelectionTheme: TextSelectionThemeData(
            selectionColor: Color(0xffBA379B).withOpacity(.5),
            cursorColor: Color(0xffBA379B).withOpacity(.6),
            selectionHandleColor: Color(0xffBA379B).withOpacity(1),
          ),
        ),
        home: Home(),

    );
  }

}

Flutter 版本现在直接使用 textSelectionHandleColor deprecated.But Flutter 添加了一种新的访问方式。

MaterialApp(
  title: 'My App',
  theme: ThemeData(
    primaryColor: Colors.red,
    textSelectionTheme: TextSelectionThemeData(
      selectionColor: Color(0xff35a19d),
      cursorColor: Color(0xff35a19d),
      selectionHandleColor: Color(0xff35a19d),
    ),
  ),
  home: MyWidget(),
);

您可以访问 Theme.of(context).textSelectionTheme.selectionHandleColor