Skip to content

Instantly share code, notes, and snippets.

@emvaized
Created June 30, 2021 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emvaized/fa1ce586109196d7357652de2843b8ff to your computer and use it in GitHub Desktop.
Save emvaized/fa1ce586109196d7357652de2843b8ff to your computer and use it in GitHub Desktop.
Test example of using webview with floating app bar on Flutter
...
AnimationController? _hidePanelController;
double _hidePanelsValue = 0.0;
String _url = 'https://flutter.dev/';
@override
void initState() {
super.initState();
_hidePanelController = new AnimationController(vsync: this);
}
@override
Widget build(BuildContext context) {
return Stack(children: [
/// Webview with touch listener
AnimatedBuilder(
animation: _hidePanelController!,
builder: (context, c) {
return Scaffold(
/// Empty space for floating appbar
appBar: PreferredSize(
preferredSize: Size(
MediaQuery.of(context).size.width, (kToolbarHeight - 1) * (1 - _hidePanelController!.value)),
child: Container(),
),
body: Listener(
onPointerMove: (_) {
/// Hide panel
///
_hidePanelsValue -= _.delta.dy / 60;
if (_hidePanelsValue > 1.0) _hidePanelsValue = 1.0;
if (_hidePanelsValue < 0.0) _hidePanelsValue = 0.0;
_hidePanelController?.value = _hidePanelsValue;
},
onPointerUp: (_) {
/// Snap panels
///
double _value = _hidePanelController!.value;
if (_value != 0.0 && _value != 1.0) {
if (_value < 0.5) {
_hidePanelController?.animateTo(0, duration: Duration(milliseconds: 150), curve: Curves.easeIn);
} else {
_hidePanelController?.animateTo(1, duration: Duration(milliseconds: 150), curve: Curves.easeIn);
}
}
},
child: InAppWebView(
initialUrlRequest: URLRequest(url: _url),
),
),
);
}),
/// Floating app bar
Positioned(
top: 0,
right: 0,
left: 0,
child: AnimatedBuilder(
animation: _hidePanelController!,
builder: (context, child) => SlideTransition(
position: Tween<Offset>(begin: const Offset(0, 0), end: Offset(0, -((kToolbarHeight / (kToolbarHeight + MediaQuery.of(context).padding.top)))))
.animate(_hidePanelController!),
child: ConstrainedBox(constraints: BoxConstraints(maxHeight: kToolbarHeight), child: AppBar(title: Text(_url))),
),
),
),
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment