0 purchases
state helpers
Contains utility function and classes to help utilize state management more efficiently.
Features #
Creating and handling StatefulWidgets more elegantly.
Helpers for Bloc management system.
Helper scope extensions function
Getting started #
Install #
flutter pub add state_helpers
copied to clipboard
Usage #
An easier way to create stateful widgets in Flutter. #
instead of writing with 2 classes:
import 'package:flutter/material.dart';
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<StatefulWidget> createState() => _SimpleCounterState();
}
class _SimpleCounterState extends State<Counter> {
var count = 0;
@override
Widget build(BuildContext context) {
return Row(
children: [
TextButton(onPressed: () {
setState(() {
count--;
});
}, child: const Text('-')),
Text('value: $count'),
TextButton(
onPressed: () {
setState(() {
count++;
});
},
child: const Text('+')
)
],
);
}
}
copied to clipboard
use one class with a state mixin:
import 'package:flutter/material.dart';
import 'package:state_helpers/state_helpers.dart';
class SimpleCounter extends StatefulWidget with StateHolder<int> {
const SimpleCounter({super.key});
@override
int get initialState => 0;
@override
Widget build(BuildContext context, int state, StateEmitter<int> emitter) {
return Row(
children: [
TextButton(onPressed: () {
emitter(state - 1);
}, child: const Text('-')),
Text('value: $state'),
TextButton(
onPressed: () {
emitter(state + 1);
},
child: const Text('+')
)
],
);
}
}
copied to clipboard
Creating BlocBuilder and BlocSelector easier #
avoid remembering multiple types when creating BlocSelector & BlocBuilder
import 'package:example/widgets/bloc/color_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class ColorPicker extends StatelessWidget {
const ColorPicker({super.key});
@override
Widget build(BuildContext context) {
return BlocSelector<ColorBloc, ColorData, String>(
selector: (st) => st.name,
builder: (context, snapshot) => Text(snapshot)
);
}
}
copied to clipboard
create a builders or selectors directly from the bloc instance:
import 'package:example/widgets/bloc/color_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class ColorPicker extends StatelessWidget {
const ColorPicker({super.key});
@override
Widget build(BuildContext context) {
return context.read<ColorBloc>().selector(
selector: (st) => st.color,
builder: (context, snapshot) =>
Container(height: 64, color: snapshot,)
);
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.