Flutter micro app - A package to speed up the creation of micro frontend(or independent features) structure in Flutter applications

Overview

A package to speed up the creation of micro frontend(or independent features) structure in Flutter applications (beta version)


Screen Shot 2022-02-03 at 00 32 35

Navigation between pages

Use [NavigatorInstance] to navigate between pages

NavigatorInstance.pop();
NavigatorInstance.pushNamed();
NavigatorInstance.pushNamedNative();
NavigatorInstance.pushReplacementNamed();
NavigatorInstance ...

Open native (Android/iOS) pages, in this way

It needs native implementation, you can see an example inside android folder

// If not implemented, always return null
final isValidEmail = await NavigatorInstance.pushNamedNative<bool>(
    'emailValidator',
    arguments: 'validateEmail:[email protected]'
);
print('Email is valid: $isValidEmail');
// Listen to all flutter navigation events
NavigatorInstance.eventController.flutterLoggerStream.listen((event) {
    logger.d('[flutter: navigation_log] -> $event');
});

// Listen to all native (Android/iOS) navigation events (if implemented)
NavigatorInstance.eventController.nativeLoggerStream.listen((event) {});

// Listen to all native (Android/iOS) navigation requests (if implemented)
NavigatorInstance.eventController.nativeCommandStream.listen((event) {});

Define micro app configurations and contracts

Configure the preferences (optional)

  MicroAppPreferences.update(
    MicroAppConfig(
      nativeEventsEnabled: true, // If you want to dispatch and listen to events between native(Android/iOS) [default = false]
      pathSeparator: MicroAppPathSeparator.slash // It joins the routes segments using slash "/" automatically
    )
  );

Register all routes

This is just a suggestion of routing strategy (Optional) It's important that all routes are availble out of the projects, avoiding dependencies between micro apps. Create all routes inside a new package, and import it in any project as a dependency. This will make possible to open routes from anywhere in a easy and transparent way.

Create the routing package: flutter create template=package micro_routes

// Export all routes
class Application1Routes extends MicroAppRoutes {
  @override
  MicroAppBaseRoute get baseRoute => MicroAppBaseRoute('application1');
  String get page1 => baseRoute.path('page1');
  String get page2 => baseRoute.path('page2', ['segment1', 'segment2']);
}

For example, you can open a page that is inside other MicroApp, in this way:

NavigatorInstance.pushNamed(OtherMicroAppRoutes().specificPage);
NavigatorInstance.pushNamed(Application1Routes().page1);

Expose all pages throuth a contract MicroApp (Inside external projects or features folder)

import 'package:micro_routes/exports.dart';

class Application1MicroApp extends MicroApp with Application1Routes {

  @override
  List<MicroAppPage> get pages => [
        MicroAppPage(name: baseRoute.name, builder: (context, arguments) => const Initial()),
        MicroAppPage(name: page1, builder: (context, arguments) => const Page1()),
        MicroAppPage(name: page2, builder: (context, arguments) {
            final page2Params.fromMap(arguments);
            return Page2(params: page2Params);
        }),
      ];
}

Initialize the host, registering all micro apps

  • MicroHost is also a MicroApp, so you can register pages here too.
  • MyApp needs to extends MicroHostStatelessWidget or MicroHostStatefulWidget
  • The MicroHost is the root widget, and it has all MicroApps, and the MicroApps has all Micro Pages.
void main() {
    runApp(MyApp());
}

class MyApp extends MicroHostStatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      navigatorKey: NavigatorInstance.navigatorKey, // Required
      onGenerateRoute: onGenerateRoute, // [onGenerateRoute] this is created automatically, so just use it, or override it, if needed.
      initialRoute: baseRoute.name,
      navigatorObservers: [
        NavigatorInstance // Add NavigatorInstance here, if you want to get didPop, didReplace and didPush events
      ],
    );
  }

  // Base route of host application
  @override
  MicroAppBaseRoute get baseRoute => MicroAppBaseRoute('/');

  // Register all root [MicroAppPage]s here
  @override
  List<MicroAppPage> get pages => [
        MicroAppPage(name: baseRoute.name, builder: (_, __) => const HostHomePage())
      ];

  // Register all [MicroApp]s here
  @override
  List<MicroApp> get microApps => [MicroApplication1(), MicroApplication2()];
}

Handling micro apps events

Dispatching events

// dispatching in channel "user_auth", only
MicroAppEventController()
    .emit(const MicroAppEvent(
        name: 'my_event',
        payload: {'data': 'lorem ipsum'},
        channels: ['user_auth'])
    );

Listen to events (MicroApp)s

// It listen to all events
@override
  MicroAppEventHandler? get microAppEventHandler =>
      MicroAppEventHandler((event) => logger.d([ event.name, event.payload]));

// It listen to events with id equals 123, only!
@override
  MicroAppEventHandler? get microAppEventHandler =>
      MicroAppEventHandler((event) {
        logger.d([ event.name, event.payload]);
      }, id: '123');

// It listen to events with channels "chatbot" and "user_auth"
@override
  MicroAppEventHandler? get microAppEventHandler =>
      MicroAppEventHandler((event) {
        // User auth feature, asked to show a popup :)
        myController.showDialog(event.payload);
      }, channels: ['chatbot', 'user_auth']);

Managing events

MicroAppEventController().unregisterHandler(id: '123');
MicroAppEventController().unregisterHandler(channels: ['user_auth']);
MicroAppEventController().pauseAllHandlers();
MicroAppEventController().resumeAllHandlers();
MicroAppEventController().unregisterAllHandlers();

Initiating an event subscription anywhere in the application (inside a StatefulWidget, for example)

Using subscription

final subscription = MicroAppEventController().stream.listen((MicroAppEvent event) {
    logger.d(event);
  });

// later, in dispose method of the widget
@override
void dispose() {
    subscription.cancel();
    super.dispose();
}

Using handler

MicroAppEventController().registerHandler(MicroAppEventHandler(id: '1234'));

// later, in dispose method of the widget
@override
void dispose() {
    MicroAppEventController().unregisterSubscription(id: '1234');
    super.dispose();
}

Overriding onGenerateRoute method

If it fails to get a page route, ask for native(Android/iOS) to open the page

  @override
  Route? onGenerateRoute(RouteSettings settings, {bool? routeNativeOnError}) {
    //! If you wish native app receive requests to open routes, IN CASE there
    //! is no route registered in Flutter, please set [routeNativeOnError: true]
    return super.onGenerateRoute(settings, routeNativeOnError: true);
  }

If it fails to get a page route, show a default error page

  @override
  Route? onGenerateRoute(RouteSettings settings, {bool? routeNativeOnError}) {
    
    final pageRoute = super.onGenerateRoute(settings, routeNativeOnError: false);

    if (pageRoute == null) {
       // If pageRoute is null, this route wasn't registered(unavailable)
       return MaterialPageRoute(
           builder: (_) => Scaffold(
                 appBar: AppBar(),
                 body: const Center(
                   child: Text('Page Not Found'),
                 ),
            ));
    }
    return pageRoute;
  }
     

The following table shows how Dart values are received on the platform side and vice versa

Dart Kotlin Swift Java
null null nil null
bool Boolean NSNumber(value: Bool) java.lang.Boolean
int Int NSNumber(value: Int32) java.lang.Integer
int, if 32 bits not enough Long NSNumber(value: Int) java.lang.Long
double Double NSNumber(value: Double) java.lang.Double
String String String java.lang.String
Uint8List ByteArray FlutterStandardTypedData(bytes: Data) byte[]
Int32List IntArray FlutterStandardTypedData(int32: Data) int[]
Int64List LongArray FlutterStandardTypedData(int64: Data) long[]
Float32List FloatArray FlutterStandardTypedData(float32: Data) float[]
Float64List DoubleArray FlutterStandardTypedData(float64: Data) double[]
List List Array java.util.ArrayList
Map HashMap Dictionary java.util.HashMap

Comments
  • implement distinct logic to lists

    implement distinct logic to lists

    https://github.com/emanuel-braz/flutter_micro_app/blob/59251675304370aba5fb54079beb3121bb80d18e/lib/src/presentation/widgets/micro_widget_builder.dart#L35

    opened by emanuel-braz 1
  • Feature/on route not registered

    Feature/on route not registered

    Summary (Feature / Bugfix description)

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [ ] Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [x] Did you update the changelog file?
    • [x] Did you update pubspec.yaml?
    • [ ] Have you written new tests for your core changes, as applicable?

    Issues (if applicable)

    opened by emanuel-braz 0
  • Release Candidate 0.11.0

    Release Candidate 0.11.0

    Summary (Feature / Bugfix description)

    • Make MicroAppEvent property name as optional

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [ ] Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [x] Did you update the changelog file?
    • [x] Did you update pubspec.yaml?
    • [ ] Have you written new tests for your core changes, as applicable?

    Issues (if applicable)

    opened by emanuel-braz 0
  • Make MicroAppEvent property name as optional

    Make MicroAppEvent property name as optional

    Summary (Feature / Bugfix description)

    • Make MicroAppEvent property name as optional

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [ ] Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [x] Did you update the changelog file?
    • [x] Did you update pubspec.yaml?
    • [ ] Have you written new tests for your core changes, as applicable?

    Issues (if applicable)

    opened by emanuel-braz 0
  • Add generic micro event controller

    Add generic micro event controller

    Summary (Feature / Bugfix description)

    • add generic micro event controller
    • add webview controllers to micro board

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [ ] Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [x] Did you update the changelog file?
    • [x] Did you update pubspec.yaml?
    • [ ] Have you written new tests for your core changes, as applicable?

    This PR closes #11

    webview 
    opened by emanuel-braz 0
  • Add GenericMicroAppEventController in order to enable webview controllers

    Add GenericMicroAppEventController in order to enable webview controllers

    Summary (Feature / Bugfix description)

    • Add GenericMicroAppEventController in order to enable webview controllers

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [ ] Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)
    opened by emanuel-braz 0
  • Release 0.9.0

    Release 0.9.0

    Summary (Feature / Bugfix description)

    Break

    • registerEventHandler method is available by mixins HandlerRegisterMixin and HandlerRegisterStateMixin
    • MicroAppEventHandler is no required to micro apps
    • Micro apps requires a name

    Added

    • Add micro board - A page to show all micro applications, routes and event handlers

    Chore

    • Supports both Flutter version 2 and version 3

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [x] Breaking change (fix or feature that would cause existing functionality to change)
    • [x] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [x] Did you update the changelog file?
    • [x] Did you update pubspec.yaml?
    • [ ] Have you written new tests for your core changes, as applicable?
    rc 
    opened by emanuel-braz 0
  • :robot: Bump version 0.9.0

    :robot: Bump version 0.9.0

    Summary (Feature / Bugfix description)

    • bump version 0.9.0
    • Supports both Flutter version 2 and version 3

    This PR fixes/implements (types of changes)

    • [x] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [x] Did you update the changelog file?
    • [x] Did you update pubspec.yaml?
    auto-pr 
    opened by github-actions[bot] 0
  • Bump version 0.9.0

    Bump version 0.9.0

    Summary (Feature / Bugfix description)

    • bump version 0.9.0
    • Supports both Flutter version 2 and version 3

    This PR fixes/implements (types of changes)

    • [x] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [x] Did you update the changelog file?
    • [x] Did you update pubspec.yaml?
    opened by emanuel-braz 0
  • :robot: Add micro board

    :robot: Add micro board

    Summary (Feature / Bugfix description)

    • Add micro app dashboard

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [ ] Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [ ] Did you update the changelog file?
    • [ ] Did you update pubspec.yaml?
    • [ ] Have you written new tests for your core changes, as applicable?
    auto-pr 
    opened by github-actions[bot] 0
  • Add micro board

    Add micro board

    Summary (Feature / Bugfix description)

    • Add micro app dashboard

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [ ] Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [ ] Did you update the changelog file?
    • [ ] Did you update pubspec.yaml?
    • [ ] Have you written new tests for your core changes, as applicable?
    opened by emanuel-braz 0
  • Handle native event controller

    Handle native event controller

    pause(app_event, navigation_command, navigation_log) resume(app_event, navigation_command, navigation_log)

    //suggestion
    pauseNative({String eventMethod = 'app_event'});
    
    pauseWeb({String eventMethod = 'app_event'});
    
    opened by emanuel-braz 0
Releases(0.12.0)
  • 0.12.0(Aug 28, 2022)

    What's Changed

    • Feature/on route not registered by @emanuel-braz in https://github.com/emanuel-braz/flutter_micro_app/pull/44
    • add onRouteNotRegistered callback by @emanuel-braz in https://github.com/emanuel-braz/flutter_micro_app/pull/45

    Full Changelog: https://github.com/emanuel-braz/flutter_micro_app/compare/0.11.0...0.12.0

    Source code(tar.gz)
    Source code(zip)
  • 0.11.0(Aug 14, 2022)

    What's Changed

    • Make MicroAppEvent property name as optional by @emanuel-braz in https://github.com/emanuel-braz/flutter_micro_app/pull/42
    • Release Candidate 0.11.0 by @emanuel-braz in https://github.com/emanuel-braz/flutter_micro_app/pull/43

    Full Changelog: https://github.com/emanuel-braz/flutter_micro_app/compare/0.10.0...0.11.0

    Source code(tar.gz)
    Source code(zip)
  • 0.10.0(Jul 23, 2022)

  • 0.8.0(May 26, 2022)

    Summary (Feature / Bugfix description)

    Break

    • Using Map as default data transfer object, instead string

    Fix

    • Enable/disable native requets in navigation events

    Added

    • Parses event data from native platform
    • Add optional timeout parameter, but still can use timeout in the old way as well

    This PR fixes/implements (types of changes)

    • [x] New feature (non-breaking change which adds functionality)
    • [x] Bug fix (non-breaking change which fixes an issue)
    • [x] Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] Chore, changes that do not affect the software behavior (docs, build process, configs, structures)

    Requirements (if applicable)

    • [x] Have you lint your code locally prior to submission?
    • [x] All new and existing tests passed?
    • [x] Did you update the changelog file?
    • [x] Did you update pubspec.yaml?
    • [x] Have you written new tests for your core changes, as applicable?
    Source code(tar.gz)
    Source code(zip)
  • 0.7.0(May 3, 2022)

    0.7.0

    [2022-05-03]

    Break

    • WidgetPageBuilder (arguments:dynamic) parameter becomes (settings:RouteSettings)

    Added

    • Separation of native events enablers
    • Make optional the widgetbuilder channels

    What's Changed

    • It makes channels as optional by @emanuel-braz in https://github.com/emanuel-braz/flutter_micro_app/pull/27
    • WidgetPageBuilder (arguments:dynamic) parameter becomes (settings:RouteSettings) by @emanuel-braz in https://github.com/emanuel-braz/flutter_micro_app/pull/28
    • Separation of native events enablers by @emanuel-braz in https://github.com/emanuel-braz/flutter_micro_app/pull/29

    Full Changelog: https://github.com/emanuel-braz/flutter_micro_app/compare/0.6.0...0.7.0

    Source code(tar.gz)
    Source code(zip)
  • 0.6.0(Apr 28, 2022)

  • 0.5.0(Feb 20, 2022)

    Added

    • Add event handler list to mixin
    • close navigator when firstpage pops
    • add transitions for navigation
    • fix lint warnings
    • add navigator widget
    • add maNav to BuildContext extension

    Break

    • change route to pageBuilder
    • getFragment becomes getPageBuilder
    Source code(tar.gz)
    Source code(zip)
  • 0.4.0(Feb 13, 2022)

Owner
Emanuel Braz
Problem-solver and customer-focused engineer🤘
Emanuel Braz
Jannis 0 Jan 29, 2022
Simple Dart package with build-in code generation. It simplifies and speedup creation of cache mechanism for dart classes.

Simple Dart package with build-in code generation. It simplifies and speedup creation of cache mechanism for dart classes.

iteo 37 Jan 2, 2023
Pdf creation module for dart/flutter

Pdf for Dart and Flutter This set of plugins allows Flutter apps to generate and print pdf files to the device printer. This plugin works for iOS and

David PHAM-VAN 954 Jan 3, 2023
A cross-platform Fediverse client for micro-blogging services written in Flutter/Dart.

Kaiteki A 快適 (kaiteki) Fediverse client for microblogging instances, made with Flutter and Dart. Currently, Kaiteki is still in a proof-of-concept/alp

Kaiteki 141 Jan 5, 2023
A Dart micro-library for asserting at build time.

A micro-library for asserting at build time. It's a common practice to configure a Dart app using const (build-time) values. There are benefits to thi

Christopher Casey 17 Oct 14, 2022
ANSI escape sequences and styling micro-library written in fluent/modern Dart.

neoansi ANSI escape sequences and styling micro-library written in fluent/modern Dart. This library provides minimal ANSI escape sequences and helpers

Neo Dart 8 Oct 31, 2022
Flutter package to render a Material Design Speed Dial.

Flutter Speed Dial Flutter package to render a Material Design Speed Dial. Usage The SpeedDial widget is built to be placed in the Scaffold.floatingAc

null 0 May 20, 2022
Flutter App Templete is a project that introduces an approach to architecture and project structure for developing Flutter apps.

Flutter App Template "Flutter App Template" is a project that introduces an approach to architecture and project structure for developing Flutter apps

Altive 126 Jan 5, 2023
Flutter template project - Simple ToDo app with scalable project structure.

Flutter Template Flutter template project - A simple TODO list app. This template provides simple UI and scalable project structure that goes beyond t

Web Factory LLC 128 Nov 21, 2022
A flutter demo app to practice Map data structure and its common operations

Map Operations A flutter demo app to practice Map data structure and its common operations Developer Alexander Sosa (https://www.linkedin.com/in/alexa

Alexander Sosa 0 Jan 3, 2022
A movies app made with Flutter focused on solid software structure patterns.

Flutter Movies App An application made with Flutter to practice the principles of Clean Architecture. Even being focused on architecture, the project

Márcio Valim 59 Dec 12, 2022
SIMPLE TODO APP WITH FEATURE WISE FOLDER STRUCTURE

SIMPLE TODO APP WITH FEATURE WISE FOLDER STRUCTURE

Khadga shrestha 0 May 13, 2022
Flutter Events App Ui Challenge Speed Code

Flutter Events app made with Flutter, Hosted on Codemagic. Don't forget to star ⭐ the repo it motivates me to share more open source Design Credits Uv

Sanskar Tiwari 402 Dec 29, 2022
Doctor booking app - Flutter Ui Challenge Speed Code

Installation flutter pub get Usage flutter run Live Demo : Flutter Doctor Booking App Web Demo Try Android APK : Download From Google Drive Dotor Bo

Sanskar Tiwari 521 Jan 3, 2023
Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to developing Flutter apps.

Flutter Architecture Blueprints Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to dev

Katsuyuki Mori 2 Apr 9, 2022
Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to developing Flutter apps.

Flutter Architecture Blueprints Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to dev

Daichi Furiya 1.5k Dec 31, 2022
Flutter-Wings - This is a structure for flutter developers developed by Invention Technology.

Flutter : Wings File Structure About Wings installation simple use Advance Use About Wings Wings is an MVC file structure build with getx for flutter

Invention Technology 20 Nov 20, 2022
Project Structure Auth Generator For Flutter

get_structure_generator Project Structure Auto Generator This is not the package

Mitul Vaghasiya 6 Mar 22, 2022