coaster 0.0.2 copy "coaster: ^0.0.2" to clipboard
coaster: ^0.0.2 copied to clipboard

A MVU implementation for Flutter inspired by The Elm Architecture

TEA Coaster #

logo
TEA Coaster is a Flutter package that implements The Elm Architecture (TEA) pattern, bringing the elegance of Elm's architecture to Flutter development.

Features #

  • Model-View-Update (MVU) Architecture: A clean and predictable state management pattern
  • Command Pattern: Handle side effects in a structured way
  • Subscription System: Manage event streams and subscriptions
  • Type Safety: Full type safety with Dart's strong typing system
  • Testability: Easy to test with clear separation of concerns

Getting Started #

Add the package to your pubspec.yaml:

dependencies:
  coaster: ^0.0.2

Usage #

Basic Counter Example #

Here's a simple counter application using TEA Coaster:

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

// Define your model
class CounterModel {
  final int count;
  
  CounterModel({required this.count});
}

// Define your messages
enum CounterMessage { increment, decrement }

// Create your update function
CounterModel update(CounterModel model, CounterMessage message) {
  switch (message) {
    case CounterMessage.increment:
      return CounterModel(count: model.count + 1);
    case CounterMessage.decrement:
      return CounterModel(count: model.count - 1);
  }
}

// Create your view
Widget view(CounterModel model, Dispatch<CounterMessage> dispatch) {
  return Scaffold(
    appBar: AppBar(title: Text('Counter Example')),
    body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('Count: ${model.count}', style: TextStyle(fontSize: 24)),
          SizedBox(height: 20),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () => dispatch(CounterMessage.increment),
                child: Text('Increment'),
              ),
              SizedBox(width: 10),
              ElevatedButton(
                onPressed: () => dispatch(CounterMessage.decrement),
                child: Text('Decrement'),
              ),
            ],
          ),
        ],
      ),
    ),
  );
}

// Use the Coaster widget
class CounterApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Coaster<CounterModel, CounterMessage>(
        init: CounterModel(count: 0),
        update: update,
        view: view,
      ),
    );
  }
}

Advanced Features #

For more advanced examples including:

  • Command pattern for side effects
  • Subscription system for event handling
  • Complex state management

Please check out our example directory for complete examples.

Contributing #

We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License #

This project is licensed under the MIT License - see the LICENSE file for details.

2
likes
140
points
191
downloads

Publisher

unverified uploader

Weekly Downloads

A MVU implementation for Flutter inspired by The Elm Architecture

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on coaster