bloc 0.2.1 copy "bloc: ^0.2.1" to clipboard
bloc: ^0.2.1 copied to clipboard

outdated

A package to help implement the bloc pattern.

example/lib/main.dart

import 'dart:async';

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

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

class MyApp extends StatelessWidget {
  final ThemeBloc _themeBloc = ThemeBloc();

  @override
  Widget build(BuildContext context) {
    return BlocBuilder(
      bloc: _themeBloc,
      builder: ((
        BuildContext context,
        ThemeData theme,
      ) {
        final IconData icon = (theme == ThemeData.light())
            ? Icons.brightness_4
            : Icons.brightness_7;

        return MaterialApp(
          title: 'Flutter Demo',
          theme: theme,
          home: Scaffold(
            appBar: AppBar(title: Text('ThemeBloc Demo')),
            body: ListView(
              children: [
                ListTile(
                  leading: Icon(icon),
                  title: Text('Dark Theme'),
                  trailing: Switch(
                    value: theme == ThemeData.dark(),
                    onChanged: (value) => _themeBloc.toggleTheme(),
                  ),
                ),
              ],
            ),
          ),
        );
      }),
    );
  }
}

abstract class ThemeEvent {}

class SetDarkTheme extends ThemeEvent {}

class SetLightTheme extends ThemeEvent {}

class ThemeBloc extends Bloc<ThemeEvent, ThemeData> {
  final ThemeData _darkTheme = ThemeData.dark();
  final ThemeData _lightTheme = ThemeData.light();
  ThemeData _currentTheme = ThemeData.light();

  void toggleTheme() {
    if (_currentTheme == ThemeData.light()) {
      _currentTheme = ThemeData.dark();
      dispatch(SetDarkTheme());
    } else {
      _currentTheme = ThemeData.light();
      dispatch(SetLightTheme());
    }
  }

  ThemeData get initialState => _currentTheme;

  @override
  Stream<ThemeData> mapEventToState(event) async* {
    if (event is SetDarkTheme) {
      yield _darkTheme;
    } else {
      yield _lightTheme;
    }
  }
}
2898
likes
0
pub points
99%
popularity

Publisher

verified publisherbloclibrary.dev

A package to help implement the bloc pattern.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, rxdart

More

Packages that depend on bloc