About the SystemChannels API to take advantage of Flutter platform-specific features

Introduction

Describes the API System Channels for using platform-specific functions from Flutter.

What is System Channels

First of all, basically I don't recommend using this API </ font>. This API is used a lot inside the Flutter Framework, but since it is an intermediate layer API as shown below, it is highly likely that it will change in future version upgrades.

If you look at the [Source Code] of SystemChannels (https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/services/system_channels.dart), the contents use MethodChannel, EventChannel, etc. You can see that. Please refer to here for Method Channel and Event Channel.

スクリーンショット 2020-02-11 13.16.33.png

Types of System Channels

Several types are available for each application. The list is shown below.

type function Higher Flutter Service
lifecycle life cycle widget
navigation navigation widget, system_chrome
system unknown….hereUsed in, but virtually nothing widget
accessibility Accessibility(Text reading etc.) PlatformViews, Semantics
platform System setting(Screen rotation,End etc.) SemanticsService,RouteNotificationMessages etc.
platform_views Platform-specific view operations AndroidView, UiKitView
skia Graphics -
keyEvent Key input RawKeyEvent
textInput Text input TextInput, AndroidView, UiKitView

Sample code

lifecycle

Life cycle listener


SystemChannels.lifecycle.setMessageHandler((message){
  print('<SystemChannels.lifecycle> $message');
  /*
  AppLifecycleState.paused
  AppLifecycleState.inactive
  AppLifecycleState.resumed
  AppLifecycleState.suspending
  AppLifecycleState.detached
   */
  return Future<String>.value();
});

navigation

Navigation operation listener


SystemChannels.navigation.setMethodCallHandler((call) {
  print('<SystemChannels.navigation> ${call.method} (${call.arguments})');
  /*
   popRoute
   pushRoute
   */
  return Future<dynamic>.value();
});

system It can't be used for anything, it's not used, and you can ignore it.

For the time being, set only the callback


SystemChannels.system.setMessageHandler((message) {
  print('<SystemChannels.system> $message');
  return Future<dynamic>.value();
});

accessibility

Text-to-speech sample


SemanticsService.announce('Hello world', TextDirection.ltr)

The Flutter Framework internal implementation of is below.

Reference

Dart


final AnnounceSemanticsEvent event = AnnounceSemanticsEvent('Hello world', TextDirection.ltr);
SystemChannels.accessibility.send(event.toMap());

platform

Exit the app


SystemNavigator.pop()

The Flutter Framework internal implementation of is below.

Exit the app


SystemChannels.platform.invokeMethod('SystemNavigator.pop');

platform_views

It is used in flutter_web, so please refer to it.

Create a view like this


final Map<String, dynamic> args = <String, dynamic>{
  'id': 1,
  'viewType': 'Create WebView',
};
SystemChannels.platform_views.invokeMethod('create', args);

skia

Skia cache size setting. There is no other function, and now I can only do this ...


const maxBytes = 4 * 1024 * 1024;
SystemChannels.skia.invokeMethod('setResourceCacheMaxBytes', maxBytes);

keyEvent

Key input listener


SystemChannels.keyEvent.setMessageHandler((message) {
  print('<SystemChannels.keyEvent> $message');
  return Future<dynamic>.value();
});

textInput

Keyboard display ON/OFF


SystemChannels.textInput.invokeMethod('TextInput.show');
SystemChannels.textInput.invokeMethod('TextInput.hide');

Recommended Posts

About the SystemChannels API to take advantage of Flutter platform-specific features
About the features of Python
I tried to touch the API of ebay
About the camera change event of Google Maps Android API
I want to know the features of Python and pip
Try to extract the features of the sensor data with CNN
About the ease of Python
About the components of Luigi
First python ② Try to write code while examining the features of python
Python beginners hit the unofficial API of Google Play Music to play music
About the return value of pthread_mutex_init ()
About the return value of the histogram.
About the basic type of Go
About the upper limit of threads-max
Master the rich features of IPython
Mastering the rich features of IPython (2)
About the behavior of yield_per of SqlAlchemy
About the size of matplotlib points
About the basics list of Python basics
Supplement to the explanation of vscode
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
I wanted to be careful about the behavior of Python's default arguments
Hit the Rakuten Ranking API to save the ranking of any category in CSV
I tried to get the authentication code of Qiita API with Python.
I tried to get the movie information of TMDb API with Python