preference_helper 0.0.5+2 copy "preference_helper: ^0.0.5+2" to clipboard
preference_helper: ^0.0.5+2 copied to clipboard

A package that makes shared_preferences easier to use, This package is built with bloc, shared_preferences

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:preference_helper/preference_helper.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async {
  var sharedPreferences = await SharedPreferences.getInstance();
  runApp(MyApp(
    preferenceBloc: PreferenceBloc(
      sharedPreferences: sharedPreferences,
      usagePreferences: [
        Preference<int>(
          key: 'counter',
          initValue: 0,
        ),
      ],
    ),
  ));
}

class MyApp extends StatelessWidget {
  final PreferenceBloc preferenceBloc;

  MyApp({@required this.preferenceBloc});

  @override
  Widget build(BuildContext context) {
    return BlocProvider<PreferenceBloc>(
      bloc: preferenceBloc,
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  PreferenceBloc _preferenceBloc;

  void _incrementCounter() {
    var counterPref = _preferenceBloc.getPreference<int>('counter');
    counterPref.value += 1;
    _preferenceBloc.dispatch(UpdatePreference(counterPref));
  }

  @override
  void initState() {
    _preferenceBloc = BlocProvider.of<PreferenceBloc>(context);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<PreferenceEvent, PreferenceState>(
      bloc: _preferenceBloc,
      builder: (BuildContext context, PreferenceState state) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  (state is PreferenceState
                          ? state.preferences.get<int>('counter').value
                          : 0)
                      .toString(),
                  style: Theme.of(context).textTheme.display1,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      },
    );
  }
}
0
likes
40
pub points
8%
popularity

Publisher

unverified uploader

A package that makes shared_preferences easier to use, This package is built with bloc, shared_preferences

Repository (GitHub)
View/report issues

License

Unlicense (LICENSE)

Dependencies

bloc, equatable, flutter, flutter_bloc, meta, shared_preferences

More

Packages that depend on preference_helper