Created
May 4, 2023 09:49
-
-
Save CoderNamedHendrick/da82b727b49931755404c932f874d49a to your computer and use it in GitHub Desktop.
No scroll refresh page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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