Adding text selection action in android for Flutter apps.

Adding text selection action in android for Flutter apps.

Have you ever wanted to give your user a shortcut, for opening the selected text in your flutter app from anywhere in the android environment?

Like this:

Image for post

If yes, then you got yourself to the right place. In this article, I’m going to talk about how this can be implemented in flutter with the help of a pub plugin action_process_text, I wrote.

Let’s get started.

First, let’s create our flutter project.

flutter create action_process_text_example</span>

Now, in the pubspec.yaml file, add the package

dependencies:
  action_process_text: <latest version></span>

Run pub get and get packages.

Now add the following code to AndroidManifest.xml file in the android\app\src\main\ folder just after the main activity.

The android:label here is the String that will be displayed on selecting the text. So, change that to something relevant to what your app does.

Now that we have completed all the setup, let’s take a look at the usage.

  1. Import the package.
**import** 'package:action_process_text/action_process_text.dart';</span>

2. Write an async function for starting up the plugin in the Constructor of the StatefulWidget, which will assign the selected text to a variable with the help of await.

This package currently works only in aStatefulWidget as setState() has to be called.

The ActionProcessText.getInputText is used to get the selected text when the action is performed, otherwise return a blank space ‘’ . It returns a future, so await should be used in a variable declaration.

Then, setState() is performed to rebuild the page, as it would have already started building, without waiting for the inputText to get the future.

The inputText can also be re-used for other purposes when the action is not performed, and InputTextController can also be used instead.

The output is

finalactionprocesstextexample.gif

Yahoo! We have done it.


You can find this example project on my repository in GitHub: action_process_text_example .

And the package on: action_process_text

with the source file on: action_process_text_source .

Everyone is welcome to report issues/bugs and help me in making the package more awesome.

HAPPY CODING!


If you encountered any issues while following this article or you have a suggestion to make, please leave a comment below and I’ll try my best to reply you.

Thanks for your time 🙌