【问题标题】:Close a BottomSheet containing a DraggableScrollableSheet in flutter在颤动中关闭包含 DraggableScrollableSheet 的 BottomSheet
【发布时间】:2021-02-11 21:24:03
【问题描述】:

我正在使用 showButtomSheet 显示包含 DraggableScrollableSheet 的 BottomSheet。我想通过单击 DarggableScrollableSheet 上的按钮来关闭 BottomSheet。我该怎么做?

只要 DraggableScrollableSheet 不靠近屏幕顶部,我就可以让它工作,但是当它接近顶部时,表格下方会出现一个模态类型的叠加层,当我关闭时工作表,模态覆盖仍然存在。

我尝试使用 PersistentBottomSheetController.close() 来关闭工作表,并使用 Navigator.pop 如下所示,但结果是一样的:关闭工作表后仍然存在深色覆盖,我找不到一种删除它的方法。

飞镖盘:http://dartpad.dev/7ec436f3c850936d74dcdbb6ff17f97c

截屏:https://i.stack.imgur.com/vE1VE.gif

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        body: MyStatelessWidget(),
      ),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  MyStatelessWidget({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        child: const Text('showBottomSheet'),
        onPressed: () {
          Scaffold.of(context).showBottomSheet<void>(
                (BuildContext context) {

              return DraggableScrollableSheet(
                  expand: false,
                  maxChildSize: 0.935,
                  builder: (context, scrollController) => ListView(
                    controller: scrollController,
                    padding: const EdgeInsets.all(8),
                    children: <Widget>[
                      Container(
                        height: 200,
                        color: Colors.amber[600],
                        child: Center(
                          child: ElevatedButton(
                            child: const Text('Close BottomSheet'),
                            onPressed: () => Navigator.pop(context),
                          ),
                        ),
                      ),
                      Container(
                        height: 500,
                        color: Colors.amber[500],
                        child: const Center(child: Text('Entry B')),
                      ),
                    ],
                  ),
              );
            },
          );
        },
      ),
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    解决办法是把关闭动作改成这样:

    ElevatedButton(
        child: const Text('Close BottomSheet'),
        onPressed: () {
            Navigator.pop(context);
            Scaffold.of(context).showBodyScrim(false, 0.0);
        }
    ),
    

    当 BottomSheet 一直拖到顶部时,Scaffold 在 BottomSheet 下方添加一个 ModalBarrier。 ModalBarrier 可以通过 ScaffoldState 方法 showBodyScrim 进行控制:https://api.flutter.dev/flutter/material/ScaffoldState/showBodyScrim.html

    【讨论】:

      【解决方案2】:

      Scaffold.of(context).showBottomSheet&lt;void&gt; 更改为showModalBottomSheet

      showModalBottomSheet(
        isScrollControlled: true,
        barrierColor: Colors.transparent,
        isDismissible: false,
        context: context, 
        builder: (context)=> ...,
      );
      

      【讨论】:

      • 不幸的是,模式不是一个选项,因为它应该可以在工作表打开一半时与下面的内容进行交互。
      猜你喜欢
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2019-03-21
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多