Testo selezionabile in Flutter
In pratica Flutter ci mette a disposizione SelectableText per visualizzare del testo che possiamo selezionare, sia col mouse che con il tap.
Testato su Linux e Android.
Inoltre avremmo anche i tasti per il copy e select all.
Ecco un esempio completo:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Flutter test'),
),
body: Center(
child: SelectableText(
'TESTO SELEZIONABILE',
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 15),
showCursor: true,
contextMenuBuilder: (context, editableTextState) {
return AdaptiveTextSelectionToolbar(
anchors: editableTextState.contextMenuAnchors,
children: editableTextState.contextMenuButtonItems
.map((ContextMenuButtonItem buttonItem) {
return CupertinoButton(
onPressed: buttonItem.onPressed,
child: SizedBox(
width: 200.0,
child: Text(
CupertinoTextSelectionToolbarButton.getButtonLabel(
context,
buttonItem,
),
),
),
);
}).toList(),
);
},
),
),
);
}
}
Enjoy!
dart flutter selectabletext
Commentami!