Skip to content
Live Demo

Source on GitHublib/src/ui_kit/fast_refresh/

Overview ​

FastRefresh uses custom ScrollPhysics for friction / rebound / overscroll, and Header/Footer notifiers that drive

inactive → drag → armed → ready → processing → processed → done.

After release the list springs to the trigger offset, then the task runs. The default skin is Classic (rotating arrow + text + spinner).

TopicNotes
EntryFastRefresh(onRefresh, onLoad, child) or FastRefresh.builder
ControllerFastRefreshController: callRefresh / callLoad / finish* / resetFooter
Resultssuccess / fail / noMore
PagingFastPagingList (fetchPage) or a FastPaging subclass
Default indicatorsFastClassicHeader / FastClassicFooter; optional FastMaterial*
CustomFastBuilderHeader / FastBuilderFooter, or listener / locator
Physicsbouncing (list moves) and clamping (list stays, indicator moves)

TIP

Do not nest another independently scrolling view inside child unless you give it its own physics, or use FastRefresh.builder and attach physics yourself. See the example app’s RefreshExample page.


Basic usage ​

dart
import 'package:fast_package/fast_package.dart';

final controller = FastRefreshController(
  controlFinishRefresh: true,
  controlFinishLoad: true,
);

FastRefresh(
  controller: controller,
  onRefresh: () async {
    await fetchFirstPage();
    controller.finishRefresh();
    controller.resetFooter();
  },
  onLoad: () async {
    final hasMore = await fetchNextPage();
    controller.finishLoad(
      hasMore ? FastRefreshResult.success : FastRefreshResult.noMore,
    );
  },
  child: ListView.builder(
    itemCount: items.length,
    itemBuilder: (_, i) => ListTile(title: Text(items[i])),
  ),
);

Pass null for onRefresh / onLoad to disable that side.

You can also skip controller finish and return FastRefreshResult.success (or fail / noMore). A thrown callback becomes fail and is not rethrown.


Widget constructor vs builder ​

The default constructor injects physics into the subtree; FastRefresh.builder hands physics to you.

Widget constructor ​

dart
FastRefresh(
  onRefresh: () async {},
  child: ListView(), // no physics argument needed
);
Notes
ProsZero boilerplate for a single list; child ScrollViews pick up refresh physics
ConsEvery scrollable in the subtree shares that physics; nested scroll can steal overscroll
Use whenOne ListView / GridView / CustomScrollView

builder constructor ​

dart
Scaffold(
  appBar: AppBar(title: const Text('Title')),
  body: FastRefresh.builder(
    onRefresh: () async {},
    childBuilder: (context, physics) {
      return CustomScrollView(
        physics: physics, // required, or pull-to-refresh never starts
        slivers: [
          SliverList(delegate: SliverChildListDelegate.fixed([])),
        ],
      );
    },
  ),
);

Keep the AppBar outside FastRefresh so refresh starts at the list top. Do not put SliverAppBar in the default builder snippet; use Nested (isNested: true) or Locator for a collapsing bar.

Notes
ProsYou choose which scroll layer refreshes; nested / multi-list layouts stay isolated
ConsForgetting physics: physics leaves platform physics; refresh will not run
Use whenNestedScrollView, PageView + lists, or another scrollable outside the list

Start with the widget constructor. Switch to builder when an inner list steals the gesture. Pair with isNested: true for NestedScrollView. The example hub has Widget, Builder, Nested, Locator, refreshOnStart, Paging, clamping, horizontal, and secondary.


State machine ​

inactive → drag → armed → ready → processing → processed → done → inactive

The Classic footer defaults to infiniteOffset = 70: load starts when the list is within 70px of the bottom. Set infiniteOffset: null for pull-and-release only.

A successful refresh with resetAfterRefresh (default true) clears footer noMore. Fail, thrown errors, and noMore do not. When controlFinishRefresh is true, reset happens on finishRefresh(success) only.

After FastRefresh is disposed or its controller is replaced, callRefresh / finishRefresh on the old controller are no-ops.

Refresh and load are mutually exclusive unless simultaneously is true.


More ​

Same order as the example hub:

  • FastRefresh.builder: see Widget constructor vs builder. Example: refresh_example/builder.
  • isNested: true: pin an outer app bar and refresh the inner list. Example: refresh_example/nested.
  • FastHeaderLocator / FastFooterLocator: place the indicator inside the list (position: locator). Example: refresh_example/locator.
  • refreshOnStart: trigger refresh after the first frame. Example: refresh_example/refresh_on_start.
  • FastPaging / FastPagingList: page / total / empty state wired to refresh and load. FastPagingList only needs fetchPage + itemBuilder. Example: refresh_example/paging. See Paging.
  • Load-more and scrolling: appending a page does not abort an in-range fling. See Load-more and scrolling.
  • clamping: true: the list does not overscroll; only the indicator moves. Example: refresh_example/clamping.
  • FastMaterialHeader / FastMaterialFooter: system-style crescent spinner. Example: refresh_example/material. See Material.
  • FastRefreshTheme: Classic copy and Material colors via ThemeExtension. See Theme.
  • Horizontal: ListView / PageView with scrollDirection: Axis.horizontal. Example: refresh_example/horizontal. See Horizontal.
  • Secondary floor: keep pulling past the refresh trigger to open a second page. Example: refresh_example/secondary. See Secondary floor.

Paging ​

For everyday lists use FastPagingList<T> — no subclass:

dart
FastPagingList<String>(
  refreshOnStart: true,
  fetchPage: (page) async {
    final res = await api.list(page);
    return FastPagingPage(
      items: res.items,
      page: res.page,
      total: res.total,
    );
  },
  itemBuilder: (context, index, item) => ListTile(title: Text(item)),
)

isNoMore: explicit hasMore → total / page+totalPage → last successful page has empty items. Keep subclassing FastPaging for complex data shapes.

FastPaging<DataType, ItemType> matches EasyRefresh's companion EasyPaging: subclasses keep data and page fields; the base class wires refresh / load, empty state, and noMore to FastRefresh.

isNoMore uses total first (count >= total), otherwise page >= totalPage. If onLoad returns no result, the base class emits noMore or success from that flag. When the first page already contains everything, refresh finishes by locking the footer as noMore.

dart
class CustomPaging extends FastPaging<List<String>, String> {
  const CustomPaging({super.key, super.refreshOnStart = true, super.itemBuilder});

  @override
  FastPagingState<List<String>, String, CustomPaging> createState() =>
      _CustomPagingState();
}

class _CustomPagingState
    extends FastPagingState<List<String>, String, CustomPaging> {
  @override
  int get count => data?.length ?? 0;

  @override
  String getItem(int index) => data![index];

  @override
  int? page;

  @override
  int? total;

  @override
  int? totalPage;

  @override
  Widget buildItem(BuildContext context, int index, String item) {
    return buildItemByBuilder(context, index, item);
  }

  @override
  Future<FastRefreshResult?> onRefresh() async {
    final first = await fetchPage(1);
    setState(() {
      data = first.items;
      page = first.page;
      total = first.total;
    });
    return null;
  }

  @override
  Future<FastRefreshResult?> onLoad() async {
    final next = await fetchPage(page! + 1);
    setState(() {
      data = <String>[...data!, ...next.items];
      page = next.page;
      total = next.total;
    });
    return null;
  }
}
TopicNotes
DefaultuseDefaultPhysics: false uses FastRefresh.builder and attaches physics to the inner CustomScrollView
Widget constructoruseDefaultPhysics: true for a single list with no nested scroll
EmptyemptyWidgetBuilder or override buildEmptyWidget when isEmpty
On startrefreshOnStart + refreshOnStartWidgetBuilder
LocatorLocator slivers are inserted when Header / Footer position is locator

See the example app’s refresh_example/paging (45 items, 10 per page, empty-state and widget-constructor toggles).


Load-more and scrolling ​

When the user is still flinging toward the bottom, onLoad often pushes the next page in the same window. The hitch is usually not “a few extra rows”. It is a ballistic scroll whose content height changes every frame, so the spring is torn down and rebuilt.

The library already does this:

OptimizationBehavior
Keep in-range inertiaAfter new items grow maxScrollExtent and pixels are back in range, FastRefresh does not goBallistic just because the footer is still processing
Rebound only if neededAfter processed, the spring is retargeted only while still overscrolled; an in-range fling keeps its current activity
Hold the scroll offsetWhen the list grows out of overscroll, the footer offset is cleared and pixels are not pulled back by the spring

Combined with Animated List, a tail append during load-more or an in-flight scroll snaps with zero duration so a SizeTransition cannot rewrite height every frame. Idle batch inserts still stagger. FastPagingList (a plain SliverList) gets the table above as well, not only the animated list.

The app can still save a frame:

  • Use itemExtent / prototypeItem when row height is fixed
  • Give tiles a stable ValueKey / itemId so the list does not rebuild from scratch
  • Do not push a huge page in one shot; do not decode images synchronously on the insert frame

The combined recipe is the “Refresh” scene under animated_list_example.


Horizontal ​

Switch the list to horizontal scroll. Classic Header / Footer move to the leading and trailing edges.

dart
FastRefresh(
  clipBehavior: Clip.none,
  header: const FastClassicHeader(),
  footer: const FastClassicFooter(infiniteOffset: null),
  onRefresh: () async {},
  onLoad: () async {},
  child: ListView.builder(
    scrollDirection: Axis.horizontal,
    itemCount: items.length,
    itemBuilder: (_, i) => SizedBox(width: 220, child: Text(items[i])),
  ),
);
TopicNotes
scrollDirectionWith Axis.horizontal, Header is on the left and Footer on the right (forward lists)
triggerAxisOptional. Axis.horizontal responds only on that axis; null means no filter
PageViewSet footer infiniteOffset: null, or paging a page starts onLoad
clipBehaviorUse Clip.none when the indicator paints outside the viewport

See refresh_example/horizontal for a page that toggles ListView and PageView.


Secondary floor ​

Pull past the normal refresh trigger to open a near-full-screen second page. The state machine is already in the kernel; compose the page with FastSecondaryBuilderHeader.

inactive → drag → armed → …          normal refresh
                 ↘ secondaryArmed → secondaryReady → secondaryOpen
                                                      ↓
                                              secondaryClosing → inactive
ParameterNotes
secondaryTriggerOffsetDistance that opens the floor; must be greater than triggerOffset
secondaryDimensionHeight when open; defaults to the viewport
secondaryVelocitySnap-open speed after release; default 3000
secondaryCloseTriggerOffsetHow far to push back before closing; default 70

Do not combine secondaryTriggerOffset with infiniteOffset (the default Header has no infinite refresh; do not enable it on a secondary Header).

dart
FastRefresh(
  clipBehavior: Clip.none,
  controller: controller,
  header: FastSecondaryBuilderHeader(
    header: const FastClassicHeader(
      position: FastRefreshIndicatorPosition.locator,
      clipBehavior: Clip.none,
      safeArea: false,
    ),
    secondaryTriggerOffset: 120,
    secondaryDimension: screenHeight - kToolbarHeight - topPadding,
    listenable: listenable,
    builder: (context, state, header) {
      return Stack(
        clipBehavior: Clip.none,
        children: [
          SizedBox(height: state.offset, width: double.infinity),
          // Second-floor page: screen height, opacity follows the gesture
          header.build(context, state),
        ],
      );
    },
  ),
  child: CustomScrollView(
    slivers: [
      // Sync SliverAppBar with listenable
      const FastHeaderLocator.sliver(),
      // list
    ],
  ),
);

Open / close:

  • Gesture: pull past secondaryTriggerOffset and release
  • Programmatic: controller.openHeaderSecondary() / closeHeaderSecondary()
  • Back: while open, PopScope(canPop: false) should call closeHeaderSecondary()

FastRefresh.clipBehavior must be Clip.none, or the taller second-floor page is clipped. Full recipe: refresh_example/secondary.


Material ​

The default skin is still Classic. For the system crescent spinner, use FastMaterialHeader (default clamping: true). FastMaterialFooter uses CircularProgressIndicator and still infinite-loads (infiniteOffset: 70, clamping: false — clamping cannot combine with infinite load).

dart
FastRefresh(
  header: const FastMaterialHeader(),
  footer: const FastMaterialFooter(),
  onRefresh: () async {},
  onLoad: () async {},
  child: ListView(),
);

Theme ​

FastRefreshTheme is a ThemeExtension. Classic copy / styles and Material colors resolve as widget args → theme → light/dark English defaults. It does not change trigger distance or springs.

dart
ThemeData(
  extensions: [
    FastRefreshTheme(
      headerTexts: FastRefreshIndicatorTexts(
        dragText: 'Pull to refresh',
        armedText: 'Release ready',
        readyText: 'Refreshing...',
        processingText: 'Refreshing...',
        processedText: 'Succeeded',
        noMoreText: 'No more',
        failedText: 'Failed',
        messageText: 'Last updated at %T',
      ),
      footerTexts: FastRefreshIndicatorTexts.footerEnglish,
      indicatorColor: Colors.blue,
    ),
  ],
)

Without a registered theme, Classic keeps its English defaults (Pull to refresh).


Custom indicator ​

dart
FastRefresh(
  header: FastBuilderHeader(
    triggerOffset: 70,
    processedDuration: Duration.zero,
    builder: (context, state) {
      return SizedBox(
        height: state.offset,
        child: Center(child: Text(state.mode.name)),
      );
    },
  ),
  onRefresh: () async {},
  child: ListView(),
);

Released under the BSD 3-Clause License