Skip to content

Commit

Permalink
fix: android back button
Browse files Browse the repository at this point in the history
  • Loading branch information
popoway committed Jan 17, 2024
1 parent 7770c8b commit 57cb59d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ Open Source Licenses and Acknowledgements are available within the "About" dialo
</tbody>
</table>

Special thanks to the [Office of Associate Provost for Innovation and Student Success](https://www.qc.cuny.edu/student-success/), the [International Students and Scholars Office](https://www.qc.cuny.edu/academics/iss/), and the [Office of Student Affairs and Enrollment Management](https://www.qc.cuny.edu/student-affairs-enrollment-management/) at Queens College for their support and guidance.
Special thanks to the following departments at Queens College for their support and guidance:

- [Office of Associate Provost for Innovation and Student Success](https://www.qc.cuny.edu/student-success/)
- [International Students and Scholars Office](https://www.qc.cuny.edu/academics/iss/)
- [Office of Student Affairs and Enrollment Management](https://www.qc.cuny.edu/student-affairs-enrollment-management/)

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
12 changes: 12 additions & 0 deletions lib/aboutapp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:back_button_interceptor/back_button_interceptor.dart';

class AboutAppPage extends StatefulWidget {
const AboutAppPage({super.key, required this.title});
Expand Down Expand Up @@ -32,11 +33,22 @@ class _AboutAppPageState extends State<AboutAppPage> {
buildSignature: 'Unknown',
installerStore: 'Unknown',
);
bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
Navigator.of(context).pop();
return true;
}

@override
void initState() {
super.initState();
_initPackageInfo();
BackButtonInterceptor.add(myInterceptor);
}

@override
void dispose() {
BackButtonInterceptor.remove(myInterceptor);
super.dispose();
}

Future<void> _initPackageInfo() async {
Expand Down
23 changes: 23 additions & 0 deletions lib/home.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
Expand Down Expand Up @@ -49,6 +50,20 @@ class CupertinoTabBarDemo extends StatelessWidget {
CupertinoIcons.profile_circled,
),
];
// set system navigation bar color and icon brightness for android
var mySystemTheme =
MediaQuery.of(context).platformBrightness == Brightness.light
? SystemUiOverlayStyle.light.copyWith(
systemNavigationBarColor:
CupertinoTheme.of(context).barBackgroundColor,
systemNavigationBarIconBrightness: Brightness.dark,
)
: SystemUiOverlayStyle.dark.copyWith(
systemNavigationBarColor:
CupertinoTheme.of(context).barBackgroundColor,
systemNavigationBarIconBrightness: Brightness.light,
);
SystemChrome.setSystemUIOverlayStyle(mySystemTheme);

return WillPopScope(
// forbidden swipe in iOS(my ThemeData(platform: TargetPlatform.iOS,) from onboarding.dart)
Expand Down Expand Up @@ -415,6 +430,14 @@ class _MyHomePageState extends State<MyHomePage> {
bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
// navigator pop
print("BACK BUTTON!"); // Do some stuff.
// if there are more than 1 routes, pop
if (Navigator.of(context).canPop()) {
Navigator.of(context).pop();
} else {
// if there is only 1 route, exit app
SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');
return true;
}
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 2.0.0+36
version: 2.0.0+37

environment:
sdk: '>=3.1.0 <4.0.0'
Expand Down

0 comments on commit 57cb59d

Please sign in to comment.