提问人:Microbob 提问时间:8/14/2023 更新时间:8/14/2023 访问量:158
在 SelectionArea 中设置所选文本
Set selected text in SelectionArea
问:
我了解如何阅读 中的选定文本,但我也想进行选择。给定对 的访问权限,如何以编程方式设置它所选择的内容?SelectionArea
SelectionArea
谢谢!
答:
1赞
Franklin Diaz
8/14/2023
#1
据我所知,SelectionArea 没有一个简单的方法来做到这一点,或者至少我没有看到关于它的文档,我已经看到了在 TextField 中可以做什么,在这里我给你举个例子。
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final TextEditingController controller = TextEditingController()
..text = 'Hello World example';
final FocusNode focusNode = FocusNode();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: const Text('Material App Bar'),
),
body: Center(
child: Column(
children: [
TextField(
focusNode: focusNode,
controller: controller,
),
TextButton(
onPressed: () {
//focus
focusNode.requestFocus();
//select all
controller.selection = TextSelection(
baseOffset: 0,
extentOffset: controller.text.length,
isDirectional: true);
},
child: const Text('Select All'))
],
),
),
),
);
}
}
上一个:文本选择句柄始终可见
评论