Flutter开发之——IOS启动Flutter

一 概述

本文介绍从原生iOS启动Flutter的几种方式

  • 直接启动(不配置缓存 FlutterEngine和路由)Flutter
  • 使用缓存FlutterEngine启动Flutter
  • 使用路由方式启动Flutter

二 直接启动(不配置缓存 FlutterEngine和路由)Flutter

2.1 代码(ViewController)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import UIKit
import Flutter

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func jumpFlutter(_ sender: Any)
{
let flutterViewController = FlutterViewController.init()
present(flutterViewController, animated: true, completion: nil)
}
}

2.2 效果图

三 使用缓存FlutterEngine启动Flutter

3.1 代码

AppDelegate.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
import UIKit
import Flutter

@main
class AppDelegate:FlutterAppDelegate {

lazy var flutterEngine=FlutterEngine(name: "my flutter engine")

override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
flutterEngine.run();
return super.application(application, didFinishLaunchingWithOptions: launchOptions);
}
}

ViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import UIKit
import Flutter

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

@IBAction func jumpFlutter(_ sender: Any)
{
let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine;
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
present(flutterViewController, animated: true, completion: nil)
}
}

3.2 效果图

四 使用路由方式启动Flutter

4.1 代码ViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import UIKit
import Flutter

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

@IBAction func jumpFlutter(_ sender: Any)
{
let flutterEngine = FlutterEngine()
flutterEngine.run(withEntrypoint: nil, initialRoute: "/a")
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
present(flutterViewController, animated: true, completion: nil)
}
}

4.2 lib/main.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
routes: <String, WidgetBuilder>{
//"/": (context) => MyHomePage(title: 'Flutter Demo Home Page'),
'/a': (context) => MyPage(title: 'Page A'),
'/b': (context) => MyPage(title: 'Page B')
},
//home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyPage extends StatelessWidget {
var title;

MyPage({Key? key, required this.title}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Text(title),
));
}
}

4.3 效果图