0 purchases
state tools
State Tools #
A light and simple State Manager for Flutter Apps.
How Simple? #
1. Create your StateNotifier class:
class CounterState extends StateNotifier<int> {
CounterState() : super(0);
void increment() => state += 1;
}
copied to clipboard
if you want a specialized type for Lists:
class CounterState extends ListStateNotifier<int> {
CounterState() : super(List.empty());
void push(int value) => add(value * 2);
@override
bool filter(int value) => value % 2 == 0;
}
copied to clipboard
You can apply a filter for items on list (not mandatory).
This filter will be applied every time the list value change.
Also, the ListStateNotifier offers prebuilt helper modifiers, like:
add(item)
removeFist(item)
addAll([ list of items ])
removeAll([ list of items ])
2. On your Widget use a StateBuilder to interact with your StateNotifier
StateBuilder<int>(
notifier: counterStateInstance,
builder: (context, state) => Text('$state')
)
...
FloatingActionButton(
onPressed: () => counterStateInstance.increment(),
tooltip: 'Increment',
child: const Icon(Icons.add),
)
copied to clipboard
Or... you can just listen the changes and do your Non-UI related logic:
StateListener<int>(
notifier: counterStateInstance,
listener: (context, state) => log('$state'),
child: const Text('My StateListener')
)
copied to clipboard
Want More? #
If you wish, you can use the PersistableStateNotifier
class CounterState extends PersistableStateNotifier<int> {
CounterState() : super(0);
void increment() => state += 1;
@override
int fromJson(Map<String, dynamic> json) => json['value'] as int;
@override
Map<String, int> toJson(int state) => {'value': state};
}
copied to clipboard
Interested? #
Add state_notifier package on your App:
state_tools: 1.0.1
copied to clipboard
License #
Copyright 2024 TMApps
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.