Flutter Application

Flutter Application

  1. Manual integration

flutter create myApp

if wanting to specify the developing language

flutter create -i swift -a kotlin xxapp

  1. Created by Android studio

    1. Click Create New Project from the Welcome window or File > New > Project from the main IDE window.
    2. Select Flutter in the menu, and click Next.
    3. Enter your desired Project name and Project location.
    4. Click Finish.
  2. The structure of application

capture_application.PNG
Folder function
android Android platform related code
ios ios platform related code
lib The flutter code, the main code we wrote, was in this folder
test The file for test code
pubspec.yaml Configuration files for some third-party dependencies

</br></br>

Flutter Module

  1. Manual integration

flutter create -t module --org com.example my_flutter

  1. Created by Android studio

Using the File > New > New Module… menu in Android Studio in your existing Android project, you can either create a new Flutter module to integrate, or select an existing Flutter module that was created previously.

capture_create_step_one.png

If you create a new module, you can use a wizard to select the module name, location, and so on.

[图片上传失败...(image-954fc0-1621912793341)]


capture_create_step_two.png

The Android Studio plugin automatically configures your Android project to add your Flutter module as a dependency, and your app is ready to build.

  1. The structure of project

[图片上传失败...(image-67093c-1621912793341)]


capture_module.PNG

</br></br>

Flutter Package

flutter create --template=package xxapp_package

  1. The structure of project

[图片上传失败...(image-b27285-1621912793341)]


capture_package.PNG
  1. publish package

please refer to:

Publishing packages

</br></br>

Flutter Plugin

flutter create --template=plugin xxapp_plugin

  1. The structure of project

The whole project includes four main directories, and the corresponding Native codes are in the android and ios directories. Lib is the Flutter code of the plugin. Example is a complete Flutter App.


capture_plugin.PNG
  1. android platform code

Before you add the android code, you need to make sure that the plug-in code is built once through the example project.

//first executing
cd flutter_plugin_demo/example/

//then executing
flutter build apk

After building the Android apk package in the example project, you can open the Android project in the example project through Android Studio. Then you can edit the class file to add plug-in functionality. In the android directory, the IDE will generate an xxxplugin.java file for you. When you open it, you can see the following sample code:


Capture_plugin_zero.PNG

There is a class FlutterPlugin that implements MethodCallHandler and a static function registerWith(which is retained to support for the old flutter version). In this static function, a new MethodChannel is created and an instance of FlutterPlugin is set to the MehodChannel. In other words, all the methodchannels and eventchannels in your plug-in are registered to the Host App through this function. the new API MethodCallHandler will be initialized and built in the onAttachedToEngine method and released in the onDetachedFromEngine method. In this way, the corresponding channel can be found when Flutter is called. we just need to rewrite the function onMethodCall.

  1. lib direcotry

IDE in the lib directory will automatically generate flutter_plugin.dart file for you. This is where the Flutter code of the plug-in is located. which is the packaging of Platform channels defined.

Capture_plugin_lib.PNG
  1. Example App
Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await MyFlutterPlugin.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  1. Release

Running the following command to see if the plug-in can be published

flutter packages pub publish --dry-run

If there is a problem, it will output the error information in the terminal, and you need to modify it until it returns successfully. You can refer to the official documentation for specific problems.

At last, running the following command to publish the plug-in.

flutter packages pub publish

  1. The plug-in registration

The registration of the plug-in is done automatically and doesn't need to register manually.

In MainActivity of example app:

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // plug-in is registered here.
    GeneratedPluginRegistrant.registerWith(this);
  }
}

GeneratedPluginRegistrant.class:

public final class GeneratedPluginRegistrant {
  public static void registerWith(@NonNull FlutterEngine flutterEngine) {
    flutterEngine.getPlugins().add(new com.example.my_flutter_plugin.MyFlutterPlugin());
  }
}
  1. Conclusion

    1. Be sure to use the example project to build the platform code once before adding the plug-in code, so that the plug-in files are displayed correctly for editing in the example project.

    2. If the plug-in functions need to rely on the third-party plug-in, then the third-party plug-in can also be added through pubspec.yaml in the plug-in project.

</br></br>

Multiple module support or workaround

  1. Import multiple Flutter modules in a native app is not support now.
    cause: It seems that Flutter doesn't support this use-case because FlutterEngine can only load a single specific set of AOT snapshots in an application.

  2. workaround

</br></br>

ARM/X86 CPU mode support

Flutter currently only supports building ahead-of-time (AOT) compiled libraries for x86_64, armeabi-v7a and arm64-v8a.

The Flutter engine has an x86 and x86_64 version. When using an emulator in debug Just-In-Time (JIT) mode, the Flutter module still runs correctly.

refer to: https://flutter.dev/docs/development/add-to-app/android/project-setup

</br></br>

Certificate pinning verification

ssl_pinning_plugin

refer to: https://pub.dev/packages/ssl_pinning_plugin

A plugin for check SSL Pinning on request HTTP. Checks the equality between the known SHA-1 or SHA-256 fingerprint and the SHA-1 or SHA-256 of the target server.

httpclient

 HttpClient httpClient = new HttpClient()
  ..badCertificateCallback =
  ((X509Certificate cert, String host, int port) {
    // tests that cert is self signed, correct subject and correct date(s) 
    return (cert.issuer == cert.subject &&
        cert.subject == 'MySelfSignedCertCN' &&
        cert.endValidity.millisecondsSinceEpoch == 1234567890);
  });

IOClient ioClient = new IOClient(httpClient);
// use ioClient to perform get/post operations from package:http

// don't forget to call ioClient.close() when done
// note, this also closes the underlying HttpClient

http

import 'package:http/http.dart' as http;

http.Client sslClient() {
  var ioClient = new HttpClient()
    ..badCertificateCallback = (X509Certificate cert, String host, int port) => true;
  http.Client _client = IOClient(ioClient);

  return _client;
}
//get 
sslClient().get(url) 
//post
sslClient().post(url) 

Dio

There are two ways to verify the https certificate. Suppose the certificate format is PEM, the code like:

String PEM="XXXXX"; // certificate content
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate  = (client) {
    client.badCertificateCallback=(X509Certificate cert, String host, int port){
        if(cert.pem==PEM){ // Verify the certificate
            return true;
        }
        return false;
    };
};

Another way is creating a SecurityContext when create the HttpClient:

(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate  = (client) {
    SecurityContext sc = new SecurityContext();
    //file is the path of certificate
    sc.setTrustedCertificates(file);
    HttpClient httpClient = new HttpClient(context: sc);
    return httpClient;
};

In this way, the format of certificate must be PEM or PKCS12.

</br></br>

SQLite integration

sqflite

SQLite plugin for Flutter. Supports iOS, Android and MacOS.

</br></br>

Firebase Analytics function

the official document

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容