Provides handles for circular iteration over states list. Supports forward and backward iterations and arbitrary position set.
dependencies:
flutter_use: ^0.0.2
class Sample extends HookWidget {
@override
Widget build(BuildContext context) {
final list = ['first', 'second', 'third', 'fourth', 'fifth'];
final stateList = useStateList(list);
final stateAtController = useTextEditingController();
final stateController = useTextEditingController();
return Column(
children: [
Text(stateList.list.toString()),
Text(
"${stateList.list.isNotEmpty ? stateList.state : null} [index: ${stateList.currentIndex}]"),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => stateList.prev(),
child: const Text('prev')),
ElevatedButton(
onPressed: () => stateList.next(),
child: const Text('next')),
],
),
TextFormField(
controller: stateAtController,
decoration: const InputDecoration(
labelText: 'set state by index',
),
),
ElevatedButton(
onPressed: () {
stateList.setStateAt(int.parse(stateAtController.text));
},
child: const Text('set state by index')),
TextFormField(
controller: stateController,
decoration: const InputDecoration(
labelText: 'set state by value',
),
),
ElevatedButton(
onPressed: () {
stateList.setState(stateController.text);
},
child: const Text('set state by value'),
),
],
);
}
}
UseStateList<T> useStateList<T>([List<T> stateSet = const []])
If stateSet
changed, became shorter than before and currentIndex
left in shrunk gap - the last element of list will be taken as current.
state
: T
— current state value;currentIndex
: int
— current state index;prev()
: void
— switches state to the previous one. If first element selected it will switch to the last one;next()
: void
— switches state to the next one. If last element selected it will switch to the first one;setStateAt(int newIndex)
: void
— set the arbitrary state by index. Indexes are looped.
4ex: if list contains 5 elements, attempt to set index 9 will bring use to 5th element, in case of negative index it will set to the 0th.setState(T state)
: void
— set the arbitrary state value that exists instateSet
. In case new state does not exists instateSet
an Error will be thrown.