FlutterView 是用來裝載flutter widget的載體英染,可以在原生上面直接添加 揽惹,有人說會(huì)有黑屏,很奇怪四康,我這邊沒有碰到哦
首先項(xiàng)目是直接新建的flutter 項(xiàng)目搪搏,單獨(dú)打開android項(xiàng)目疯溺,項(xiàng)目視圖是這樣的
項(xiàng)目的build.gradle上面注意要引入flutter
implementation project(':flutter')
這個(gè)時(shí)候原生項(xiàng)目就可以使用 flutterView了,就不會(huì)報(bào)警告了
項(xiàng)目就簡(jiǎn)單了建立了2個(gè)Activity,如下圖
接下來就直接貼代碼了墨闲,很簡(jiǎn)單
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頁(yè)面" />
</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里面去注冊(cè)activity
接下來就是flutter了,先看一下項(xiàng)目結(jié)構(gòu)
然后就是代碼了 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é)束了,最后跑起來看下效果圖
親測(cè)沒有黑屏琐脏,如果有問題日裙,請(qǐng)留言
native 和 flutter 的混合界面如下