Android打开Flutter页面

1. 调起Flutter页面

  • 在Android项目的清单文件AndroidManifest中添加FlutterActivity
 
  • 跳转到FlutterActivity
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(
                        FlutterActivity.createDefaultIntent(MainActivity.this)
                );
            }
        });
    }
}

注意:经过运行发现,点击按钮跳转到Flutter页面的时候启动很慢。

2. 优化启动速度

class MainActivity : AppCompatActivity() {

    private lateinit var flutterEngine: FlutterEngine
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        flutterEngine = initFlutterEngine()
        findViewById

Flutter代码:

void main() => runApp(const MyApp());

Widget getRouter(String name) {
  switch (name) {
    case "main":
      return const MyApp();
    default:
      return MaterialApp(
        title: "Flutter Demo",
        theme: ThemeData(primarySwatch: Colors.blue),
        home: Container(
          alignment: Alignment.center,
          child: Text("not font page ${name}"),
        ),
      );
  }
}

经过运行发现启动变快。

你可能感兴趣的:(Android打开Flutter页面)