Skip to content

Commit

Permalink
feat: ✨ Backward Compatible to control visbility
Browse files Browse the repository at this point in the history
Add enum to control the checking behavior
  • Loading branch information
FaganOoi committed Aug 3, 2023
1 parent dff7d32 commit 0ac9b4f
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 104 deletions.
45 changes: 45 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

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)

```dart
VersionBanner(
text: "Yay!",
packageExtensions: [".dev"],
extensionHandling: VersionBannerExtensionHandling.packageContainDev,
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
Expand All @@ -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
61 changes: 1 addition & 60 deletions example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -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"}
5 changes: 3 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
package="com.example.version.banner">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
Expand Down
3 changes: 1 addition & 2 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
package="com.example.version.banner">
<application
android:label="version_banner"
android:name="Version Banner"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.example
package com.example.version.banner

import io.flutter.embedding.android.FlutterActivity

Expand Down
2 changes: 1 addition & 1 deletion example/android/app/src/profile/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
package="com.example.version.banner">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
Expand Down
6 changes: 3 additions & 3 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}
}
Expand All @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}

Expand Down
18 changes: 5 additions & 13 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -124,7 +116,7 @@ packages:
dependency: transitive
description:
name: lints
sha256: a2c3d198cb5ea2e179926622d433331d8b58374ab8f29cdda6e863bd62fd369c
sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions lib/enum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum VersionBannerExtensionHandling {
packageContainDev,
packageSuffixDev,
}
20 changes: 19 additions & 1 deletion lib/version_banner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -81,6 +82,8 @@ class VersionBanner extends StatelessWidget {
/// the banner will not be shown.
final List<String>? packageExtensions;

final VersionBannerExtensionHandling extensionHandling;

VersionBanner({
required this.child,
this.text,
Expand All @@ -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,
});

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 0ac9b4f

Please sign in to comment.