Created
June 30, 2021 00:38
-
-
Save emvaized/fa1ce586109196d7357652de2843b8ff to your computer and use it in GitHub Desktop.
Test example of using webview with floating app bar on Flutter
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
... | |
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