![](/img/trans.png)
[英]Flutter : willpopScope is not working in appBar back button
[英]Flutter testing WillPopScope with back button
在我的主頁小部件上,當用戶點擊系統后退按鈕時,它會通過 WillPopScope 顯示一個確認對話框小部件。 我想測試這個對話框,但我不知道如何按下測試文件上的后退按鈕。
我不知道如何按測試文件上的后退按鈕。
這將幫助您查看應用欄(應用頂部)上的后退按鈕。 它將允許您通過 appbar 查看返回按鈕
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
title: Text("Title"),
centerTitle: true,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => _onWillPop(),
),
),
body: Body(),
),
);
_onWillPop 將成為您問題的“確認對話框小部件”。
顯然,您無法在測試中訪問Navigator
。 所以我的解決方案是在抽水小部件中的某處包含BackButton
並像這樣彈出頁面:
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Container(
child: BackButton(),
),
),
));
...
await tester.pageBack();
我有同樣的問題,但我的應用程序中沒有后退按鈕。 我想測試 android 系統后退按鈕。 我就是這樣做的。 也許它對遇到和我一樣問題的人有幫助。
testWidgets("test onWillPop",(WidgetTester tester) async {
bool willPopCalled = false;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: WillPopScope(
onWillPop: () async {
willPopCalled = true;
return false;
},
child: Container(),
),
),
),
);
final dynamic widgetsAppState = tester.state(find.byType(WidgetsApp));
await widgetsAppState.didPopRoute();
await tester.pump();
expect(willPopCalled, true);
});
靈感來自: https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/will_pop_test.dart
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.