arch 0.0.1+6 copy "arch: ^0.0.1+6" to clipboard
arch: ^0.0.1+6 copied to clipboard

discontinued
outdated

MVVM architecture components for Flutter. Helps developers build loosely-coupled applications.

example/lib/main.dart

import 'package:arch/arch.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp(AppInitializer()));

class AppInitializer implements Initializer {
  @override
  void registerTypes(ServiceRegistry registry) {
    registry
      ..registerForNavigation(
        view: () => HomeView(),
        viewModel: () => HomeViewModel(registry.resolve<NavigationService>()),
        viewName: 'HomeView',
      )
      ..registerForNavigation(
        view: () => SecondView(),
        viewModel: () => SecondViewModel(
          registry.resolve<NavigationService>(), // NavigationService and DialogService are automatically registered
          registry.resolve<DialogService>(),
        ),
        // This will default to the view's type name `SecondView`.
      );
  }
}

class ParameterNames {
  static const messageFromHome = 'messageFromHome';
  static const messageFromSecond = 'messageFromSecond';
}

class MyApp extends Application {
  MyApp(Initializer initializer) : super(initializer);

  @override
  void onInitialize() {
    print('Initialized');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Arch Demo',
      navigatorKey: NavigationService.navigatorKey, // IMPORTANT
      theme: ThemeData(primarySwatch: Colors.blue),
      home: HomeView(),
    );
  }
}

class HomeView extends StatefulWidget {
  HomeView() : super(key: Key('HomeView'));

  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends ViewStateBase<HomeView, HomeViewModel> {
  @override
  Widget buildView(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(viewModel.title)),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times: ${viewModel.counter}'),
            RaisedButton(
              child: Text('Go to Second View'),
              onPressed: viewModel.navigate,
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => viewModel.counter++,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class HomeViewModel extends ViewModelBase {
  HomeViewModel(NavigationService navigationService) : super(navigationService: navigationService);

  String title = 'Home View';

  int _counter = 0;
  int get counter => _counter;
  set counter(int value) {
    if (_counter != value) {
      _counter = value;
      notifyListeners('counter');
    }
  }

  Future<void> navigate() async {
    final result = await navigationService.push('SecondView', {ParameterNames.messageFromHome: 'Hello'});
    if (result?.containsKey(ParameterNames.messageFromSecond) == true) {
      print(result[ParameterNames.messageFromSecond]);
    }
  }
}

class SecondView extends ViewBase<SecondViewModel> {
  SecondView() : super(key: Key('SecondView'));

  @override
  Widget buildView(BuildContext context, SecondViewModel viewModel) {
    return Scaffold(
      appBar: AppBar(title: Text(viewModel.title)),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('Go Back'),
              onPressed: viewModel.goBack,
            ),
            RaisedButton(
              child: Text('Show Alert'),
              onPressed: viewModel.alert,
            ),
          ],
        ),
      ), //
    );
  }
}

class SecondViewModel extends ViewModelBase {
  SecondViewModel(NavigationService navigationService, DialogService dialogService)
      : super(navigationService: navigationService, dialogService: dialogService);

  final title = 'Second View';

  @override
  Future<void> init([Map<String, Object> parameters]) async {
    if (parameters?.containsKey(ParameterNames.messageFromHome) == true) {
      print(parameters[ParameterNames.messageFromHome]);
    }
  }

  void goBack() => navigationService.pop({ParameterNames.messageFromSecond: 'Hi!'});

  void alert() => dialogService.alert('This is an alert dialog');
}
2
likes
0
pub points
0%
popularity

Publisher

unverified uploader

MVVM architecture components for Flutter. Helps developers build loosely-coupled applications.

Homepage
Repository
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, get_it, meta, page_transition, provider

More

Packages that depend on arch