簡體   English   中英

WillPopScope 不適用於 Flutter 中的 IOS

[英]WillPopScope not working for IOS in Flutter

在我的 flutter 應用程序中,我想控制將彈出 scope 為 IOS,但它不起作用。 並且我沒有像一些示例建議的那樣使用 PageRoute 並且CupertinoWillPopScope package 不工作,它拋出錯誤anchorpoint 所以有人可以幫我嗎?

我為 IOS 嘗試了gestureDetector ,但它沒有觸發對上一頁的操作,這正是我想要做的。

if (!Platform.isIOS) {
      return WillPopScope(
        onWillPop: () async {
          timerController.startTimer();
          !Navigator.of(context).userGestureInProgress;
          return true;
        },
        child: MyPage(),
      );
    } else {
      return GestureDetector(
        onHorizontalDragUpdate: (details) {
          int sensitivity = 8;
          if (details.delta.dx > sensitivity) {
            // Right Swipe
            //timerController.startTimer();
          } else if (details.delta.dx < -sensitivity) {
            //Left Swipe
            timerController.startTimer();
          }
        },
        child: MyPage(),
      );
    }

假設您想要從頁面 A(NewPage) 到頁面 B(SecondRoute) 的 go。 要從 A 導航到 B,我們使用以下代碼:

Navigator.push(context, MaterialPageRoute(builder: (context) => const SecondRoute()))

稍后,當用戶使用滑動手勢或任何其他方式返回頁面 A 時,如果您想在用戶從 B 返回 A 時執行某些操作,我們可以在 Navigator.push function 中調用“then”。Go 通過以下代碼用於在用戶從頁面 B 返回頁面 A 后查看文本更改。

import 'package:flutter/material.dart';

class NewPage extends StatefulWidget {
  NewPage({Key? key}) : super(key: key);

  @override
  State<NewPage> createState() => _NewPageState();
}

class _NewPageState extends State<NewPage> {
  String inputstring = 'Here is a default String';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(inputstring),
              ElevatedButton(
                onPressed: () {
                  Navigator.push(
                          context, MaterialPageRoute(builder: (context) => const SecondRoute()))
                      .then((value) => {
                            setState(() {
                              inputstring = 'New Text after navigation is popped';
                            })
                          });
                },
                child: Text('GO To Second Page'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  const SecondRoute({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
    );
  }
}

在“then”中,您可以撥打任何您想要撥打的電話 function。 希望這能滿足您的需求。

問題沒有解決讓GitHub:這里

我為 WillPopScope 定制了WillPopScope它對我有用,我希望它也對你有用。

class MyWillPopScope extends StatelessWidget {

  MyWillPopScope({required this.child, this.onWillPop = false, Key? key}) : super(key: key);

  final Widget child;
  final bool onWillPop;

  @override
  Widget build(BuildContext context) {
    return Platform.isIOS ?
    GestureDetector(
      onPanUpdate: (details) {
        if (details.delta.dx > 0) {
          if(onWillPop){
            Navigator.of(context).pop();
          }
        }
      },
      child: WillPopScope(
        onWillPop: ()async{
          return false;
        },
        child: child,
      )
    )
    : WillPopScope(child: child, onWillPop: () async {
      return onWillPop;
    });
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM