flutter android 开发笔记(二.module集成)

flutter android 开发笔记(二.module集成),第1张

概述1,在as已有的安卓项目中,新建flutter项目   选择新建fluttermodule    项目的setting.gradle文件已经自动引入flutter_module2,确认下路径是否正确     安卓原生代码 2,初始化flutter的控制器mFlutter1Engine=newFlutterEngine(this);mFlutte

1,在as已有的安卓项目中,新建Flutter项目

 

 

 选择新建Flutter module

 

 

 

 

项目的setting.gradle文件已经自动引入Flutter_module2,确认下路径是否正确

 

 

 

 

 

安卓原生代码

 

2,初始化Flutter的控制器

 mFlutter1Engine = new FlutterEngine(this); mFlutter1Engine.getDartExecutor().executeDartEntrypoint(         DartExecutor.DartEntrypoint.createDefault() );

  

3,创建FluttervIEw

 FlutterVIEw FlutterVIEw = new FlutterVIEw(this); FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(         VIEwGroup.LayoutParams.MATCH_PARENT,         VIEwGroup.LayoutParams.MATCH_PARENT); FrameLayout flContainer = findVIEwByID(R.ID.fl_Flutter); flContainer.addVIEw(FlutterVIEw, lp); flContainer.setVisibility(VIEw.INVISIBLE); FlutterUidisplayListener Listener = new FlutterUidisplayListener() {     @OverrIDe     public voID onFlutterUidisplayed() {         flContainer.setVisibility(VIEw.VISIBLE);//当Flutter ui展示出来的时候,显示FluttervIEw内容     }     @OverrIDe     public voID onFlutterUiNolongerdisplayed() {     } }; FlutterVIEw.addOnFirstFrameRenderedListener(Listener);

  

4,展示Flutter

 mFlutter1VIEw.attachToFlutterEngine(mFlutter1Engine);

  

整体androID代码

package xyz.djytest.Flutter_test_native_project;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.FrameLayout;import androIDx.appcompat.app.AppCompatActivity;import io.Flutter.embedding.androID.FlutterVIEw;import io.Flutter.embedding.engine.FlutterEngine;import io.Flutter.embedding.engine.FlutterEngineCache;import io.Flutter.embedding.engine.dart.DartExecutor;import io.Flutter.embedding.engine.renderer.FlutterUidisplayListener;public class Flutter2Activity extends AppCompatActivity {    FlutterEngine mFlutter1Engine;    FlutterVIEw   mFlutter1VIEw;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_Flutter2);        initFlutterEngine();        mFlutter1VIEw = createFlutterVIEw();        mFlutter1VIEw.attachToFlutterEngine(mFlutter1Engine);    }    private FlutterVIEw createFlutterVIEw() {        FlutterVIEw FlutterVIEw = new FlutterVIEw(this);        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(                VIEwGroup.LayoutParams.MATCH_PARENT,                VIEwGroup.LayoutParams.MATCH_PARENT);        FrameLayout flContainer = findVIEwByID(R.ID.fl_Flutter);        flContainer.addVIEw(FlutterVIEw, lp);        flContainer.setVisibility(VIEw.INVISIBLE);        FlutterUidisplayListener Listener = new FlutterUidisplayListener() {            @OverrIDe            public voID onFlutterUidisplayed() {                flContainer.setVisibility(VIEw.VISIBLE);            }            @OverrIDe            public voID onFlutterUiNolongerdisplayed() {            }        };        FlutterVIEw.addOnFirstFrameRenderedListener(Listener);        return FlutterVIEw;    }    private voID initFlutterEngine() {        mFlutter1Engine = FlutterEngineCache.getInstance().get("Flutter");        if (mFlutter1Engine == null){            mFlutter1Engine = new FlutterEngine(this);            mFlutter1Engine.getDartExecutor().executeDartEntrypoint(                    DartExecutor.DartEntrypoint.createDefault()            );            FlutterEngineCache.getInstance().put("Flutter", mFlutter1Engine);        }    }}

 xml文件

<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical">    <FrameLayout        androID:ID="@+ID/fl_Flutter"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:orIEntation="vertical">    </FrameLayout>   <linearLayout       androID:layout_gravity="bottom"       androID:layout_marginBottom="20dp"       androID:layout_wIDth="match_parent"       androID:layout_height="wrap_content"       androID:orIEntation="vertical">       <TextVIEw           androID:layout_wIDth="match_parent"           androID:layout_height="wrap_content"           androID:text="我是Flutter2Activity的native的text" />       <button           androID:ID="@+ID/button2"           androID:layout_wIDth="wrap_content"           androID:text="修改text内容"           androID:layout_height="wrap_content"/>   </linearLayout></FrameLayout>

  

Flutter代码

Flutter页面入口

 

 

 

默认页面内容

import 'package:Flutter/material.dart';voID main() => runApp(MyApp());class MyApp extends StatelessWidget {  // This Widget is the root of your application.  @overrIDe  Widget build(BuildContext context) {    return MaterialApp(      Title: 'Flutter Demo',      theme: themeData(        // This is the theme of your application.        //        // Try running your application with "Flutter run". You'll see the        // application has a blue toolbar. Then, without quitting the app, try        // changing the primarySwatch below to colors.green and then invoke        // "hot reload" (press "r" in the console where you ran "Flutter run",        // or press Run > Flutter Hot Reload in a Flutter IDE). Notice that the        // counter dIDn't reset back to zero; the application is not restarted.        primarySwatch: colors.blue,      ),      home: MyHomePage(Title: 'Flutter Demo Home Page'),    );  }}class MyHomePage extends StatefulWidget {  MyHomePage({Key key, this.Title}) : super(key: key);  // This Widget is the home page of your application. It is stateful, meaning  // that it has a State object (defined below) that contains fIElds that affect  // how it looks.  // This class is the configuration for the state. It holds the values (in this  // case the Title) provIDed by the parent (in this case the App Widget) and  // used by the build method of the State. FIElds in a Widget subclass are  // always marked "final".  final String Title;  @overrIDe  _MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State<MyHomePage> {  int _counter = 0;  voID _incrementCounter() {    setState(() {      // This call to setState tells the Flutter framework that something has      // changed in this State, which causes it to rerun the build method below      // so that the display can reflect the updated values. If we changed      // _counter without calling setState(), then the build method would not be      // called again, and so nothing would appear to happen.      _counter++;    });  }  @overrIDe  Widget build(BuildContext context) {    // This method is rerun every time setState is called, for instance as done    // by the _incrementCounter method above.    //    // The Flutter framework has been optimized to make rerunning build methods    // fast, so that you can just rebuild anything that needs updating rather    // than having to indivIDually change instances of Widgets.    return Scaffold(      appbar: Appbar(        // Here we take the value from the MyHomePage object that was created by        // the App.build method, and use it to set our appbar Title.        Title: Text(Widget.Title),      ),      body: Center(        // Center is a layout Widget. It takes a single child and positions it        // in the mIDdle of the parent.        child: Column(          // Column is also a layout Widget. It takes a List of children and          // arranges them vertically. By default, it sizes itself to fit its          // children horizontally, and trIEs to be as tall as its parent.          //          // Invoke "deBUG painting" (press "p" in the console, choose the          // "Toggle DeBUG Paint" action from the Flutter Inspector in AndroID          // Studio, or the "Toggle DeBUG Paint" command in Visual Studio Code)          // to see the wireframe for each Widget.          //          // Column has varIoUs propertIEs to control how it sizes itself and          // how it positions its children. Here we use mainAxisAlignment to          // center the children vertically; the main axis here is the vertical          // axis because Columns are vertical (the cross axis would be          // horizontal).          mainAxisAlignment: MainAxisAlignment.center,          children: <Widget>[            Text(              'You have pushed the button this many times:',            ),            Text(              '$_counter',              style: theme.of(context).texttheme.display1,            ),          ],        ),      ),      floatingActionbutton: floatingActionbutton(        onpressed: _incrementCounter,        tooltip: 'Increment',        child: Icon(Icons.add),      ), // This trailing comma makes auto-formatting nicer for build methods.    );  }}

  

 

打开Flutter2Activity时的展示效果

 

总结

以上是内存溢出为你收集整理的flutter android 开发笔记(二.module集成)全部内容,希望文章能够帮你解决flutter android 开发笔记(二.module集成)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1061402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存