Flutter Widget 之SelectableText

1,556 阅读1分钟

你是否曾经想过要使用应用程序中的一些文本可被选择呢? SelectableText给你这样的功能

ezgif.com-gif-maker.gif

正如Text widget一样, SelectableText具有相同字段,使你可以设定一些像字体和颜色的东西

SelectableText(
    'my selectable text',
    style: TextStyle(color: Colors.blue),
);

你可以使用showCursor参数,显示选择的起点,使用cursorWidth,cursorRadius和cursorColor参数自定义光标的外观

SelectableText(
    'my selectable text',
    showCursor: true,
    cursorWidth: 5,
    cursorColor: Colors.green,
    cursorRadius: Radius.circlar(5),
);

image.png

要指定选择文本后可用的操作,使用toolbarOptions,默认情况下,可以选择所有文字并复制

SelectableText(
    'my selectable text',
    toolbarOptions: ToolbarOptions(copy: true),
);

image.png

可以添加onTap处理程序以相应单击手势

SelectableText(
    'my selectable text',
    onTap: () => print('that tickles!'),
);

如果有一些文字长度超过文本框,可向下滚动以选择更多文本,你可以使用scrollPhysics参数自定义滚动行为

SelectableText(
    'my selectable text',
    maxLines: 1,
    scrollPhysics: ClampingScrollPhysics(),
);

同样的,如果药使RichText可被选择,可使用.rich命名构造函数

SelectableText.rich('my selectable rich text')

如果想了解有关SelectableText的内容,或者关于Flutter的其他功能,请访问flutter.dev

原文翻译自视频:视频地址