uloc 1.0.8 copy "uloc: ^1.0.8" to clipboard
uloc: ^1.0.8 copied to clipboard

A lifecycle-aware routing library for Flutter. Supports named routes, Provider integration, code generation, and page lifecycle hooks like onInit, onReady, and onDispose.

πŸ”— ULoC β€” UI-Logic-Controller / Router for Flutter #

ULoC is a developer-friendly tool that combines routing, logic injection, and screen scaffolding into one seamless workflow for Flutter.
It's designed to work perfectly with Provider, and follows scalable patterns like MVC or MVVM.

Stop wiring up routes manually. Let ULoC handle it, while you focus on logic and design.


🧭 Overview #

  • πŸ”§ Based on provider for easy state and logic injection
  • πŸ” Auto-generates Routes and ULoC map from a @ULoCDeclaration
  • 🧱 Scaffolds new screens with controller + view files
  • 🧬 Lifecycle hooks (onInit, onReady, onDispose)
  • πŸš€ Supports dynamic named routes with parameters β€” great for deep linking
  • βœ… Perfect for large projects and scalable architecture (MVC/MVVM)

πŸ“¦ Installation #

In your Flutter project:

dependencies:
  uloc: ^1.0.0

Then:

dart pub get

βš™οΈ CLI Commands #

// To install globally:
dart pub global activate uloc

// To install to package:
dart pub add uloc

// Print usage information.
uloc help
uloc -h
uloc --help

// Generate routing files.
// Generate routing files for the current project from ULoCDeclaration
// By default, the route declaration dir is lib/routes/routes.dart.
// the target file dir is lib/routes/routes.uloc.g.dart
uloc gen-route
uloc gr
uloc gen-route --dir lib/routes/routes.dart --target lib/routes/routes.uloc.g.dart
uloc gr -d lib/routes/routes.dart -t lib/routes/routes.uloc.g.dart

// Generate new widget page.
// By default, the route declaration dir is lib/app/screens/.
// the structure as below:
// lib/screens/home/
// β”œβ”€β”€ views/pages/home_page.dart
// └── controllers/home_controller.dart
uloc gen-page home
uloc gp book_detail --parameters id,title
uloc gp book_detail --parameters id --parameters title --gen-route --route-declaration-dir lib/routes/routes.dart --route-target-dir lib/routes/routes.uloc.g.dart
uloc gp home -g -r lib/routes/routes.dart -t lib/routes/routes.uloc.g.dart

✨ Route Declaration Example #

@ULoCDeclaration()
class MyRoutes extends ULoCRouteDeclaration {
  @override
  Map<String, ULoCRoute<ULoCProvider>> get route => {
    'HOME': ULoCRoute(
      route: '/',
      provider: (context, _) => HomeController(context),
      child: HomePage,
    ),
    'DETAIL': ULoCRoute(
      route: '/detail/:id/:name',
      provider: (context, params) =>
          DetailController(context, id: params?['id'], type: params?['type']),
      child: DetailPage,
    ),
  };
}

πŸ“„ Generated Output #

class Routes {
  Routes._();

  static const RouteName HOME = '/';
  static RouteName DETAIL({String? id, String? name}) => id == null && name == null ? '/detail/:id/:name' : '/detail/$id/$name';
}

/// use this to pass to [MaterialApp] Route setting
final ULoC uloc = ULoC([
  RouteProperties<HomeController>(
    routeName: Routes.HOME,
    provider: (context, _) => HomeController(context),
    child: HomePage(),
  ),
  RouteProperties<DetailController>(
    routeName: Routes.DETAIL(),
    provider: (context, params) => DetailController(context, id: params?['id'], type: params?['type']),
    child: DetailPage(),
  ),
]);

--

πŸ“„ Controller #

class DetailController extends ULoCProvider {
  final String? id;
  final String? type;
  DetailController(super.context, {this.id, this.type});
  String name = "Detail";
  String content = "Detail has not yet implemented";

  int count = 0;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onDispose() {
    super.onDispose();
  }

  void increment() {
    count++;
    setstate();
  }

  void decrement() {
    setstate(() {
      count--;
    });
  }
}

--

πŸ“„ View #

class DetailPage extends StatefulWidget {
  const DetailPage({super.key});

  @override
  State<DetailPage> createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  DetailController get watch => context.watch<DetailController>();
  DetailController get controller => context.read<DetailController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(watch.name)),
      body: Center(
        child: Text(watch.count.toString(), style: TextStyle(fontSize: 40)),
      ),
      floatingActionButton: Column(
        mainAxisSize: MainAxisSize.min,
        spacing: 8,
        children: [
          FloatingActionButton.small(
            heroTag: 'increment',
            onPressed: controller.increment,
            child: Icon(Icons.add),
          ),
          FloatingActionButton.small(
            heroTag: 'decrement',
            onPressed: controller.decrement,
            child: Icon(Icons.remove),
          ),
        ],
      ),
    );
  }
}


--

πŸ“„ Usage #

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ULoC Demo',
      initialRoute: Routes.HOME,
      routes: uloc.routes,
      onGenerateRoute: uloc.routeBuilder,
    );
  }
}


πŸ”„ Lifecycle Hooks #

Each controller can optionally define lifecycle methods: You can setstate() in controller Each time setstate() is called, Widgets what are watching will be rerendered

@override
void onInit() {
  fetchData();
}

@override
void onReady() {
  showDialog();
  accessContext();
}

@override
void onDispose() {
  removeResource()
}

🧠 Architecture Friendly #

ULoC fits into modern app structure:

  • MVC β€” Controller handles logic, View is UI
  • MVVM β€” Controller = ViewModel, View observes data changes
  • Clean separation between logic and UI

πŸ”— Deep Linking #

Named routes support :params like /user/:id. Navigate with:

context.pushNamed(Routes.Detail(id: '42'))

Works with Firebase Dynamic Links, URI parsers, etc.


❀️ Contributing #

Feel free to:

  • Submit bug reports or ideas
  • Open pull requests
  • Improve the ecosystem

πŸ“„ License #

MIT License Β© NGUYEN HAI DANG

4
likes
0
points
86
downloads

Publisher

verified publisherdannynguyen.vn

Weekly Downloads

A lifecycle-aware routing library for Flutter. Supports named routes, Provider integration, code generation, and page lifecycle hooks like onInit, onReady, and onDispose.

Homepage
Repository (GitHub)
View/report issues

Topics

#routing #codegen #lifecycle #provider #architecture

License

unknown (license)

Dependencies

args, flutter, provider

More

Packages that depend on uloc