颤振-向下拖动以解散

时间:2018-08-19 00:38:20

标签: flutter

我想通过向下拖动来关闭页面。 但是我不知道是使用<p>还是使用gesture,无论如何我都不知道在向下拖动时如何显示上一页。 有人知道怎么做吗?

3 个答案:

答案 0 :(得分:2)

您可以为此使用Dismissible小部件。文档示例:https://flutter.io/docs/cookbook/gestures/dismissible

答案 1 :(得分:1)

使用Dismissible向上/向下拖动以关闭页面的简单示例:

Dismissible(
    direction: DismissDirection.vertical,
    key: Key('key'),
    onDismissed: (direction) {
      Navigator.of(context).pop();
    },
    child: yourChild
)

答案 2 :(得分:0)

我正在为自己解决同一问题

enter image description here

这是above上方演示的dartpad link

完整源代码

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Open route'),
          onPressed: () {
            showGeneralDialog(
              barrierLabel: "Label",
              barrierDismissible: false,
              barrierColor: Colors.black.withOpacity(0.5),
              transitionDuration: Duration(milliseconds: 400),
              context: context,
              pageBuilder: (context, anim1, anim2) {
                return SecondRoute();
              },
              transitionBuilder: (context, anim1, anim2, child) {
                return SlideTransition(
                  position: Tween(begin: Offset(0, 1), end: Offset(0, 0))
                      .animate(anim1),
                  child: child,
                );
              },
            );
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Dismissible(
      direction: DismissDirection.vertical,
      key: const Key('key'),
      onDismissed: (_) => Navigator.of(context).pop(),
      child: Scaffold(
        appBar: AppBar(
          title: Text("Second Route"),
        ),
        body: Align(
          alignment: Alignment.center,
          child: Container(
            height: 300,
            width: 300,
            child: SizedBox.expand(child: FlutterLogo()),
            margin: EdgeInsets.only(bottom: 50, left: 12, right: 12),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(40),
            ),
          ),
        ),
      ),
    );
  }
}

注意:未测试SecondRoute具有(ListViewSingleChildScroll)的情况,但我认为在这种情况下,如果您从AppBar或未覆盖的位置开始拖动它应该按预期关闭的ListView