四、Activity 中启动 FlutterActivity 页面
将 Flutter 页面当做一个新的 Activity 启动 ;
findViewById(R.id.flutter2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = FlutterActivity .withNewEngine() .initialRoute("Android 启动 FlutterActivity") .build(MainActivity.this); //intent.putExtra("initParams", "Android 中 Activity 启动 Flutter"); startActivity(intent); } });
执行结果 : 点击 方式二 按钮 , 弹出 FlutterActivity ;
五、完整代码示例
这里只贴出主界面完整代码 , 具体的配置参数 , 查看 GitHub 或者 CSDN 源码快照 ;
1、Android 主界面代码示例
package com.example.flutter_native; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.view.View; import io.flutter.embedding.android.FlutterActivity; import io.flutter.embedding.android.FlutterFragment; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.flutter1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); // 创建 FlutterFragment fragmentTransaction.replace(R.id.frame, FlutterFragment.createDefault()); fragmentTransaction.commit(); } }); findViewById(R.id.flutter2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = FlutterActivity .withNewEngine() .initialRoute("Android 启动 FlutterActivity") .build(MainActivity.this); //intent.putExtra("initParams", "Android 中 Activity 启动 Flutter"); startActivity(intent); } }); } }
2、Flutter 完整代码示例
import 'package:flutter/material.dart'; import 'dart:ui'; void main() => runApp( /// 该构造方法中传入从 Android 中传递来的参数 MyApp(initParams: window.defaultRouteName,) ); class MyApp extends StatelessWidget { /// 这是从 Android 中传递来的参数 final String initParams; /// 构造方法 , 获取从 Android 中传递来的参数 const MyApp({Key? key, required this.initParams}):super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: initParams), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required 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.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); } }