Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CoderNamedHendrick/da82b727b49931755404c932f874d49a to your computer and use it in GitHub Desktop.
Save CoderNamedHendrick/da82b727b49931755404c932f874d49a to your computer and use it in GitHub Desktop.
No scroll refresh page.
/// A resuable widget for getting a page to refresh without needing to scroll with the ugly scroll behaviour
/// on ios. Used by wrapping the widget with the view with this widget as so
/// class MyHomePage extends StatelessWidget {
/// const MyHomePage({super.key, required this.title});
///
/// final String title;
///
/// @override
/// Widget build(BuildContext context) {
/// return NoScrollRefreshWidget(
/// onRefresh: () async {},
/// child: Scaffold(
/// appBar: AppBar(
/// title: Text(title),
/// ),
/// body: Center(
/// child: Column(
/// mainAxisAlignment: MainAxisAlignment.center,
/// children: const <Widget>[
/// Text(
/// 'You have pushed the button this many times:',
/// ),
/// ],
/// ),
/// ),
/// ),
/// );
/// }
/// }
class NoScrollRefreshWidget extends StatelessWidget {
const NoScrollRefreshWidget({
Key? key,
required this.onRefresh,
required this.child,
}) : super(key: key);
final Widget child;
final RefreshCallback onRefresh;
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (_, constraints) => RefreshIndicator(
onRefresh: onRefresh,
displacement: MediaQuery.of(context).viewPadding.top,
child: ScrollConfiguration(
behavior: _ClampingScrollBehavior(),
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: constraints.maxHeight,
maxHeight: constraints.maxHeight,
),
child: child,
),
),
),
),
);
}
}
class _ClampingScrollBehavior extends ScrollBehavior {
@override
ScrollPhysics getScrollPhysics(BuildContext context) =>
const ClampingScrollPhysics();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment