frefresh
help you to build pull-down refresh and pull-up loading in the simplest way.
although unprecedented simplicity, but the effect is amazing. it also supports configuration refresh and loading elements. the complete controller allows you to help you control the entire dynamic process.
guide
⚙️ parameter & interface
�� frefresh param
param | type | necessary | default | desc |
---|---|---|---|---|
child | widget | true | null | main view content |
header | widget | false | null | elements that will be displayed when you pull down and refresh |
headerbuilder | headerbuilder | false | null | construct a pull-down refresh element. [header] configuration will be overwritten. |
headerheight | double | false | 50.0 | [header] the height of the area |
headertrigger | double | false | 0.0 | the distance to trigger pull-down refresh should be greater than [headerheight] |
onrefresh | voidcallback | false | null | callback when refresh is triggered |
footer | widget | false | null | elements that will be displayed when pulling up |
footerbuilder | footerbuilder | false | null | build pull-up loading elements. will override [footer] configuration. |
footerheight | double | false | 0.0 | [footer] the height of the area |
footertrigger | double | false | 0.0 | the distance to trigger the pull-up loading should be greater than [headerheight] |
shouldload | bool | false | true | whether the pull-up load should be triggered. in some scenarios, when the loading is completed, the pull-up loading element will need to be turned into a footer |
onload | voidcallback | false | null | callback when loading is triggered |
controller | frefreshcontroller | false | null | [refresh] controller. see [refresh controller] for details |
⌨️ frefreshcontroller
�� param
param | type | desc |
---|---|---|
refreshstate | refreshstate | get the pull-down refresh status. see [refreshstate] for details |
loadstate | loadstate | get the pull-up loading status. see [loadstate] for details |
position | double | current scroll position |
scrollmetrics | scrollmetrics | current scroll information. see [scrollmetrics] for details. |
backoriginonloadfinish | bool | when loading is completed, whether to return to the original position. this parameter is useful when the gridview only adds one element. |
�� interface
void refresh({duration duration = const duration(milliseconds: 300)})
actively trigger pull-down refresh.
[duration] the duration of the pull-down effect. default 300ms
finishrefresh()
end pull-down refresh.
finishload()
end pull-up loading.
void setonstatechangedcallback(onstatechangedcallback callback)
set up status listener. e.g .:
controller.setonstatechangedcallback((state){
if (state is refreshstate) {
}
if (state is loadstate) {
}
})
void setonscrolllistener(onscrolllistener onscrolllistener)
set up scroll listener. receive [scrollmetrics].
void scrollto(double position, {duration duration = const duration(milliseconds: 300)})
scroll to the specified position.
void scrollby(double offset, {duration duration = const duration(milliseconds: 300)})
scroll the specified distance.
void jumpto(double position)
jump to the specified position.
�� refreshstate
value | desc |
---|---|
preparing_refresh | reach [headertrigger], ready to enter refresh state |
refreshing | refreshing |
finishing | refresh ending |
idle | idle state |
�� loadstate
value | desc |
---|---|
preparing_load | reach [footertrigger], ready to enter the loading state |
loading | loading |
finishing | load ending |
idle | idle state |
�� demo
�� refresh example
this is our most common pull-down refresh example in daily development ��. believe me, if you want to build such an effect, it will be very difficult!
but if you use frefresh, the situation is completely different.
next, we only need a few lines of code to complete the construction of this effect.
1. create frefreshcontroller
/// create a controller
frefreshcontroller controller = frefreshcontroller()
2. create frefresh
frefresh(
/// set up the controller
controller: controller,
/// create header
header: buildrefreshview(),
/// need to pass the size of the header area
headerheight: 75.0,
/// content area widget
child: listview.builder(
physics: neverscrollablescrollphysics(),
shrinkwrap: true,
...
),
/// this function will be called back after entering refreshing
onrefresh: () {
/// end refresh via controller
controller.finishrefresh();
},
);
done ��!
this is all you need to do to create a pull-down refresh.
frefresh takes care of everything, developers only need to focus on the construction of the header area and content area.
⚠️ attention,to use listview, gridview in frefresh, you need to configure their
physics: neverscrollablescrollphysics ()
,shrinkwrap: true
, otherwise it will affect the scrolling and layout effects.
�� headerbuilder demo
frefresh(
controller: controller,
/// build the header area with headerbuilder
headerbuilder: (setter, constraints) {
return fsuper(
/// get the available space in the current header area
width: constraints.maxwidth,
height: constraints.maxheight,
...
onclick:{
setter((){
/// refresh the header area
})
},
);
},
headerheight: 100.0,
/// build content area
child: gridview.builder(),
/// this function will be called back after entering the refreshing state
onrefresh: () {
/// finish refresh
controller.finishrefresh();
}
)
frefresh provides a very flexible header area construction method, which is to complete the construction through the headerbuilder function.
in the headerbuilder function, the developer can get the refresh function statesetter for the partial refresh header area and the real-time size of the header area through the parameters.
this way, the header area is given more open creativity.
�� load example
corresponding to the pull-down refresh, the construction of the pull-up loading effect is also very simple.
1. create frefreshcontroller
/// create a controller
frefreshcontroller controller = frefreshcontroller()
2. create frefresh
frefresh(
/// setup the controller
controller: controller,
/// create footer area
footer: linearprogressindicator(),
/// need to setup footer area height
footerheight: 20.0,
/// create content area
child: buildercontent(),
/// this function will be called back after entering the loading state
onload: () {
/// end loading state
controller.finishload();
},
)
building pull-ups is equally simple enough. developers only need to pay attention to the construction of footer area and content area, and the state changes and visual effects control during the pull-up loading process can be safely handed over to frefresh.
�� footerbuilder demo
frefresh(
controller: controller,
/// build footer area widget by footerbuilder
footerbuilder: (setter) {
/// get refresh status, partially update the content of footer area
controller.setonstatechangedcallback((state) {
setter(() {
...
});
});
return buildfooter();
},
footerheight: 38.0,
child: buildcontent(),
onload: () {
controller.finishload();
},
)
frefresh also provides a builder function footerbuilder for building the footer area. through this function, you can get the refresh function statesetter which refreshes only the footer area.
in this way, the developer can easily change the view of the footer area according to the status or some other conditions. very intimate ��.
⚙️ frefreshcontroller
frefresh provides developers with intimate controllers frefreshcontroller, which supports many convenient capabilities.
1. add controller to frefresh
/// create controller
frefreshcontroller controller = frefreshcontroller()
/// configure controller for frefresh
frefresh(
controller: controller,
)
when the developer creates a controller and then sets it into a frefresh, the controller can start to monitor the status of this frefresh and control it.
2. stop refreshing or loading
when the refresh state or loading state is triggered, data processing tasks such as network requests are usually performed. after these tasks are completed, we need to stop the refresh state or loading state. how to do it?
controller.finishrefresh()
can stop refreshingcontroller.finishload()
can stop loading
3. state change listen
controller5.setonstatechangedcallback((state) {
/// refresh status
if (state is refreshstate) {
}
/// loading state
if (state is loadstate) {
}
});
through the above simple code, you can monitor the status change of frefresh, whether it is pull-down refresh or pull-up loading.
4. scroll listen
controller.setonscrolllistener((metrics) {
/// get scroll information
});
frefreshcontroller it is really convenient to add sliding monitor. the parameters received is [scrollmetrics],it can get very comprehensive information such as current scroll distance, maximum scroll distance, whether it exceeds the scroll range, etc..
5. actively trigger refresh
through frefreshcontroller, developers can also actively trigger a refresh, and can specify the length of time to slide to the refresh position.
controller.refresh(duration: duration(milliseconds: 2000));
this feature is very useful in many scenarios.
6. scroll control
frefreshcontroller provides a variety of intimate and delicate sliding controls for developers to choose.
/// scroll to the specified position
controller.scrollto(100.0, duration:duration(milliseconds: 2000));
/// scroll the specified distance
controller.scrollby(20.0, duration:duration(milliseconds: 800));
/// jump to the specified position
controller.jumpto(100.0);
this makes many beautiful interactions easier to build.
�� how to use?
add dependencies in the project pubspec.yaml
file:
pub dependency
dependencies:
frefresh: ^<version number>
git dependency
dependencies:
frefresh:
git:
url: '[email protected]:fliggy-mobile/frefresh.git'
ref: '<branch number or tag number>'
how to run demo project?
- clone project to local
- enter the project
example
directory and run the following command
flutter create .
- run the demo in
example
Comments are closed.