欢迎您光临深圳塔灯网络科技有限公司!
电话图标 余先生:13699882642

网站百科

为您解码网站建设的点点滴滴

Android工程集成flutter

发表日期:2018-11 文章编辑:小灯 浏览次数:2473

Android工程集成flutter

官方方案

https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps

第一个步:
新建一个标准的APP工程testFlutterInAPP,APP目录下面通过命令创建flutter模块工程

flutter create -t module my_flutter

image.png

编译flutter 代码生成Android的AAR文件

$ cd .android/ $ ./gradlew flutter:assembleDebug 

AAR 文件生成到.android/Flutter/build/outputs/aar/目录下面

添加Host APP 依赖flutter module 模块,在setting.gradle 文件中加入

include ':app' // assumed existing content setBinding(new Binding([gradle: this])) // new evaluate(new File(// new settingsDir.parentFile, // new 'my_flutter/.android/include_flutter.groovy'// new ))

app/build.gradle 增加对flutter 工程依赖

// testFlutterInAPP/app/build.gradledependencies { implementation project(':flutter') } 

构建成功后,执行Android的run启动应用 ,点击打开flutter 。


image.png

Java 层代码

 openFlutter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { View flutterView = Flutter.createView( MainActivity.this, getLifecycle(), "route1" ); FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600, 800); layout.leftMargin = 100; layout.topMargin = 200; addContentView(flutterView, layout); } }); 

flutter 代码

import 'dart:ui';import 'package:flutter/material.dart';void main() => runApp(_widgetForRoute(window.defaultRouteName)); Widget _widgetForRoute(String route) { switch (route) { case 'route1': return new MyApp(); case 'route2': return new MyApp(); default: return Center( child: Text('Unknownroute1 : $route', textDirection: TextDirection.ltr), ); } return new MyApp(); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Demo', theme: new ThemeData( primarySwatch: Colors.blue, ), home: new MyHomePage(title: 'Flutter Demo 1111' 'Home Page'), ); } }class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key);final String title;@override _MyHomePageState createState() => new _MyHomePageState(); }class _MyHomePageState extends State<MyHomePage> { int _counter = 0;void _incrementCounter() { setState(() {_counter++; }); }@override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar(title: new Text(widget.title), ), body: new Center( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ new Text( 'You have pushed the button this many times:', ), new Text( '$_counter', style: Theme.of(context).textTheme.display1, ), ], ), ), floatingActionButton: new FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: new Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); } } 

运行 flutter 工程 ,进入my_flutter 目录执行flutter run 命令

image.png

即可启动flutter ,r == hot reload R == hot restart
q == quit d == detach,

或者启动Android应用调试

image.png

但是,好像DEBUG无效??

闲鱼方案

连接 https://zhuanlan.zhihu.com/p/40528502


本页内容由塔灯网络科技有限公司通过网络收集编辑所得,所有资料仅供用户学习参考,本站不拥有所有权,如您认为本网页中由涉嫌抄袭的内容,请及时与我们联系,并提供相关证据,工作人员会在5工作日内联系您,一经查实,本站立刻删除侵权内容。本文链接:http://dengtar.com/18082.html
相关APP开发
 八年  行业经验

多一份参考,总有益处

联系深圳网站公司塔灯网络,免费获得网站建设方案及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:余经理:13699882642

Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.