美文网首页
Android打开Flutter页面

Android打开Flutter页面

作者: 谢尔顿 | 来源:发表于2022-09-01 13:13 被阅读0次

    1. 调起Flutter页面

    • 在Android项目的清单文件AndroidManifest中添加FlutterActivity
     <activity
                android:name="io.flutter.embedding.android.FlutterActivity"
                android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
                android:hardwareAccelerated="true"
                android:windowSoftInputMode="adjustResize" />
    
    • 跳转到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<Button>(R.id.btn).setOnClickListener {
                startActivity(
                    FlutterActivity.withCachedEngine("default_engine_id").build(this)
                )
            }
        }
    
        private fun initFlutterEngine(): FlutterEngine {
            //创建Flutter引擎
            val flutterEngine = FlutterEngine(this)
            //指定要跳转的flutter页面
            flutterEngine.navigationChannel.setInitialRoute("main")
            flutterEngine.dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
            //这里做一个缓存,可以在适当的时候执行它,例如app里,在跳转前执行预加载
            val flutterEngineCache = FlutterEngineCache.getInstance()
            flutterEngineCache.put("default_engine_id", flutterEngine)
            return flutterEngine
        }
    
        override fun onDestroy() {
            super.onDestroy()
            flutterEngine.destroy()
        }
    }
    

    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页面

          本文链接:https://www.haomeiwen.com/subject/lstsnrtx.html