Skip to content

Commit

Permalink
fix: 修复无法从主页退出应用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Dec 6, 2023
1 parent 079bc2e commit d238a95
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

## [Unreleased]

### Fixed

- 修复无法从主页退出应用的问题

## [0.9.5] - 2023-12-06

### Added
Expand Down
3 changes: 2 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<application
android:label="${appName}"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:enableOnBackInvokedCallback="true">
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
14 changes: 10 additions & 4 deletions lib/blog/view/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,18 @@ class _BlogHomeScreenState extends State<BlogHomeScreen> {
},
child: const Icon(Icons.open_in_new),
),
onWillPop: () async {
if (await _controller.webviewController.canGoBack()) {
onPopInvoked: (didPop) async {
if (didPop) {
return;
}

final NavigatorState navigator = Navigator.of(context);
final canGoBack = await _controller.webviewController.canGoBack();
if (canGoBack) {
await _controller.webviewController.goBack();
return false;
} else {
navigator.pop();
}
return true;
},
);
},
Expand Down
10 changes: 6 additions & 4 deletions lib/storage/view/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ class StorageHomeScreen extends StatelessWidget {
);
}
: null,
onWillPop: () async {
if (state is StorageHomeSuccess && state.itemType != ItemType.all) {
canPop: () {
return state is StorageHomeSuccess &&
state.itemType == ItemType.all;
},
onPopInvoked: (didPop) {
if (state is StorageHomeSuccess) {
BlocProvider.of<StorageHomeBloc>(context)
.add(const StorageHomeFetched(itemType: ItemType.all));
return false;
}
return true;
},
);
},
Expand Down
33 changes: 12 additions & 21 deletions lib/widgets/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class MyHomePage extends StatelessWidget {
final List<Widget>? slivers;
final Widget? floatingActionButton;
final Future<void> Function()? onRefresh;
final Future<bool> Function()? onWillPop;
final bool Function()? canPop;
final void Function(bool)? onPopInvoked;

const MyHomePage({
super.key,
Expand All @@ -20,7 +21,8 @@ class MyHomePage extends StatelessWidget {
this.slivers,
this.floatingActionButton,
this.onRefresh,
this.onWillPop,
this.canPop,
this.onPopInvoked,
});

@override
Expand All @@ -32,12 +34,13 @@ class MyHomePage extends StatelessWidget {
drawer: const MyDrawer(),
floatingActionButton: floatingActionButton,
onRefresh: onRefresh,
onWillPop: onWillPop,
bottomNavigationBar: TabSelector(
activeTab: activeTab,
onTabSelected: (tab) =>
BlocProvider.of<TabBloc>(context).add(TabChanged(tab)),
),
canPop: canPop,
onPopInvoked: onPopInvoked,
);
}
}
Expand All @@ -55,8 +58,9 @@ class MySliverScaffold extends StatelessWidget {
final Widget? drawer;
final PreferredSizeWidget? appbarBottom;
final Future<void> Function()? onRefresh;
final Future<bool> Function()? onWillPop;
final AppBarSize appBarSize;
final bool Function()? canPop;
final void Function(bool)? onPopInvoked;

const MySliverScaffold({
super.key,
Expand All @@ -68,32 +72,19 @@ class MySliverScaffold extends StatelessWidget {
this.bottomNavigationBar,
this.drawer,
this.onRefresh,
this.onWillPop,
this.appbarBottom,
this.appBarSize = AppBarSize.medium,
this.canPop,
this.onPopInvoked,
});

@override
Widget build(BuildContext context) {
return Scaffold(
drawer: drawer,
body: PopScope(
canPop: false,
onPopInvoked: (bool didPop) async {
if (didPop) {
return;
}
final NavigatorState navigator = Navigator.of(context);
final onWillPopCopy = onWillPop;
if (onWillPopCopy == null) {
navigator.pop();
} else {
final shouldPop = await onWillPopCopy();
if (shouldPop) {
navigator.pop();
}
}
},
canPop: canPop == null ? false : canPop!(),
onPopInvoked: onPopInvoked,
child: ConditionalParentWidget(
condition: onRefresh != null,
conditionalBuilder: (child) {
Expand Down

0 comments on commit d238a95

Please sign in to comment.