Sub topic 2
Creating a Flutter Module
This chapter assumes you know basic Dart and Flutter widgets. You should also know how to run commands in a terminal and understand that an Android or iOS app has a native host project. The host project remains responsible for the existing app. The Flutter module provides the Flutter screens that the host can display.
What a Flutter module is
A Flutter module is a reusable Flutter project designed to be added to an existing Android or iOS application. A normal Flutter application owns its native runner. A module does not. Its native runner belongs to the host app that embeds it.
Use a module when you want to add Flutter screens gradually. For example, an existing shopping app could keep its native checkout flow while adding a Flutter product catalog. This approach avoids rewriting the entire application at once.
Create the module
Create the module beside the native application, not inside the native application's source directory:
flutter create --template module my_flutter_module
cd my_flutter_module
flutter runThe --template module option selects Flutter's module template. The command creates the my_flutter_module directory, initializes Dart package metadata, and generates supporting host projects for local development.
A module contains the files shown below.
| File or directory | Purpose |
|---|---|
| `lib/` | Dart and Flutter source code |
| `pubspec.yaml` | Module name, dependencies, assets, and SDK constraints |
| `.android/` | Generated Android host project used for development and integration support |
| `.ios/` | Generated iOS host project used for development and integration support |
The .android and .ios directories are hidden because their names begin with a dot. They are generated implementation details. Do not normally edit them directly. Native application configuration belongs in the real Android or iOS host project.
The flutter run command starts the module through a generated development host. This lets you build and test Flutter code before connecting the module to the existing app.
Add the first module screen
Open lib/main.dart and replace its contents with a small, complete Flutter application:
import 'package:flutter/material.dart';
void main() {
runApp(const CatalogModule());
}
class CatalogModule extends StatelessWidget {
const CatalogModule({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Catalog',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
useMaterial3: true,
),
home: const CatalogPage(),
);
}
}
class CatalogPage extends StatelessWidget {
const CatalogPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Catalog')),
body: const Center(
child: Text('Products from Flutter'),
),
);
}
}Run the module again after saving:
flutter runThe module's main() function is its default Dart entrypoint. An entrypoint is a Dart function where the Flutter engine starts executing the module. Later, a native host can choose another entrypoint for a different Flutter screen.
Understand the development boundary
The generated development host is useful for checking widgets, navigation, assets, and package dependencies. It is not the application that your users will install. The real Android or iOS host supplies its own application lifecycle, navigation, permissions, and native services.
Keep module code independent from native APIs when possible. When the Flutter screen needs host data, define a small interface rather than coupling the screen to a particular Activity or view controller. The host integration can then be added without changing the module's basic project structure.
Verify the module
Before integrating the module, check that it works independently:
- Run
flutter pub getif you add or change dependencies. - Run
flutter analyzeto find Dart and Flutter issues. - Run
flutter testwhen the module contains tests. - Run
flutter runon an available emulator or device.
Exercise
Why should you create a Flutter module beside the existing native app instead of treating it as the native app's replacement?
Show answer
The module supplies reusable Flutter code and screens, while the existing Android or iOS project remains responsible for the host application's lifecycle and native features.