美文网首页
原生应用集成flutter

原生应用集成flutter

作者: 星空梦想 | 来源:发表于2020-12-29 17:33 被阅读0次

    一创建Flutter model

    原生项目目录: xxxx/flutter_hybrid/Native项目:

       cd xxxx/flutter_hybrid/

      flutter create -t module flutter_module

    生成flutter_module的宿主工程

    .android. — flutter_module的android宿主工程

    .ios —flutter_module的iOS宿主工程

    lib —flutter_module的Dart部分代码

    二ios 工程配置

    1.pod文件添加代码

    flutter_application_path = ‘../flutter_module/’

    Eval(File.read(File.join(flutter_aoolication_path,’.ios’,’flutter’,’pod helper.rb’)),binding)

    2.pod install

    3.关闭 bitcode

    4.添加build phase 构建Dart代码

       选中项目,点击 New Run Script Phase

      “$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh” buiObject-cld

    “$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh” embed

    5.OC中调用Flutter module

    A:直接使用FlutterViewController的方式

    #import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>

    FlutterViewzcontroller * flutter = [FlutterViewzcontroller new];

    GeneratedPluginRegistrant.register(with:flutter);

    [flutter setInitialRoute:@‘route1’];

    [self presentViewController:flutter animated:true completion:nil];

    B:AppDelegate继承FlutterAppDelegate

    self.flutterEngine = [[FlutterEngine alloc] initWithName:’io.flutter’ project:nil];

    [self.flutterEngine runWithEntrypoint:nil];

    [generatedPluginRegistrant registerWithRegistry:self.flutterEngine];

    Return [super application:application didFinishLaunchingWithOptions:launchOptions];

    三安卓工程配置

    1.settings.gradle添加如下代码

    setBinding(new Binding([gradle: this]))

    evaluate(new File(settingsDir.parentFile,’flutter_module/.android/include_flutter.groovy’))

    2.app中添加flutter的依赖

    dependencies{implementation project(‘:flutter’)}

    3.java中调用Flutter module

    方式有两种:a:使用Flutter.createView API的方式

                            b:使用FlutterFragment的方式

    eg: public void onclick(View view){

       View flutterView = Flutter.createView(

          MainActivity.this,

          getLifecycle(),

          ‘route1’

          );

       FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600,600);

       Layout.leftMargin = 100;

       Layout.topMargin = 200;

       addContentView(flutterView,layout);

    }

    }

    eg: public void onclick(View view){

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();

        tx.replace(R.id.somecontainer,Flutter.createFragment(‘route1’));

        tx.commit();

      }

    }

    四传递参数

      tx.replace(R.id.somecontainer,Flutter.createFragment(‘{name:’devio’,dataList:[‘aa’,’bb’,’cc’]}’));

    Dart中:String initParams = window.defaultRouteName;

    相关文章

      网友评论

          本文标题:原生应用集成flutter

          本文链接:https://www.haomeiwen.com/subject/hmcqoktx.html