How to Build a Custom Chrome Extension with Flutter: A Beginner’s Guide

Sandeep Singh (Full Stack Dev.)
5 min readDec 29, 2022

--

How to build a custom chrome extension with Flutter

Are you looking to take your web development skills to the next level? Have you considered building a Chrome extension?

With the power of Flutter, it’s easier than ever to create custom, interactive extensions that enhance the user experience of the Chrome browser.

In this post, we’ll walk through the steps of building a Chrome extension with Flutter, from setting up your development environment to packaging your extension for distribution.

Whether you’re a seasoned web developer or just starting out, this tutorial will provide you with the tools you need to create your own Chrome extension using Flutter.

How to create a chrome extension with Flutter web?

Here is an example of how you might create a simple Chrome extension using Flutter:

First, create a new Flutter project in your code editor and create a new directory called “extension” within the project directory.

Next, create a file called “manifest.json” in the “extension” directory with the following contents:

{
"manifest_version": 2,
"name": "My Extension",
"description": "A simple extension built with Flutter",
"version": "1.0",
"permissions": [
"activeTab"
],
"browser_action": {
"default_popup": "index.html"
}
}

This file contains the metadata for your extension, including its name, description, and permissions. It also specifies that the extension will have a browser action that opens a popup when clicked, and that the HTML for the popup will be stored in a file called “index.html”.

Also Read:

Next, use Flutter to build the UI for your extension. You can do this by creating a new Flutter widget in the “lib” directory of your project and building the interface using Flutter’s built-in widgets. For example:

import 'package:flutter/material.dart';
class MyExtension extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("My Extension"),
),
body: Center(
child: Text("Hello, World!"),
),
);
}
}

To display this widget in the extension’s popup, you will need to build the Flutter app and then create an HTML file that loads the compiled JavaScript code.

Next, create an HTML file called “index.html” in the “extension” directory with the following contents:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My Extension</title>
</head>
<body>
<div id="root"></div>
<script src="main.dart.js"></script>
</body>
</html>

This HTML file creates a div element with an ID of “root,” which will be used as the container for your Flutter app. It also loads the compiled JavaScript code for your Flutter app using the script tag.

Finally, create a file called “background.js” in the “extension” directory with the following contents:

chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.create({url: "index.html"});
});

This code listens for a click event on the extension’s browser action and opens the extension’s popup when the button is clicked.

If you wants a dedicated tutorial video on this topic, then just comment down and I will make it as fast as I can!

To build the Flutter app, run the following command in the terminal:

flutter build web

This will create a directory called “build” in your project, containing the compiled JavaScript code for your Flutter app.

🟢 Now RUN this command:

flutter build web --web-renderer html --csp

You will find the generated files inside build/web folder present in your root Flutter project directory.

How to Test the Extension?

To test your extension, go to the Chrome Extension Management page (chrome://extensions), enable “Developer mode,” and use the “Load Unpacked” feature to load your extension. Once you are satisfied with your extension, you can package it for distribution by selecting “Pack Extension” from the Chrome Extension Management page.

1. Enable the Developer mode toggle present in the top-right corner of the webpage.

2. Click Load unpacked.

3. Select the <flutter_project_dir>/build/web folder.

You will see that the new extension is now added to that page.

The extension will get automatically installed, you will be able to access it just like any regular extension by clicking on the extension icon present in the top bar (it can also be pinned for easy access).

I hope this example helps you understand the process of building a Chrome extension with Flutter. Let me know if you have any questions or if you need further assistance.

If you are Looking to break into the exciting world of Flutter App development? Look no further! This gift article is packed with the latest techniques and insights to help you get started on your journey. Whether you’re a beginner or an experienced developer, you’ll find valuable information and resources to take your skills to the next level. So don’t wait — start learning and mastering Flutter today with this amazing resource!

Conclusion

In this post, we’ve learned how to use Flutter to build a custom Chrome extension. We’ve covered the basics of setting up a development environment, creating a manifest file to define the extension’s metadata, building a user interface with Flutter, and using the Chrome Extension API to interact with the Chrome browser. By following these steps, you should now have the skills and knowledge to create your own Chrome extension using Flutter. Whether you’re looking to add extra functionality to your favorite browser or just want to explore the world of web development, building a Chrome extension with Flutter is a great way to dive in and start creating. Thanks for following along, and happy coding!

If you find this article useful for you, please subscribe us for getting such amazing articles daily. Happy coding 😊🧑‍💻

If you wants a dedicated tutorial video on this topic, then just comment down and I will make it as fast as I can!

--

--

Sandeep Singh (Full Stack Dev.)
Sandeep Singh (Full Stack Dev.)

Written by Sandeep Singh (Full Stack Dev.)

Elite Freelancer | I Build secure and scalable web solutions for businesses

Responses (1)