底部 sheet 初始高度为屏幕的一半,如果滚动则高度增加到全屏

bottom sheet with initial height half of screen and if it scroll then height is increase to full screen

我有一个底部 sheet 和下面的代码,我把高度设置为 300.0 但我想在用户滚动时将高度增加到全屏..我该怎么做..请

void _showBottomSheet() {
    setState(() {
      _showPersBottomSheetCallBack = null;
    });

    _scaffoldKey.currentState
        .showBottomSheet((context) {
      return new Container(
        height: 300.0,
        color: Colors.greenAccent,
        child: new Center(
          child: new Text("Hi BottomSheet"),
        ),
      );
    })
        .closed
        .whenComplete(() {
      if (mounted) {
        setState(() {
          _showPersBottomSheetCallBack = _showBottomSheet;
        });
      }
    });
  }

flutter 包底部 sheet 并不意味着 会占据整个屏幕,尽管稍作调整就可以产生您期望的行为。 如果您查看 BottomSheet constructor,您会发现以下内容:

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    return new BoxConstraints(
      minWidth: constraints.maxWidth,
      maxWidth: constraints.maxWidth,
      minHeight: 0.0,
      maxHeight: constraints.maxHeight * 9.0 / 16.0
    );
  }

如果去掉9.0/16.0的高度限制,底部sheet会占据全屏高度。

如果您只想要全屏底部 sheet(如@edmond 所演示),您现在可以使用 isScrollControlled: true,而无需维护您自己的 BottomSheet 破解版.

无论如何,问题是关于首先将 sheet 加载到半高,并能够在滚动时扩展到全高。对于那个细粒度的控件,您仍然希望使用 isScrollControlled: true,但您也可以将模态 sheet 的内容包装在 DraggableScrollableSheet 中。 This Github comment 向您展示了如何操作,我将其复制到此处以供参考:

showModalBottomSheet(
  context: context,
  isScrollControlled: true,
  builder: (BuildContext context) {
    return DraggableScrollableSheet(
      initialChildSize: 0.5, // half screen on load
      maxChildSize: 1,       // full screen on scroll
      minChildSize: 0.25,
      builder: (BuildContext context, ScrollController scrollController) {
        return Container(
          color: Colors.white,
          child: ListView.builder(
            controller: scrollController,
            itemCount: 25,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(title: Text('Item $index'));
            },
          ),
        );
      },
    );
  },
);