dart - Flutter - 通过滑动或按下 floatingActionButton 来展开

我有一个名为 DashboardWidget 的主小部件。在里面,我有一个 Scaffold 和一个 BottomNavigationBar 和一个 FloatingActionButton:

现在,我想制作一个可以从底部拖动的小部件:

  1. 用手指向上滑动。
  2. 按下 FloatingActionButton

也就是说,我想展开BottomNavigationBar

Here's a design concept in case I was unclear.

问题是,我不确定从哪里开始实现。我曾考虑删除 BottomNavigationBar 并创建一个可以扩展的自定义小部件,但我也不确定是否可行。

最佳答案

输出:


我使用了不同的方法,没有AnimationControllerGlobalKey等,逻辑代码很短(_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(() {});
    });
  }
}

关于dart - Flutter - 通过滑动或按下 floatingActionButton 来展开 bottomNavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56042354/

相关文章:

google-maps - Flutter:如何使用新的 Marker API 向 Google M

flutter - 导航器使用 pushNamed 传递参数

android - Flutter - 如何使列屏幕可滚动

search - Flutter - 更改 SearchDelegate 的搜索提示文本

flutter - 如何在 flutter 中检索当前的类名?

flutter - 相机和图像选择器插件有什么区别?

android - 可以在 Android Studio 中使用 Flutter 开发 IOS 吗?

dart - 在 Flutter 中显示全屏图像 onTap

android - 如何在文本小部件 flutter 中显示 html 字符串

dart - 如何更改手机屏幕上的 flutter 应用程序名称显示?