yaz
yaz package for state, content and user’s options managements
this package is supported for web, mobile and desktop applications.
yaz package is consisted of two parts;
1) state management
- yaz package is a state management package in the first place. it uses only
changenotifier
for the whole process, due to this it is really simple and fast.
it uses only one widget. widget of “yazlistenerwidget” - yaz helps you to convert any objects to
changenotifier
with a simple code. - yaz supports for collection changes both in separate or entirely.
2) content management
- yaz provides you to get and change user options by a simple code from any part of the application.
- yaz helps you to store and cache your app’s frequently used contents.
example of flutter counter app
/// you don't need a stateful widget
class myhomepage extends statelesswidget {
myhomepage({key? key, required this.title}) : super(key: key);
final string title;
/// 1 ) define your variable and convert notifier
final counter = 0.notifier;
@override
widget build(buildcontext context) {
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:',
),
/// 2 ) convert widget your changenotifier
counter.builder((context) => text(
'${counter.value}',
style: theme.of(context).texttheme.headline4,
)),
],
),
),
floatingactionbutton: floatingactionbutton(
onpressed: () {
/// 3) increment count
counter.value++;
},
tooltip: 'increment',
child: const icon(icons.add),
),
);
}
}
usage
how to convert an object to a change notifier?
any object might be converted into a changenotifier
: yaznotifier
.notifier
works on any object and returns a yaznotifier
var counter = 0.notifier;
var editing = "mehmet yaz".notifier;
var foo = foo().notifier;
.value
gets and set an value in yaznotifier
instance;
this setter triggers all listeners
print(counter.value);
// output - 0 -
counter.value++;
print(counter.value);
// output - 1 -
! attention:
if you wish to listen changes in instance members of foo
:
// don't
var foo = foo().notifier;
// do
var bar = foo().bar.notifier;
because foo
is triggered only like this;
foo.value = foo(); // new instance
foo.value = anotherfoo;
builtnotifier
you can see the widgets wrapped by builtnotifier
when they are rebuilt. they blink when they are rebuilt.
builtnotifier(
child: yourwidget()
);
yazlistenerwidget
yazlistenerwidget
works with any changenotifier
instance. there is no other widget for state management.
yazlistenerwidget(
changenotifier: changenotifier,
builder: (buildcontext c) {
return mywidget();
}
);
also you can create yazlistenerwidget
instance by .builder
method works on any changenotifier
s.
changenotifier.builder((context) => mywidget());
! performance tip:
narrow down this widget as narrow as possible. using more yazlistenerwidget
s is better than using a single widget to wrap all things.
single variable
/// define notifier
var counter = 0.notifier;
/// listen with widget
yazlistenerwidget(
changenotifier: counter,
builder: (buildcontext c) {
return text(counter.value.tostring());
}
//or
counter.builder((context) => text(counter.value.tostring()))
);
multiple variable
/// define notifiers
final counter1 = 0.notifier;
final counter2 = 5.notifier;
final counter3 = 10.notifier;
/// listen with widget
yazlistenerwidget(
changenotifier: counter1.combinewith([counter2, counter3]),
/// or changenotifier: multiplechangenotifier([counter1 , counter2 ,counter3]),
/// or changenotifier: [counter1 , counter2, counter3].notifyall
builder: (buildcontext c) {
return text(counter.value.tostring());
}
);
how to listen collection changes?
there are 3 ways to listen collections
1) classical total changes – bad way
in this way, standard .notifier
is used
but this triggers only like this;
var list = [0,1,2,3].notifier
list.value = [10 , 20 , 30]; // triggered
list.value[0] = 50; // not triggered
// usage
yazlistenerwidget(
changenotifier: list,
//....
);
2) triggering in any changes – mid-way
in this way, a yazlist is created.
a yazlist
can be created by .listenall
from all list
instances.
yazmap
can be created from all map
s by this way, as well.
yazlist
is a changenotifier
and all listeners are triggered in any changes.
yazlist<int> list = [0,1,2,3].listenall;
list[0] = 5; // triggered
list.add(10) = 10; // triggered
list.remove(0) = 30; // triggered
/// triggered by all methods similarly
// usage
yazlistenerwidget(
changenotifier: list,
//....
);
3) trigger every element separately – good way
you can get a list<yaznotifier>
on any list
by .listeners
method.
in this way, value
changes of elements trigger all listeners
when making length-changings like adding or removing elements or making yaznotifier
instance changes; listeners are not triggered.
for instance, let us assume that there are two separate widgets to listen index of 0 and index of 1.
when index of 0 is changed, only the widget of 0 is rebuilt.
other one is not rebuilt.
to trigger all changes, .notifyall
method on list<yaznotifier>
may be used.
var listeners = [0,1].listeners;
// 0. widget
yazlistenerwidget(
changenotifier: listeners[0],
//....
);
// 1. widget
yazlistenerwidget(
changenotifier: listeners[1],
//....
);
// 2. widget
yazlistenerwidget(
changenotifier: listeners.notifyall,
//....
);
listeners[0].value++; // rebuilt 0. and 2. widgets
listeners[1].value++; // rebuilt 1. and 2. widgets
all can be applied on map
.
you can get a map<k, yaznotifier<v>>
by .listeners
on map
.
how to listen stream?
all streams convertible by .yazstream
or .yazstreamwithdefault(value)
on stream
.yazstream
returns nullable yazstream<t?>
.yazstreamwithdefault(value)
returns non-nullable yazstream<t>
yazstream<user?> stream = db.getchanges().yazstream;
stream.builder((context) => yourwidget());
/// don't forget
stream.cancel();
user options
simple example
you can manage every user option by this service.
///
class myhomepage extends statelesswidget {
///
myhomepage({key? key}) : super(key: key);
@override
widget build(buildcontext context) {
return scaffold(
body: center(
/// wrap widgets with useroption
/// use default value or initialize on your splash screen
child: optionwrapper<double>(
useroption: useroption("font_size_multiplier", defaultvalue: 0.5),
builder: (c, option) {
return text(
"user option example",
style: textstyle(fontsize: 10.0 * option.value),
/// rebuild with new option
/// or use directly :
/// fontsize: 10.0 * useroption("font_size_multiplier").value,
);
},
),
),
floatingactionbutton: floatingactionbutton(
onpressed: () {
/// change options
useroption<double>("font_size_multiplier").value += 0.1;
},
tooltip: 'increment font size',
child: const icon(icons.add),
),
);
}
}
usage
you can get option in everywhere:
useroption<double>("font_size").value;
and you change
useroption<double>("font_size").value = 10.0;
you can wrap your widgets with options, when options are changed the widgets are rebuilt;
optionswrapper(
option: useroption<double>("font_size"),
builder : (c , option) {
return text("hello world!" , style:textstyle(
fontsize: option.value
));
}
)
you can init by your default values;
useroptions().init(list<useroption> list);
you can listen changes for to fetch on db
useroptions().onuseroptionschange((options){
//fetch
});
if you don’t use init functions, you may set default values in the first use.
you don’t have to set default values for your further uses in session flow.
useroption<double>("font_size" , defaultvalue: 10);
content controller
you can store or cache your common uses contents.
for example: friend users, e-commerce categories eg.
just! you will get content with their identifier.
you don’t need a second query from db to recall content repeatedly.
however, if the content is too old, it will be restored from db.
var controller = usercontroller();
await controller.getcontent("user_1");
/// if content was brought before will get from local
/// else will get from db, save to local and return to you
usage
implement cacheable
your cache able class must have a unique identifier.
///
class user with cacheable<user> {
///
user(this.userid, this.username);
@override
string get identifier => userid;
///
string userid;
///
string username;
}
implement your controller
there are two kind content controller:
- cachecontentcontroller : store contents only session
- storagecontentcontroller : store contents in device key-value storage
///
class usercontroller extends storagecontentcontroller<user> {
/// singleton recommended
/// getter for your content by identifier
/// common use this getter get content from db
@override
future<user?> contentgetter(string identifier) async {
await future.delayed(const duration(milliseconds: 300));
return user(identifier, "user of $identifier");
}
/// don't need for cachecontentcontroller
/// need for store device storage
@override
user fromjson(map<string, dynamic> map) {
return user.fromjson(map);
}
/// maximum storage count
@override
int get maxcount => 30;
/// maximum storage duration
/// if your content older than, recall getter from db
@override
duration get maxduration => const duration(minutes: 30);
/// save key for local storage
///
/// don't need for cachecontentcontroller
/// need for store device storage
@override
string get savekey => "users";
/// don't need for cachecontentcontroller
/// need for store device storage
@override
map<string, dynamic> tojson(user instance) {
return instance.tojson();
}
}
also you can inititialize with your unordered contents
controller.init(contents);
save , update or remove content
but many cases you dont need this. because if you determine max’s carefully,
getcontent
do these for you.
controller.save(content);
controller.remove(id);
controller.update(id);
Comments are closed.