pike 1.0.3 copy "pike: ^1.0.3" to clipboard
pike: ^1.0.3 copied to clipboard

Pike is an event-driven state management library, enabling efficient state updates and handling through events.

example/lib/main.dart

// ignore_for_file: avoid_print

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

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

/// {@template app}
/// A [StatelessWidget] that:
/// * uses [bloc](https://pub.dev/packages/bloc) and
/// [flutter_bloc](https://pub.dev/packages/flutter_bloc)
/// to manage the state of a counter and the app theme.
/// {@endtemplate}
class App extends StatelessWidget {
  /// {@macro app}
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiPikeProvider(
      providers: [
        PikeProvider<ThemeCarp>(
          pike: ThemeCarp(),
        ),
        PikeProvider<CounterPike>(
          pike: CounterPike(),
        ),
      ],
      child: const AppView(),
    );
  }
}

/// {@template app_view}
/// A [StatelessWidget] that:
/// * reacts to state changes in the [ThemeCarp]
/// and updates the theme of the [MaterialApp].
/// * renders the [CounterPage].
/// {@endtemplate}
class AppView extends StatelessWidget {
  /// {@macro app_view}
  const AppView({super.key});

  @override
  Widget build(BuildContext context) {
    return PikeBuilder<ThemeCarp, ThemeData>(
      builder: (_, theme) {
        return MaterialApp(
          theme: theme,
          home: const CounterPage(),
        );
      },
    );
  }
}

/// {@template counter_page}
/// A [StatelessWidget] that:
/// * provides a [CounterPike] to the [CounterView].
/// {@endtemplate}
class CounterPage extends StatelessWidget {
  /// {@macro counter_page}
  const CounterPage({super.key});

  @override
  Widget build(BuildContext context) {
    return const CounterView();
  }
}

/// {@template counter_view}
/// A [StatelessWidget] that:
/// * demonstrates how to consume and interact with a [CounterPike].
/// {@endtemplate}
class CounterView extends StatelessWidget {
  /// {@macro counter_view}
  const CounterView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Counter')),
      body: Center(
        child: PikeBuilder<CounterPike, int>(
          builder: (context, count) {
            return Text(
              '$count',
              style: Theme.of(context).textTheme.displayLarge,
            );
          },
        ),
      ),
      floatingActionButton: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          FloatingActionButton(
            child: const Icon(Icons.add),
            onPressed: () {
              PikeProvider.of<CounterPike>(context)
                  .add(CounterIncrementPressed());
            },
          ),
          const SizedBox(height: 4),
          FloatingActionButton(
            child: const Icon(Icons.remove),
            onPressed: () {
              PikeProvider.of<CounterPike>(context)
                  .add(CounterDecrementPressed());
            },
          ),
          const SizedBox(height: 4),
          FloatingActionButton(
            child: const Icon(Icons.brightness_6),
            onPressed: () {
              PikeProvider.of<ThemeCarp>(context).toggleTheme();
            },
          ),
        ],
      ),
    );
  }
}

/// Event being processed by [CounterPike].
abstract class CounterEvent {}

/// Notifies bloc to increment state.
class CounterIncrementPressed extends CounterEvent {}

/// Notifies bloc to decrement state.
class CounterDecrementPressed extends CounterEvent {}

/// {@template counter_bloc}
/// A simple [Pike] that manages an `int` as its state.
/// {@endtemplate}
class CounterPike extends Pike<CounterEvent, int> {
  /// {@macro counter_bloc}
  CounterPike() : super(0) {
    on<CounterIncrementPressed>((event, emit) => emit(state + 1));
    on<CounterDecrementPressed>((event, emit) => emit(state - 1));
  }
}

/// {@template brightness_cubit}
/// A simple [Carp] that manages the [ThemeData] as its state.
/// {@endtemplate}
class ThemeCarp extends Carp<ThemeData> {
  /// {@macro brightness_cubit}
  ThemeCarp() : super(_lightTheme);

  static final _lightTheme = ThemeData.light();

  static final _darkTheme = ThemeData.dark();

  /// Toggles the current brightness between light and dark.
  void toggleTheme() {
    emit(state.brightness == Brightness.dark ? _lightTheme : _darkTheme);
  }
}
1
likes
160
points
415
downloads
screenshot

Publisher

verified publishercontributors.info

Weekly Downloads

Pike is an event-driven state management library, enabling efficient state updates and handling through events.

Homepage
Repository (GitHub)
View/report issues

Topics

#pike #state-management #state #event-driven

Documentation

API reference

Funding

Consider supporting this project:

www.patreon.com

License

MIT (license)

Dependencies

flutter

More

Packages that depend on pike