Flutter Bottom Sheet:如何在用户与 sheet 交互时更改高度

Flutter Bottom Sheet: How to change height when user interacts with the sheet

在我的 flutter 应用程序中,我有一个底部 sheet,显示的初始高度为 100。我想增加 sheet 的高度(比如可能是 500 或满屏幕)当用户与之交互时(可能是手势检测或单击 sheet 上的按钮)

是否可以在加载后更改 sheet 的高度。

我的屁股sheet有点像

  Widget _bottomSheetContainer(BuildContext context) {
    return Container(
        height: 100,
        decoration: BoxDecoration(
            border:
                Border(top: BorderSide(color: Theme.of(context).dividerColor))),
        child: Row(children: [
          Expanded(
            child:  Padding(
              padding: EdgeInsets.all(10.0),
              child: Text('Some text here!'),
            )
          ),
          Expanded(
            child: IconButton(
                icon: Icon(Icons.arrow_upward),
                // color: themeData.primaryColor,
                onPressed: () => _onPressed(context)
            ),
          ),
        ]));
  }

  _onPressed(BuildContext context) {
    BottomSheet w = context.widget;
    // Can we change the container height (from 100 to 500) that's part of the bottom sheet from here?

  }

您需要确保您的 widget 是 stateful widget,然后您可以使用 setState(() => ...) 更新它的实例参数,flutter 将更新渲染结果。

class _WidgetState extends State<Widget> {
  double height = 200.0; // starting height value

  Widget build() { 
    <...>
    Container(
        height: height,
    <...>
  }

  _onPressed(BuildContext context) {
      BottomSheet w = context.widget;
      // Can we change the container height (from 100 to 500) that's part of the bottom sheet from here?
      setState({
        height = 300.0; // new height value
      })
  }

}