How can I build a Chrome extension using Flutter? – Flutter

by
Ali Hasan
dart flutter google-chrome html

Quick Fix: To build a Chrome extension with Flutter, ensure you have the latest Flutter version and add web support. Modify index.html, manifest.json files accordingly. Build the extension with flutter build web, and load the unpacked extension in Chrome by enabling Developer mode.

The Problem:

Using Flutter, I would like to create a Chrome extension. As a Flutter developer, I’m unsure how to adapt my existing Flutter knowledge to the world of Chrome extensions. I’m looking for a structured and detailed guide that covers the process of building a Chrome extension using Flutter, including necessary modifications to project files like index.html and manifest.json to enable extension functionality.

The Solutions:

Solution 1: Creating a Flutter-based Chrome extension

To build a Chrome extension using Flutter, follow these steps:

1. Setting up the project:

  • Ensure you have the latest version of Flutter installed.
  • Run this command in the project’s top-level directory to add web support to your existing project:
flutter create --platforms web .

2. Modifying index.html:

  • Remove unsupported scripts from web/index.html.
  • Insert the script tag:
<script src="main.dart.js" type="application/javascript"></script>
  • Set extension view dimensions by modifying the starting <html> tag:
<html style="height: 600px; width: 350px">

3. Modifying manifest.json:

  • Replace the contents of web/manifest.json with the appropriate extension configuration.
  • Include essential fields like name, description, version, content security policy, default popup, and icon.

4. Building the extension:

  • Generate a Flutter web build using the HTML renderer and --csp flag:
flutter build web --web-renderer html --csp

5. Installing and testing the extension:

  • Load the unpacked extension by clicking on "Load unpacked" and selecting the build/web folder.
  • Enable Developer mode in Chrome.
  • Access the extension from the Chrome extension bar.

Q&A

How to structure a Flutter project to function as a Chrome extension?

Use a template to create a new Flutter project or add web support to existing project, install & set up necessary dependencies.

What modifications are needed in index.html for Chrome extension functionality?

Add Dart script, set extension view dimensions, and specify content security policy.

What modifications are needed in manifest.json for Chrome extension functionality?

Configure extension details, including name, description, version, content security policy, default popup, and icon.

Video Explanation:

The following video, titled "Flutter Chrome Extension - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Learn how to make flutter chrome extension. More here https://www.dbestech.com/tutorials/how-to-make-flutter-chrome-extension Buy me a ...