I understand how to read the selected text in a SelectionArea
, but I would also like to make selections as well. Given access to a SelectionArea
, how do I programmatically set what it is selecting?
Thanks!
as far as I know SelectionArea does not have an easy way to do that or at least I have not seen documentation about it, , where I have seen what can be done in the TextFields, here I leave you an example.
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'))
],
),
),
),
);
}
}