【问题标题】:Flutter - Edge of screen drag goes back a page unintentionallyFlutter - 屏幕拖动边缘无意返回页面
【发布时间】:2020-01-26 09:00:48
【问题描述】:

我一直在通过颤振使用材料来设计我的应用程序。意外地,我在屏幕边缘滑动,它返回一个页面到它不应该的路线。这在整个应用程序中是一致的,并且 Cupertino 包没有在任何地方使用。

在决定转到登录视图(如果已注销)或主页(已登录)之前,应用会以自定义启动屏幕启动

如果登录,向后滑动总是会返回到初始屏幕,因为它是之前的路线,并且应用程序崩溃。

第 1 页

  @override
  Widget build(BuildContext context) {
    return _loading == false
        ? Scaffold(
            backgroundColor: Colors.white,
            appBar: _buildBar(context),
            body: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          Text(
                            'test',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 18),
                          ),
                          SizedBox(height: 10),
                          ButtonTheme(
                              minWidth: MediaQuery.of(context).size.width / 2,
                              height: 40,
                              child: RaisedButton(
                                textColor: Colors.white,
                                color: prefix0.backgroundColor,
                                child: Text("View"),
                                onPressed: () => Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => PDFView(
                                              document: link,
                                            ))),
                                shape: new RoundedRectangleBorder(
                                  borderRadius: new BorderRadius.circular(15.0),
                                ),
                              )),
                        ],
                      ),  
            resizeToAvoidBottomPadding: false,
          )
        : Center(child: CircularProgressIndicator());
  }
}

第 2 页


class PDFView extends StatefulWidget {
  final String document;
  final File file;

  PDFView({this.document, this.file});
  @override
  _PDFViewState createState() => _PDFViewState();
}

class _PDFViewState extends State<PDFView> {
  bool _isLoading = true;

  Widget _buildBar(BuildContext context) {
    return new AppBar(
      backgroundColor: prefix0.backgroundColor,
      centerTitle: true,
      title: Text('PDF View'),
      actions: <Widget>[
        FlatButton(
          child: Icon(
            Icons.share,
            color: Colors.white,
          ),
          onPressed: () {
            // Share.share('${widget.document}');
          },
        ),
      ],
    );
  }

  PDFDocument doc;

  @override
  void initState() {
    super.initState();
    _getData();
  }

  _getData() async {
    if (widget.document != null) {
      doc = await PDFDocument.fromURL(widget.document);
      setState(() {
        _isLoading = false;
      });
    } else if (widget.file != null) {
      doc = await PDFDocument.fromFile(widget.file);
      setState(() {
        _isLoading = false;
      });
    } else {

      doc = await PDFDocument.fromURL(
          'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf');
      setState(() {
        _isLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildBar(context),
      body: Center(
          child: _isLoading
              ? Center(child: CircularProgressIndicator())
              : PDFViewer(
                  document: doc,
                  showPicker: false,
                )),
    );
  }
}

为一团糟,我在编辑文档方面的糟糕尝试表示歉意。

如何防止这些滑动?

寻找解决方案的可能发展:

在脚手架上方添加WillPopScope( onWillPop: () { return Future.value(false); }, 可防止滑动。但是,它也会禁用后退按钮。因此,只有当视图是“可弹出的”时,滑动操作才可用```

【问题讨论】:

  • 请提供您的代码和应用截图,让其他人了解您的问题。
  • 我添加了一些代码和图像来显示被拖动的屏幕。
  • 当您不想通过滑动(登录页面)返回该页面时,可以使用 pushReplacement。它将旧页面的索引替换为路由堆栈中新页面的索引

标签: flutter dart


【解决方案1】:

这是 iOS 上 Material 包的默认行为,iPhone 用户希望在应用程序的任何地方都有这种行为,但是如果您不想让用户在登录后立即返回某个特定屏幕,例如登录屏幕在这种情况下,您应该使用pushReplacement 而不仅仅是push,但如果您想在每种情况下都这样做,请查看here

【讨论】:

    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多