Reputation: 601
On Android, I'm used to TextView's textIsSelectable attribute but I didn't see that in the Text docs.
Currently I'm using a TextField (editable) and not saving any changes to the displayed text. My primary need is to allow copy-paste.
Upvotes: 3
Views: 1076
Reputation: 7958
I solved this in one of my projects with the following (simplified) code:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
String _text = 'TestString';
/// Pastes given String to the clipboard and shows Popup-Snackbar
void copyToClipboard(String toClipboard) {
ClipboardData data = new ClipboardData(text: toClipboard);
Clipboard.setData(data);
_scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text(toClipboard + ' copied to clipboard.'),
));
}
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(
title: new Text('TestProject'),
),
body: new InkWell(
onLongPress: () => copyToClipboard(_text),
child: new Center(
child: new Text(_text),
),
),
);
}
}
So just wrap your text in a widget that can detect gestures or use a GestureDetector to call the clipboard method.
Hope it helps.
Upvotes: 5