Skip to content

Commit 65a2232

Browse files
committed
Method Channel Improvements Done
1 parent 87f2baf commit 65a2232

File tree

10 files changed

+125
-28
lines changed

10 files changed

+125
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
package com.webreinvent.vaahflutter
22

3+
import android.os.Bundle
4+
import android.widget.Toast
35
import io.flutter.embedding.android.FlutterActivity
6+
import io.flutter.embedding.engine.FlutterEngine
7+
import io.flutter.plugin.common.MethodChannel
48

5-
class MainActivity: FlutterActivity() {
9+
class MainActivity : FlutterActivity() {
10+
private val CHANNEL = "snackbar_channel"
11+
12+
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
13+
super.configureFlutterEngine(flutterEngine)
14+
15+
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
16+
if (call.method == "showSnackbar") {
17+
showSnackbar()
18+
result.success("Snackbar displayed")
19+
} else {
20+
result.notImplemented()
21+
}
22+
}
23+
}
24+
25+
private fun showSnackbar() {
26+
runOnUiThread {
27+
Toast.makeText(this, "This is a test", Toast.LENGTH_SHORT).show()
28+
}
29+
}
630
}

assets/env/develop.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"one_signal_config": null,
1818
"pusher_config": null,
1919
"show_debug_panel": true,
20+
"method_channel_map": {},
2021
"debug_panel_color": 3422552064
21-
}
22+
}

assets/env/production.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"one_signal_config": null,
1818
"pusher_config": null,
1919
"show_debug_panel": false,
20+
"method_channel_map": {},
2021
"debug_panel_color": 3422552064
21-
}
22+
}

assets/env/staging.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"one_signal_config": null,
1818
"pusher_config": null,
1919
"show_debug_panel": true,
20+
"method_channel_map": {},
2021
"debug_panel_color": 3422552064
21-
}
22+
}

lib/vaahextendflutter/env/env.dart

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class EnvironmentConfig {
6262
this.oneSignalConfig,
6363
this.pusherConfig,
6464
required this.showDebugPanel,
65+
required this.methodChannelMap,
6566
required this.debugPanelColor,
6667
});
6768

@@ -82,6 +83,7 @@ class EnvironmentConfig {
8283
final OneSignalConfig? oneSignalConfig;
8384
final PusherConfig? pusherConfig;
8485
final bool showDebugPanel;
86+
final Map<String, dynamic> methodChannelMap;
8587
@JsonKey(fromJson: _colorFromJson, toJson: _colorToJson)
8688
final Color debugPanelColor;
8789

@@ -123,6 +125,7 @@ class EnvironmentConfig {
123125
pushNotificationsServiceType: PushNotificationsServiceType.none,
124126
internalNotificationsServiceType: InternalNotificationsServiceType.none,
125127
showDebugPanel: true,
128+
methodChannelMap: {}, // Map native method names to their corresponding channels here
126129
debugPanelColor: Colors.black.withOpacity(0.8),
127130
);
128131
}

lib/vaahextendflutter/env/env.g.dart

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
abstract class BasePlatformService {
2+
Future<T?> invokeMethod<T>(String method, [dynamic arguments]);
3+
Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]);
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:flutter/services.dart';
2+
3+
import '../../../env/env.dart';
4+
import 'base_service.dart';
5+
6+
class PlatformChannelService implements BasePlatformService {
7+
static final Map<String, EventChannel> _eventChannels = {};
8+
9+
@override
10+
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) async {
11+
final String methodChannelName = EnvironmentConfig.getConfig.methodChannelMap[method];
12+
final MethodChannel channel = MethodChannel(methodChannelName);
13+
try {
14+
final result = await channel.invokeMethod<T>(method, arguments);
15+
return result;
16+
} on MissingPluginException catch (e) {
17+
if (channel.name.isEmpty) {
18+
throw 'Please provide correct method_channel_name in env config: ${e.message}';
19+
}
20+
throw 'No plugin handler for the method call was found: ${e.message}(${channel.name})';
21+
} on PlatformException catch (e) {
22+
throw 'Failed to invoke method: ${e.message}';
23+
} on Exception catch (_) {
24+
rethrow;
25+
}
26+
}
27+
28+
@override
29+
Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) {
30+
if (!_eventChannels.containsKey(eventChannelName)) {
31+
_eventChannels[eventChannelName] = EventChannel(eventChannelName);
32+
}
33+
return _eventChannels[eventChannelName]!.receiveBroadcastStream(arguments);
34+
}
35+
}

lib/views/pages/home.dart

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter/services.dart';
23

34
import '../../vaahextendflutter/services/notification/internal/notification_view.dart';
45
import 'ui/index.dart';
@@ -20,16 +21,41 @@ class HomePage extends StatefulWidget {
2021
}
2122

2223
class _HomePageState extends State<HomePage> {
24+
static const MethodChannel _platform = MethodChannel('snackbar_channel');
25+
26+
/// Calls the native platform method to show a snackbar.
27+
Future<void> _showNativeSnackbar() async {
28+
try {
29+
final String? result = await _platform.invokeMethod<String>('showSnackbar');
30+
if (result != null) {
31+
debugPrint("Native Snackbar Response: $result");
32+
} else {
33+
debugPrint("Native Snackbar returned null response");
34+
}
35+
} on PlatformException catch (e, stackTrace) {
36+
debugPrint("Failed to show snackbar: ${e.message}, Code: ${e.code}");
37+
debugPrint(stackTrace.toString());
38+
} on MissingPluginException {
39+
debugPrint("MethodChannel 'showSnackbar' not implemented on this platform.");
40+
} catch (e, stackTrace) {
41+
debugPrint("Unexpected error while showing snackbar: $e");
42+
debugPrint(stackTrace.toString());
43+
}
44+
}
45+
2346
@override
2447
void initState() {
2548
super.initState();
26-
// TODO:
27-
// increaseOpenCount();
49+
// TODO: increaseOpenCount();
2850
}
2951

3052
@override
3153
Widget build(BuildContext context) {
3254
return Scaffold(
55+
floatingActionButton: FloatingActionButton(
56+
onPressed: _showNativeSnackbar,
57+
child: const Icon(Icons.message),
58+
),
3359
appBar: AppBar(
3460
actions: const [
3561
InternalNotificationsBadge(),

pubspec.lock

+22-22
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ packages:
55
dependency: transitive
66
description:
77
name: _fe_analyzer_shared
8-
sha256: f256b0c0ba6c7577c15e2e4e114755640a875e885099367bf6e012b19314c834
8+
sha256: "16e298750b6d0af7ce8a3ba7c18c69c3785d11b15ec83f6dcd0ad2a0009b3cab"
99
url: "https://pub.dev"
1010
source: hosted
11-
version: "72.0.0"
11+
version: "76.0.0"
1212
_flutterfire_internals:
1313
dependency: transitive
1414
description:
@@ -21,15 +21,15 @@ packages:
2121
dependency: transitive
2222
description: dart
2323
source: sdk
24-
version: "0.3.2"
24+
version: "0.3.3"
2525
analyzer:
2626
dependency: transitive
2727
description:
2828
name: analyzer
29-
sha256: b652861553cd3990d8ed361f7979dc6d7053a9ac8843fa73820ab68ce5410139
29+
sha256: "1f14db053a8c23e260789e9b0980fa27f2680dd640932cae5e1137cce0e46e1e"
3030
url: "https://pub.dev"
3131
source: hosted
32-
version: "6.7.0"
32+
version: "6.11.0"
3333
args:
3434
dependency: transitive
3535
description:
@@ -178,10 +178,10 @@ packages:
178178
dependency: transitive
179179
description:
180180
name: collection
181-
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
181+
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
182182
url: "https://pub.dev"
183183
source: hosted
184-
version: "1.18.0"
184+
version: "1.19.0"
185185
colorize:
186186
dependency: "direct main"
187187
description:
@@ -529,18 +529,18 @@ packages:
529529
dependency: transitive
530530
description:
531531
name: leak_tracker
532-
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
532+
sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06"
533533
url: "https://pub.dev"
534534
source: hosted
535-
version: "10.0.5"
535+
version: "10.0.7"
536536
leak_tracker_flutter_testing:
537537
dependency: transitive
538538
description:
539539
name: leak_tracker_flutter_testing
540-
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
540+
sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379"
541541
url: "https://pub.dev"
542542
source: hosted
543-
version: "3.0.5"
543+
version: "3.0.8"
544544
leak_tracker_testing:
545545
dependency: transitive
546546
description:
@@ -569,10 +569,10 @@ packages:
569569
dependency: transitive
570570
description:
571571
name: macros
572-
sha256: "0acaed5d6b7eab89f63350bccd82119e6c602df0f391260d0e32b5e23db79536"
572+
sha256: "1d9e801cd66f7ea3663c45fc708450db1fa57f988142c64289142c9b7ee80656"
573573
url: "https://pub.dev"
574574
source: hosted
575-
version: "0.1.2-main.4"
575+
version: "0.1.3-main.0"
576576
matcher:
577577
dependency: transitive
578578
description:
@@ -786,7 +786,7 @@ packages:
786786
dependency: transitive
787787
description: flutter
788788
source: sdk
789-
version: "0.0.99"
789+
version: "0.0.0"
790790
source_gen:
791791
dependency: transitive
792792
description:
@@ -823,10 +823,10 @@ packages:
823823
dependency: transitive
824824
description:
825825
name: stack_trace
826-
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
826+
sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377"
827827
url: "https://pub.dev"
828828
source: hosted
829-
version: "1.11.1"
829+
version: "1.12.0"
830830
stream_channel:
831831
dependency: transitive
832832
description:
@@ -847,10 +847,10 @@ packages:
847847
dependency: transitive
848848
description:
849849
name: string_scanner
850-
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
850+
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
851851
url: "https://pub.dev"
852852
source: hosted
853-
version: "1.2.0"
853+
version: "1.3.0"
854854
term_glyph:
855855
dependency: transitive
856856
description:
@@ -863,10 +863,10 @@ packages:
863863
dependency: transitive
864864
description:
865865
name: test_api
866-
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
866+
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
867867
url: "https://pub.dev"
868868
source: hosted
869-
version: "0.7.2"
869+
version: "0.7.3"
870870
timezone:
871871
dependency: "direct main"
872872
description:
@@ -911,10 +911,10 @@ packages:
911911
dependency: transitive
912912
description:
913913
name: vm_service
914-
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
914+
sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b
915915
url: "https://pub.dev"
916916
source: hosted
917-
version: "14.2.5"
917+
version: "14.3.0"
918918
watcher:
919919
dependency: transitive
920920
description:

0 commit comments

Comments
 (0)