Last active
July 7, 2021 12:53
-
-
Save sondt2709/22f2aea35c5e5ce521553fb93ef346b3 to your computer and use it in GitHub Desktop.
Flutter persistent draggable bottom widget
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 'package:flutter/material.dart'; | |
class PersistentDraggableBottomWidget extends StatefulWidget { | |
PersistentDraggableBottomWidget() : super(); | |
@override | |
_PersistentDraggableBottomWidgetState createState() => _PersistentDraggableBottomWidgetState(); | |
} | |
class _PersistentDraggableBottomWidgetState extends State<PersistentDraggableBottomWidget> { | |
double _parentHeight = 0; | |
double _childHeight = 0; | |
double _ratio = 0.5; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Persistent Draggable Bottom Widget'), | |
), | |
body: Container( | |
color: Colors.green, | |
child: Builder( | |
builder: (context) { | |
WidgetsBinding.instance?.addPostFrameCallback((_) { | |
_parentHeight = context.size?.height ?? 0; | |
_calculate(); | |
print('Parent height: ' + (context.size?.height.toString() ?? 'null')); | |
}); | |
return Stack( | |
children: [ | |
Container( | |
color: Colors.yellow, | |
), | |
DraggableScrollableSheet( | |
expand: true, | |
initialChildSize: _ratio, | |
minChildSize: 0.1, | |
maxChildSize: _ratio, | |
builder: (context, scrollController) => SingleChildScrollView( | |
physics: ClampingScrollPhysics(), | |
controller: scrollController, | |
child: Builder( | |
builder: (context) { | |
WidgetsBinding.instance?.addPostFrameCallback((_) { | |
_childHeight = context.size?.height ?? 0; | |
_calculate(); | |
print('Child height: ' + (context.size?.height.toString() ?? 'null')); | |
}); | |
return Column( | |
children: [ | |
Container( | |
height: 200, | |
color: Colors.red, | |
), | |
Container( | |
height: 200, | |
color: Colors.cyan, | |
), | |
], | |
); | |
}, | |
), | |
), | |
), | |
], | |
); | |
}, | |
), | |
), | |
); | |
} | |
void _calculate() { | |
if (_parentHeight == 0 || _childHeight == 0) return; | |
final ratio = _childHeight / _parentHeight; | |
setState(() { | |
_ratio = ratio; | |
}); | |
} | |
} |
Author
sondt2709
commented
Jul 7, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment