栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Flutter-通过滑动或按下floatActionButton来展开bottomNavigationBar

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter-通过滑动或按下floatActionButton来展开bottomNavigationBar

输出:


我使用了另一种方法,并且没有使用

AnimationController
GlobalKey
等等,逻辑代码非常短(
_handleClick
)。

我只使用了4个变量,简单而又简短!

void main() => runApp(MaterialApp(home: HomePage()));class HomePage extends StatefulWidget {  @override  _HomePageState createState() => _HomePageState();}class _HomePageState extends State<HomePage> {  static double _minHeight = 80, _maxHeight = 600;  Offset _offset = Offset(0, _minHeight);  bool _isOpen = false;  @override  Widget build(BuildContext context) {    return Scaffold(      backgroundColor: Color(0xFFF6F6F6),      appBar: AppBar(backgroundColor: Color(0xFFF6F6F6), elevation: 0),      body: Stack(        alignment: Alignment.bottomCenter,        children: <Widget>[          Align( alignment: Alignment.topLeft, child: FlatButton(   onPressed: _handleClick,   splashColor: Colors.transparent,   textColor: Colors.grey,   child: Text(_isOpen ? "Back" : ""), ),          ),          Align(child: FlutterLogo(size: 300)),          GestureDetector( onPanUpdate: (details) {   _offset = Offset(0, _offset.dy - details.delta.dy);   if (_offset.dy < _HomePageState._minHeight) {     _offset = Offset(0, _HomePageState._minHeight);     _isOpen = false;   } else if (_offset.dy > _HomePageState._maxHeight) {     _offset = Offset(0, _HomePageState._maxHeight);     _isOpen = true;   }   setState(() {}); }, child: AnimatedContainer(   duration: Duration.zero,   curve: Curves.easeOut,   height: _offset.dy,   alignment: Alignment.center,   decoration: BoxDecoration(       color: Colors.white,       borderRadius: BorderRadius.only(         topLeft: Radius.circular(30),         topRight: Radius.circular(30),       ),       boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.5), spreadRadius: 5, blurRadius: 10)]),   child: Text("This is my Bottom sheet"), ),          ),          Positioned( bottom: 2 * _HomePageState._minHeight - _offset.dy - 28, // 56 is the height of FAB so we use here half of it. child: FloatingActionButton(   child: Icon(_isOpen ? Icons.keyboard_arrow_down : Icons.add),   onPressed: _handleClick, ),          ),        ],      ),    );  }  // first it opens the sheet and when called again it closes.  void _handleClick() {    _isOpen = !_isOpen;    Timer.periodic(Duration(milliseconds: 5), (timer) {      if (_isOpen) {        double value = _offset.dy + 10; // we increment the height of the Container by 10 every 5ms         _offset = Offset(0, value);        if (_offset.dy > _maxHeight) {          _offset = Offset(0, _maxHeight); // makes sure it does't go above maxHeight          timer.cancel();        }      } else {        double value = _offset.dy - 10; // we decrement the height by 10 here        _offset = Offset(0, value);        if (_offset.dy < _minHeight) {          _offset = Offset(0, _minHeight); // makes sure it doesn't go beyond minHeight          timer.cancel();        }      }      setState(() {});    });  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410208.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号