From 0ac9b4f4d4d1fc62d045353d2344a92549a44962 Mon Sep 17 00:00:00 2001 From: FaganOoi Date: Thu, 3 Aug 2023 22:25:53 +0800 Subject: [PATCH] feat: :sparkles: Backward Compatible to control visbility Add enum to control the checking behavior --- .vscode/launch.json | 45 ++++++++++++++ README.md | 24 +++++--- example/.flutter-plugins-dependencies | 61 +------------------ example/android/app/build.gradle | 5 +- .../android/app/src/debug/AndroidManifest.xml | 2 +- .../android/app/src/main/AndroidManifest.xml | 3 +- .../com/example/example/MainActivity.kt | 2 +- .../app/src/profile/AndroidManifest.xml | 2 +- example/android/build.gradle | 6 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- example/ios/Runner.xcodeproj/project.pbxproj | 6 +- example/lib/main.dart | 13 ++-- example/pubspec.lock | 18 ++---- lib/enum.dart | 4 ++ lib/version_banner.dart | 20 +++++- 15 files changed, 109 insertions(+), 104 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 lib/enum.dart diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..252f6ff --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "flutter_version_banner", + "request": "launch", + "type": "dart" + }, + { + "name": "flutter_version_banner (profile mode)", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "flutter_version_banner (release mode)", + "request": "launch", + "type": "dart", + "flutterMode": "release" + }, + { + "name": "example", + "cwd": "example", + "request": "launch", + "type": "dart" + }, + { + "name": "example (profile mode)", + "cwd": "example", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "example (release mode)", + "cwd": "example", + "request": "launch", + "type": "dart", + "flutterMode": "release" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index a3bc2de..3c2483b 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,9 @@ An app Banner Widget that shows the current app version. Can be used with [App Flavors](https://flutter.dev/docs/deployment/flavors) to show what flavor of the app is currently being used. - ## Getting Started - -This widget should wrap `MaterialApp` or `CupertinoApp`. +This widget should wrap `MaterialApp` or `CupertinoApp`. ![Example](https://user-images.githubusercontent.com/10728633/59044609-5aa82c00-8876-11e9-99d2-da84a81af2e6.png) @@ -14,6 +12,7 @@ This widget should wrap `MaterialApp` or `CupertinoApp`. VersionBanner( text: "Yay!", packageExtensions: [".dev"], + extensionHandling: VersionBannerExtensionHandling.packageContainDev, child: MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', @@ -25,13 +24,18 @@ VersionBanner( ); ``` -`packageExtensions` will check the app's package name with the given array. If the package name ends with any of the String in the array, then the banner is visible. +`packageExtensions` will check the app's package name with the given array based on value of `extensionHandling`. + +[Original/Default Behavior] If `extensionHandling` is `VersionBannerExtensionHandling.packageContainDev` and the package name contains part of the String in the array, then the banner is visible. + +If `extensionHandling` is `VersionBannerExtensionHandling.packageSuffixDev` and the package name ends with any of the String in the array, then the banner is visible. The following properties can be changed: -* `color` - Banner's color -* `textStyle` - Banner's text style -* `text` - The text to appear. If set to null will show the `pubspec` version -* `location` - the banner's location on the screen, based on the class [BannerLocation](https://api.flutter.dev/flutter/widgets/BannerLocation-class.html) -* `packageExtensions` - will check the app's package name with the given array. If the package name ends with any of the String in the array, then the banner is visible. -* `visible` - decides if the banner is visible or not. If set to `false`, it overrides the `packageExtensions` attribute +- `color` - Banner's color +- `textStyle` - Banner's text style +- `text` - The text to appear. If set to null will show the `pubspec` version +- `location` - the banner's location on the screen, based on the class [BannerLocation](https://api.flutter.dev/flutter/widgets/BannerLocation-class.html) +- `packageExtensions` - will check the app's package name with the given array and decide the banner's visibility +- `extensionHandling` - will control the behavior for `packageExtensions` to work +- `visible` - decides if the banner is visible or not. If set to `false`, it overrides the `packageExtensions` attribute diff --git a/example/.flutter-plugins-dependencies b/example/.flutter-plugins-dependencies index e3edc05..539fb27 100644 --- a/example/.flutter-plugins-dependencies +++ b/example/.flutter-plugins-dependencies @@ -1,60 +1 @@ -{ - "date_created": "2023-06-05 18:54:51.753675", - "dependencyGraph": [ - { - "dependencies": [], - "name": "package_info_plus" - } - ], - "info": "This is a generated file; do not edit or check into version control.", - "plugins": { - "android": [ - { - "dependencies": [], - "name": "package_info_plus", - "native_build": true, - "path": "/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/" - } - ], - "ios": [ - { - "dependencies": [], - "name": "package_info_plus", - "native_build": true, - "path": "/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/" - } - ], - "linux": [ - { - "dependencies": [], - "name": "package_info_plus", - "native_build": false, - "path": "/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/" - } - ], - "macos": [ - { - "dependencies": [], - "name": "package_info_plus", - "native_build": true, - "path": "/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/" - } - ], - "web": [ - { - "dependencies": [], - "name": "package_info_plus", - "path": "/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/" - } - ], - "windows": [ - { - "dependencies": [], - "name": "package_info_plus", - "native_build": false, - "path": "/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/" - } - ] - }, - "version": "3.10.3" -} +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"package_info_plus","path":"/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/","native_build":true,"dependencies":[]}],"android":[{"name":"package_info_plus","path":"/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/","native_build":true,"dependencies":[]}],"macos":[{"name":"package_info_plus","path":"/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/","native_build":true,"dependencies":[]}],"linux":[{"name":"package_info_plus","path":"/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/","native_build":false,"dependencies":[]}],"windows":[{"name":"package_info_plus","path":"/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/","native_build":false,"dependencies":[]}],"web":[{"name":"package_info_plus","path":"/Users/fagan/.pub-cache/hosted/pub.dev/package_info_plus-4.0.2/","dependencies":[]}]},"dependencyGraph":[{"name":"package_info_plus","dependencies":[]}],"date_created":"2023-08-03 22:15:16.244989","version":"3.10.3"} \ No newline at end of file diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 5fe3c92..e3442b1 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -27,6 +27,7 @@ apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion flutter.compileSdkVersion + ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -43,8 +44,8 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId "com.example.example" - minSdkVersion flutter.minSdkVersion + applicationId "com.example.version.banner" + minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger() targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/example/android/app/src/debug/AndroidManifest.xml b/example/android/app/src/debug/AndroidManifest.xml index c208884..4536f7a 100644 --- a/example/android/app/src/debug/AndroidManifest.xml +++ b/example/android/app/src/debug/AndroidManifest.xml @@ -1,5 +1,5 @@ + package="com.example.version.banner"> diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 61adb01..0854a52 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,8 +1,7 @@ + package="com.example.version.banner"> + package="com.example.version.banner"> diff --git a/example/android/build.gradle b/example/android/build.gradle index 4256f91..f7eb7f6 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -1,12 +1,12 @@ buildscript { - ext.kotlin_version = '1.6.10' + ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.0' + classpath 'com.android.tools.build:gradle:7.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } @@ -26,6 +26,6 @@ subprojects { project.evaluationDependsOn(':app') } -task clean(type: Delete) { +tasks.register("clean", Delete) { delete rootProject.buildDir } diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties index bc6a58a..6b66533 100644 --- a/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/example/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 4153177..9bd5487 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -366,7 +366,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.example; + PRODUCT_BUNDLE_IDENTIFIER = com.example.version.banner; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_VERSION = 5.0; @@ -495,7 +495,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.example; + PRODUCT_BUNDLE_IDENTIFIER = com.example.version.banner; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -518,7 +518,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.example; + PRODUCT_BUNDLE_IDENTIFIER = com.example.version.banner; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_VERSION = 5.0; diff --git a/example/lib/main.dart b/example/lib/main.dart index 47b925c..8c54df1 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -22,12 +22,13 @@ class MyApp extends StatelessWidget { home: const MyHomePage(title: 'Flutter Demo Home Page'), ); - if (isDevVersion) { - return VersionBanner( - packageExtensions: const ["example"], child: materialApp); - } - - return materialApp; + return VersionBanner( + packageExtensions: const ["example"], + text: 'dev', + // uncomment if want to use suffix + // extensionHandling: VersionBannerExtensionHandling.packageSuffixDev, + child: materialApp, + ); } } diff --git a/example/pubspec.lock b/example/pubspec.lock index 846c2b9..187fe4e 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -25,14 +25,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.0" - charcode: - dependency: transitive - description: - name: charcode - sha256: fb98c0f6d12c920a02ee2d998da788bca066ca5f148492b7085ee23372b12306 - url: "https://pub.dev" - source: hosted - version: "1.3.1" clock: dependency: transitive description: @@ -82,7 +74,7 @@ packages: dependency: "direct dev" description: name: flutter_lints - sha256: b543301ad291598523947dc534aaddc5aaad597b709d2426d3a0e0d44c5cb493 + sha256: "2118df84ef0c3ca93f96123a616ae8540879991b8b57af2f81b76a7ada49b2a4" url: "https://pub.dev" source: hosted version: "2.0.2" @@ -108,7 +100,7 @@ packages: dependency: transitive description: name: http_parser - sha256: e362d639ba3bc07d5a71faebb98cde68c05bfbcfbbb444b60b6f60bb67719185 + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" url: "https://pub.dev" source: hosted version: "4.0.2" @@ -124,7 +116,7 @@ packages: dependency: transitive description: name: lints - sha256: a2c3d198cb5ea2e179926622d433331d8b58374ab8f29cdda6e863bd62fd369c + sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452" url: "https://pub.dev" source: hosted version: "2.1.1" @@ -180,7 +172,7 @@ packages: dependency: transitive description: name: plugin_platform_interface - sha256: "075f927ebbab4262ace8d0b283929ac5410c0ac4e7fc123c76429564facfb757" + sha256: "6a2128648c854906c53fa8e33986fc0247a1116122f9534dd20e3ab9e16a32bc" url: "https://pub.dev" source: hosted version: "2.1.4" @@ -241,7 +233,7 @@ packages: dependency: transitive description: name: typed_data - sha256: "53bdf7e979cfbf3e28987552fd72f637e63f3c8724c9e56d9246942dc2fa36ee" + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c url: "https://pub.dev" source: hosted version: "1.3.2" diff --git a/lib/enum.dart b/lib/enum.dart new file mode 100644 index 0000000..3892c22 --- /dev/null +++ b/lib/enum.dart @@ -0,0 +1,4 @@ +enum VersionBannerExtensionHandling { + packageContainDev, + packageSuffixDev, +} diff --git a/lib/version_banner.dart b/lib/version_banner.dart index 04f7557..92350b9 100644 --- a/lib/version_banner.dart +++ b/lib/version_banner.dart @@ -2,6 +2,7 @@ library version_banner; import 'package:flutter/widgets.dart'; import 'package:package_info_plus/package_info_plus.dart'; +import 'package:version_banner/enum.dart'; /// Version Banner Widget /// @@ -81,6 +82,8 @@ class VersionBanner extends StatelessWidget { /// the banner will not be shown. final List? packageExtensions; + final VersionBannerExtensionHandling extensionHandling; + VersionBanner({ required this.child, this.text, @@ -89,6 +92,7 @@ class VersionBanner extends StatelessWidget { color: Color.fromARGB(255, 255, 255, 255), fontWeight: FontWeight.bold), this.color = const Color.fromARGB(255, 255, 0, 0), this.visible = true, + this.extensionHandling = VersionBannerExtensionHandling.packageContainDev, this.packageExtensions, }); @@ -108,7 +112,10 @@ class VersionBanner extends StatelessWidget { if (packageExtensions != null) { for (var package in packageExtensions!) { if (snapshot.data != null && - snapshot.data!.packageName.endsWith(package)) { + isPackageNameMeetExtension( + snapshot.data!.packageName, + package, + )) { isDev = true; break; } @@ -158,4 +165,15 @@ class VersionBanner extends StatelessWidget { /// The banner is not visible, return the child instead return child; } + + bool isPackageNameMeetExtension( + String appPackageName, + String packageNameToMatch, + ) { + if (this.extensionHandling == + VersionBannerExtensionHandling.packageContainDev) + return appPackageName.contains(packageNameToMatch); + + return appPackageName.endsWith(packageNameToMatch); + } }