In Flutter, load the locally built ReactJS application which contains JavaScript code that initializes the Draft editor and Codox client module.
In Flutter, fetch the initial state from the backend and initialize the Draft editor.
In Flutter, set up listeners (callbacks) to respond to Codox client library session and error events.
Start Codox session
When Codox triggers hooks in the JavaScript code, corresponding callbacks in Flutter are invoked, passing the necessary data.
Step-by-Step Integration Tutorial
Step 1: Set Up Local Files
The first step is to prepare the local files for your project. Create a directory named assets/ in the root of your Flutter project to store HTML and JavaScript files.
Step 2: Prepare a ReactJS App with Draft and Codox
Check Node.js installation: Install and download Node.js.
Clone the starter project: Use the following command to clone the a React + Draft + Codox starter project:
Install dependencies:
Navigate to the project directory and install the required dependencies:
Configure Codox settings:
Update the codoxConfig variable in src/lexical/App.jsx with your unique docId, username, and apiKey:
Note: In this example, the codoxConfig is defined inside the React app. You can also pass these parameters Flutter app into the ReactJS app for more dynamic control.
Build the ReactJS app:
Run the following command to build the project:
Copy the build to assets:
Copy the generated build directory to the assets/ directory of your Flutter project and rename it to draft/:
Your assets/ directory should now look like this:
Step 3: Initialize InAppWebView in Flutter
To display the ReactJS app in Flutter, we will use the InAppWebView plugin. The following code in lib/main.dart sets this up:
Step 4: Initialize the Editor State
Before starting or joining a Codox session, the latest version of the document content must be available to the Draft editor component. The following code shows one way to set up this up from the Flutter app.
Add an initialization function in your ReactJS app to expose a method for Flutter to set up the editor with initial content. You can use the following window.initDraftEditor code from the ReactJS starter project
Then, invoke this function from Flutter after the page has been fully loaded using the onLoadStop callback from InAppWebView .
Step 5: Start Codox Session
After the initial Draft editor state is set up, a Codox session should launch automatically based on the parameters in the config object discussed in Step 2.
Optional: Codox hooks and error events
You can listen to events from Codox to allow the Flutter app to respond to content change notifications, member change notifications, and errors triggered within the Codox session.
On the Flutter side you need to setup handlers to manage JavaScript messages sent from Codox, like the following example.