state_mgr

Creator: coderz1093

Last updated:

0 purchases

TODO
Add to Cart

Description:

state mgr

state_mgr #
State manager for flutter widgets
ChangeLog #
ChangeLog.md
About #
This plugin allows management of states your objects not using stateful widgets
State manager allows you to separate the business logic of managing the state of the model and its presentation
Usage #
Below is a basic sample application using the state_manager library
import 'package:flutter/material.dart';
import 'package:state_mgr/state_mgr.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Simple stater demo',
home: MyStaterPage(title: 'Flutter Demo Stater Page'),
);
}
}

class MyStaterPage extends StatelessWidget {
const MyStaterPage({super.key, required this.title});

final String title;

static int _counter = 0;

@override
Widget build(BuildContext context) {
return Stater<StateManager>(
builder: (stater) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: (){
_counter++;
stater.invalidate();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
);
}
}
copied to clipboard
Let's take out the logic of processing the states of the counter in a separate class CounterState.
'manager' is used to create a counter state management context
class MyStaterPage extends StatelessWidget {
const MyStaterPage({super.key, required this.title});

final String title;

@override
Widget build(BuildContext context) {
return Stater(
manager: () => CounterState(),
builder: (stater) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'${stater.counter}',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => stater.counter++,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
);
}
}

class CounterState extends StateManager {
int _counter = 0;
int get counter => _counter;
set counter(int value) {
if(_counter == value) return;
_counter = value;
invalidate();
}
}
copied to clipboard
Now all counter state management logic can be performed in a separate CounterState class.
For example, let's add emulation of a long wait for a response from the server
class CounterState extends StateManager {
int _counter = 0;
int get counter => _counter;
set counter(int value) {
if(_counter == value) return;

Future.delayed(const Duration(seconds: 1), () {
_counter = value;
invalidate();
});
}
}
copied to clipboard
Now we can expand and add more options to control the counter state logic

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.