Flutter:防止抽屉关闭

3

在Flutter中是否可以防止关闭抽屉?

我正在web应用中使用 endDrawer 作为表单。 我已经禁用了滑动打开,同时我也只想允许通过按钮关闭它,这样用户就不会因为点击外部而意外关闭它。 然而,我没有看到任何如prevent dismiss这样的布尔值或任何实现 WillPopScope 的方式。

那么 - 是否可以在不创建自己的 Drawer 实现的情况下实现这一点?

2个回答

1

我有同样的问题,不想让用户在滑动或在抽屉外部单击时意外关闭包含表单的抽屉。

我的解决方案是将抽屉的内容包装在一个GestureDetector中,然后添加:

onHorizontalDragEnd: (v) {
  // do nothing
},

通过点击外部关闭仍然是可能的,所以我这样修复了它:将抽屉设置为100%宽度,背景透明,并给Drawer的子元素指定所需的endDrawerWidth。
完整示例:
return Scaffold(
  endDrawerEnableOpenDragGesture: false,
  endDrawer: Container(
    width: MediaQuery.of(context).size.width,
    child: Drawer(
      backgroundColor: Colors.transparent,
      child: GestureDetector(
        onHorizontalDragEnd: (v) {
          // do nothing
        },
        child: Row(
          children: [
            Expanded(child: Container(color: Colors.transparent,)),
            Container(
              /// replace by your desired width
              width: MediaQuery.of(context).size.width/2,
              color: Colors.white,
              child: Center(child: Text("your content")),
            ),
          ],
        ),
      ),
    ),
  ),
  body: Center(child: Text("your boby")),
);

0
一个我使用的解决方法是在Scaffold.body中添加一行,然后将第一个小部件设置为抽屉,第二个小部件设置为实际的主体。通过不将其包含在Scaffold中,AppBar不会控制Drawer。
所以大致如下:
class FullScreenPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(title: Text("Big screen web app")),
    body: Row(
      children: [
        Drawer(/* Menu items here */),
        Placeholder(/* Normal body here */),
      ]
    )
  );
} 

谢谢,但这样做不能让它成为一个末尾抽屉,对吧?或者有没有办法将其放在右侧? - TheSpixxyQ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接