flutter 原生跳轉(zhuǎn)到裝有flutterView的原生activity

FlutterView 是用來裝載flutter widget的載體,可以在原生上面直接添加 ,有人說會(huì)有黑屏,很奇怪,我這邊沒有碰到哦

首先項(xiàng)目是直接新建的flutter 項(xiàng)目,單獨(dú)打開android項(xiàng)目,項(xiàng)目視圖是這樣的


項(xiàng)目視圖.jpg

項(xiàng)目的build.gradle上面注意要引入flutter

implementation project(':flutter')
項(xiàng)目視圖2.jpg

這個(gè)時(shí)候原生項(xiàng)目就可以使用 flutterView了,就不會(huì)報(bào)警告了

項(xiàng)目就簡單了建立了2個(gè)Activity,如下圖


項(xiàng)目視圖3.jpg

接下來就直接貼代碼了,很簡單
MainActivity.class

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button open = findViewById(R.id.openBtn);
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyFlutterActivity.class);
                startActivity(intent);
            }
        });

    }
}

activity_main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/openBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="打開flutter頁面" />


</RelativeLayout>

第二個(gè)Activity MyFlutterActivity.class

public class MyFlutterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flutter);

        final FlutterView flutterView = Flutter.createView(
                this,
                getLifecycle(),
                "route1"
        );


        final FrameLayout layout = findViewById(R.id.flutter_container);
        layout.addView(flutterView);

        final FlutterView.FirstFrameListener[] listeners = new FlutterView.FirstFrameListener[1];
        listeners[0] = new FlutterView.FirstFrameListener() {
            @Override
            public void onFirstFrame() {
                layout.setVisibility(View.VISIBLE);
            }
        };
        flutterView.addFirstFrameListener(listeners[0]);
    }
}

布局代碼activity_flutter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:visibility="visible">

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="原生text"/>

    <FrameLayout android:id="@+id/flutter_container"
            android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout>

</LinearLayout>

記得要去manifest里面去注冊activity

接下來就是flutter了,先看一下項(xiàng)目結(jié)構(gòu)


flutter結(jié)構(gòu).jpg

然后就是代碼了 main.dart

import 'dart:ui';

import 'package:flutter/material.dart';

void main() => runApp(
    _widgetForRoute(window.defaultRouteName)
);

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

Widget _widgetForRoute(String route) {
return MyApp();
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          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.
    );
  }
}

代碼就這樣結(jié)束了,最后跑起來看下效果圖


效果1.jpg

效果2.jpg

親測沒有黑屏,如果有問題,請留言

native 和 flutter 的混合界面如下


原生和flutter view.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容