Android Pysical后退按钮关闭应用程序

时间:2020-02-17 12:00:19

标签: flutter

我通过以下代码推送新的小部件:

Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (BuildContext context) => BigPhoto(widget.images)))
          },

在打开新页面后,当我单击“后退”按钮时,该android物理后退按钮随后关闭,而不是弹出。我该如何解决?

完整实施:

return Stack(
  children: <Widget>[
    StreamBuilder<int>(
      stream: bloc.currentItemStream,
      builder: (context, snapshot) {
        return GestureDetector(
          onTap: () => {
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (BuildContext context) => BigPhoto(widget.images, snapshot.data - 1)))
          },
          child: AspectRatio(
              aspectRatio: 411 / 308,
              child: PageView.builder(
                physics: BouncingScrollPhysics(),
                itemCount: widget.images.length,
                onPageChanged: (value) {
                  bloc.changeCurrentItemIndex(value + 1);
                },
                itemBuilder: (context, position) {
                  return FittedBox(
                    fit: BoxFit.cover,
                    child: Container(
                      child: Image.network(
                        "{widget.images[position]}",
                        fit: BoxFit.cover,
                      ),
                    ),
                  );
                },
              )),
        );
      }
    ),
    Positioned(
      left: 26,
      bottom: 9,
      child: StreamBuilder<int>(
          stream: bloc.currentItemStream,
          builder: (context, snapshot) {
            return Container(
              padding: const EdgeInsets.only(
                  bottom: 3, top: 3, left: 7, right: 7),
              decoration: BoxDecoration(
                border: Border.all(width: 3.0, color: Colors.white),
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(20.0)),
              ),
              child: Text(
                " ${snapshot.data} / ${widget.images.length}",
                style: TextStyle(
                    fontSize: 11.0,
                    fontFamily: 'Roboto-Regular',
                    backgroundColor: Colors.white),
              ),
            );
          }),
    ),
  ],
);

1 个答案:

答案 0 :(得分:2)

我建议这种解决方案: 通过将构建窗口小部件放入WillPopScoop小部件并将onWillPop设置为false,您将无法通过导航页面上的物理后退按钮来返回。然后像这样实现自己的后退按钮。

Widget build(BuildContext context) {
WillPopScope(
  onWillPop: () async => false,
  child: Material(
      child: Scaffold(
          appBar: AppBar(
          leading: IconButton(
                  icon: Icon(Icons.arrow_back_ios),
                  onPressed: () {
                    _goBack(context);
                  })),
          body : Image.asset("YOUR_URL_IMAGE"));

_goback()方法:

_goBack(BuildContext context) {
Navigator.pop(context); }