shark flutter (official)
a flutter server rendering framework
what it is
shark is a server rendering framework, a server-driven ui framework.
simple use case of shark
let say you have a text
widget, you specify it as json on the server return to the client,
the client use shark to receive the text widget and show the ui successfully.
after a while, you want to change the text widget to a button
, instead of modify it on the client code and go through all the mobile app store update process,
you only need to modify the text widget to a button widget on the server, and the shark client received it, showed the newest ui.
project diagram
how to use
first, init
shark library on main method on your application
void main() {
widgetsflutterbinding.ensureinitialized();
await shark.init(hosturl: 'http://yourhost.com');
runapp(myapp());
}
second, set up ui widget
- to be noticed, every thing related to widget is controlled by
sharkcontroller
get
method is where we send the request
late final sharkcontroller _controller;
/// init the shark controller
@override
void initstate() {
_controller = sharkcontroller.fromurl(path: '/container',)..get();
super.initstate();
}
@override
widget build(buildcontext context) {
return sharkwidget(controller: _controller);
}
to redirect to another page, call redirect
_controller.redirect('/your_new_path');
to refresh current page, call refresh
_controller.refresh();
if you want to create your own parser
class mycustomparser extends sharkparser {}
routing
shark
auto handles your page routing, if you do not want it, set handleclickevent
to false
_sharkcontroller = sharkcontroller('/your_path', handleclickevent: false);
click event
routing trigger by click event, which you can set it like this on your json widget file.
a sample event json format
{
"type": "container",
"click_event": "route://your_path?xxx=xxx"
}
schema
: xxx://
path
: your_path
argument
: after the prefix ‘?’ is the argument field.
xxx=xxx, separate with &
sample:
"click_event": "route://second_page?name=hello&place=world"
the schema represents the routing action
currently, there are 4 routing action
route
: prefix with route://
, internally would call navigator.pushname('new_path', args)
push a new route to navigator
** please remember to specify route path
on your route map
if not, navigator would throw an error
then sharkcontroller would try to navigator to a new default shark widget with the following path
pop
: prefix with pop://
, internally would call navigator.pop(result)
redirect
: prefix with redirect://
, redirect current page with the following path
link
: use url_launcher
internally to open a url on browser, please visit url_launcher to configure the detail requirements for each platform
caching
shark
use dio_cache_interceptor for caching purposes.
when you initialize the shark library, you can pass a cachestrategy
argument to config your caching setting.
parsing
note that shark uses dynamic_widget for widget parsing,
if you want to create your own parser
class mycustomparser extends sharkparser {}
then add this new parser to shark widget
_sharkcontroller.addparser(mycustomparser());
to view the json format, go visit [documentation](https://github.com/dengyin2000/dynamic_widget/blob/master/widgets.md) on dynamic_widget.
localization
shark
uses easy_localization for localization, follow the instructions to set up.
after setting up easy_localization package, next thing you need to do is to add a translatedtextparser
to shark widget
translatedtextparser
do all the localize work for you
_sharkcontroller.addparse(translatedtextparser());
in your server json file, modify the text
tag to translatedtext
{
"type": "text",
"data": "redirect to next page"
}
to
{
"type": "translatedtext",
"data": "redirect to next page"
}
data
value should be the value corresponds to your translations json file key
sample translations file zh.json
{
"navigate to next page":"push路由,转入下一页",
"redirect to next page": "转换至另一页",
"to goog.gl": "去谷歌"
}
that’s it ~~
Comments are closed.