Flutter Stuff

How to Add Selectable/Copy Text Widget in Flutter

**Adding a Selectable/Copy Text Widget in Flutter: A Step-by-Step Guide**

Are you tired of manually copying and pasting text from your Flutter app? Do you want to give your users the ability to select and copy text within your app? In this post, we’ll show you how to add a selectable/copy text widget in Flutter. It’s easier than you think!

**Step 1: Add the `rich_text` Package**

The first step is to add the `rich_text` package to your Flutter project. You can do this by updating your `pubspec.yaml` file:

“`yaml
dependencies:
flutter:
sdk: flutter
rich_text: ^0.0.2+1
“`

Then, run `flutter pub get` in your terminal to get the package.

**Step 2: Create a Rich Text Widget**

Create a new file for your widget and import the `rich_text` package:

“`dart
import ‘package:flutter/material.dart’;
import ‘package:rich_text/rich_text.dart’;

class SelectableText extends StatefulWidget {
final String text;
final TextStyle normalStyle;
final TextStyle selectedStyle;

const SelectableText({Key key, this.text, this.normalStyle, this.selectedStyle})
: super(key: key);

@override
_SelectableTextState createState() => _SelectableTextState();
}

class _SelectableTextState extends State {
@override
Widget build(BuildContext context) {
return RichText(
text: TextSpan(
text: widget.text,
style: widget.normalStyle,
recognizer: TapGestureRecognizer()
..onTap = () {
setState(() {
_selectAll();
});
},
),
);
}

void _selectAll() {
final text = FlutterClipboard.copyText(widget.text);
if (text) {
print(‘Text copied to clipboard’);
} else {
print(‘Failed to copy text’);
}
}
}
“`

In this code, we’re creating a `SelectableText` widget that takes three properties: `text`, `normalStyle`, and `selectedStyle`. The `normalStyle` property determines the appearance of the text when it’s not selected, and the `selectedStyle` property determines the appearance when it is selected. The `text` property is the actual text that will be displayed.

**Step 3: Use the SelectableText Widget**

Now, let’s use our `SelectableText` widget in a `Scaffold`:

“`dart
import ‘package:flutter/material.dart’;
import ‘package:your_app/selectable_text.dart’; // Replace with your app name

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Selectable Text’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text(‘Selectable Text’),
),
body: Center(
child: SelectableText(
text: ‘Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.’,
normalStyle: TextStyle(fontSize: 24.0),
selectedStyle: TextStyle(fontSize: 28.0, color: Colors.blue),
),
),
),
);
}
}
“`

Run your app and you should see the text displayed with a blue color when selected. When you tap on the text, it will copy the text to the clipboard.

That’s it! You’ve successfully added a selectable/copy text widget to your Flutter app.

Remember to customize the styles and behavior to fit your app’s design and requirements. Happy coding!

Leave a Comment

Scroll to Top