Last active
December 19, 2024 08:37
-
-
Save josh-burton/45adea1ea0bd23c239a01a21ba331482 to your computer and use it in GitHub Desktop.
Flutter CustomScrollView with footer background color that fills overscroll
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
import 'dart:math'; | |
import 'dart:ui'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const StickyFooterExample()); | |
} | |
class StickyFooterExample extends StatefulWidget { | |
const StickyFooterExample({super.key}); | |
@override | |
State<StickyFooterExample> createState() => _StickyFooterExampleState(); | |
} | |
class _StickyFooterExampleState extends State<StickyFooterExample> { | |
late ScrollController _scrollController; | |
@override | |
void initState() { | |
super.initState(); | |
_scrollController = ScrollController(); | |
} | |
@override | |
void dispose() { | |
_scrollController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.light().copyWith(), | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text("Sample"), | |
), | |
backgroundColor: Colors.red, | |
body: Stack( | |
children: [ | |
// Footer background that stays behind the content | |
ListenableBuilder( | |
listenable: _scrollController, | |
builder: (context, child) { | |
return Align( | |
alignment: Alignment.bottomCenter, | |
child: Container( | |
color: Colors.blue, | |
height: max(0.0, _scrollController.hasClients ? _scrollController.offset : 0.0), | |
width: double.infinity, | |
), | |
); | |
}, | |
), | |
ScrollConfiguration( | |
behavior: ScrollConfiguration.of(context).copyWith(dragDevices: { | |
PointerDeviceKind.touch, | |
PointerDeviceKind.mouse, | |
}), | |
child: CustomScrollView( | |
controller: _scrollController, | |
physics: AlwaysScrollableScrollPhysics( | |
parent: BouncingScrollPhysics(), | |
), | |
slivers: [ | |
SliverFillRemaining( | |
hasScrollBody: false, | |
fillOverscroll: true, | |
child: Column( | |
mainAxisSize: MainAxisSize.max, | |
mainAxisAlignment: MainAxisAlignment.end, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Container( | |
color: Colors.green, | |
height: .3 * MediaQuery.sizeOf(context).height, | |
), | |
Container( | |
color: Colors.blue, | |
height: .1 * MediaQuery.sizeOf(context).height, | |
), | |
], | |
), | |
), | |
], | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment