README_CN.md 8.81 KB
Newer Older
Jidong Chen's avatar
init  
Jidong Chen committed
1 2 3 4
<p align="center">
  <img src="flutter_boost.png">
</p>

5

yangwu.jia's avatar
yangwu.jia committed
6 7 8
# Release Note

 请查看最新版本0.1.50的release note 确认变更,[0.1.50 release note](https://github.com/alibaba/flutter_boost/releases)
9

Jidong Chen's avatar
init  
Jidong Chen committed
10 11 12 13 14 15
# FlutterBoost

新一代Flutter-Native混合解决方案。 FlutterBoost是一个Flutter插件,它可以轻松地为现有原生应用程序提供Flutter混合集成方案。FlutterBoost的理念是将Flutter像Webview那样来使用。在现有应用程序中同时管理Native页面和Flutter页面并非易事。 FlutterBoost帮你处理页面的映射和跳转,你只需关心页面的名字和参数即可(通常可以是URL)。


# 前置条件
yangwu.jia's avatar
yangwu.jia committed
16
在继续之前,您需要将Flutter集成到你现有的项目中。flutter sdk 的版本需要 v1.5.4-hotfixes,否则会编译失败.
Jidong Chen's avatar
init  
Jidong Chen committed
17 18 19 20 21 22 23

# 安装

## 在Flutter项目中添加依赖项。

打开pubspec.yaml并将以下行添加到依赖项:

Jidong Chen's avatar
Jidong Chen committed
24
```json
yangwu.jia's avatar
yangwu.jia committed
25
flutter_boost: ^0.1.53
Jidong Chen's avatar
init  
Jidong Chen committed
26 27
```

Jidong Chen's avatar
Jidong Chen committed
28
或者可以直接依赖github的项目的版本,Tag,pub发布会有延迟,推荐直接依赖Github项目
yangwu.jia's avatar
yangwu.jia committed
29

Jidong Chen's avatar
Jidong Chen committed
30
```java
yangwu.jia's avatar
yangwu.jia committed
31

Jidong Chen's avatar
Jidong Chen committed
32 33 34
flutter_boost:
        git:
            url: 'https://github.com/alibaba/flutter_boost.git'
yangwu.jia's avatar
yangwu.jia committed
35
            ref: '0.1.53'
yangwu.jia's avatar
yangwu.jia committed
36
            
Jidong Chen's avatar
Jidong Chen committed
37
```
Jidong Chen's avatar
init  
Jidong Chen committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
## Dart代码的集成
将init代码添加到App App

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();

    ///register page widget builders,the key is pageName
    FlutterBoost.singleton.registerPageBuilders({
      'sample://firstPage': (pageName, params, _) => FirstRouteWidget(),
      'sample://secondPage': (pageName, params, _) => SecondRouteWidget(),
    });

  }

  @override
  Widget build(BuildContext context) => MaterialApp(
      title: 'Flutter Boost example',
      builder: FlutterBoost.init(), ///init container manager
      home: Container());
}
```

## iOS代码集成。

Jidong Chen's avatar
Jidong Chen committed
72 73
注意:需要将libc++ 加入 "Linked Frameworks and Libraries" 中。

Jidong Chen's avatar
init  
Jidong Chen committed
74 75 76 77 78 79 80 81 82 83 84
使用FLBFlutterAppDelegate作为AppDelegate的超类

```objectivec
@interface AppDelegate : FLBFlutterAppDelegate <UIApplicationDelegate>
@end
```


为您的应用程序实现FLBPlatform协议方法。

```objectivec
余玠's avatar
余玠 committed
85
@interface PlatformRouterImp : NSObject<FLBPlatform>
Jidong Chen's avatar
init  
Jidong Chen committed
86 87 88

@property (nonatomic,strong) UINavigationController *navigationController;

余玠's avatar
余玠 committed
89
+ (PlatformRouterImp *)sharedRouter;
Jidong Chen's avatar
init  
Jidong Chen committed
90 91 92 93

@end


余玠's avatar
余玠 committed
94
@implementation PlatformRouterImp
Jidong Chen's avatar
init  
Jidong Chen committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

- (void)openPage:(NSString *)name
          params:(NSDictionary *)params
        animated:(BOOL)animated
      completion:(void (^)(BOOL))completion
{
    if([params[@"present"] boolValue]){
        FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
        [vc setName:name params:params];
        [self.navigationController presentViewController:vc animated:animated completion:^{}];
    }else{
        FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
        [vc setName:name params:params];
        [self.navigationController pushViewController:vc animated:animated];
    }
}


- (void)closePage:(NSString *)uid animated:(BOOL)animated params:(NSDictionary *)params completion:(void (^)(BOOL))completion
{
    FLBFlutterViewContainer *vc = (id)self.navigationController.presentedViewController;
    if([vc isKindOfClass:FLBFlutterViewContainer.class] && [vc.uniqueIDString isEqual: uid]){
        [vc dismissViewControllerAnimated:animated completion:^{}];
    }else{
        [self.navigationController popViewControllerAnimated:animated];
    }
}

@end
```



在应用程序开头使用FLBPlatform初始化FlutterBoost。

余玠's avatar
余玠 committed
130 131
```objc
 PlatformRouterImp *router = [PlatformRouterImp new];
Jidong Chen's avatar
init  
Jidong Chen committed
132
 [FlutterBoostPlugin.sharedInstance startFlutterWithPlatformrouter
余玠's avatar
余玠 committed
133
                                                        onStart^FlutterEngine *engine{
Jidong Chen's avatar
init  
Jidong Chen committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                                                            
                                                        }];
```

## Android代码集成。

在Application.onCreate()中初始化FlutterBoost

```java
public class MyApplication extends FlutterApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterBoostPlugin.init(new IPlatform() {
            @Override
            public Application getApplication() {
                return MyApplication.this;
            }

            @Override
yangwu.jia's avatar
yangwu.jia committed
154 155
            public boolean isDebug() {
                return true;
Jidong Chen's avatar
init  
Jidong Chen committed
156 157 158
            }

            @Override
yangwu.jia's avatar
yangwu.jia committed
159 160
            public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) {
                PageRouter.openPageByUrl(context,url,urlParams,requestCode);
Jidong Chen's avatar
init  
Jidong Chen committed
161 162 163
            }

            @Override
yangwu.jia's avatar
yangwu.jia committed
164 165 166 167 168 169 170 171 172 173
            public IFlutterEngineProvider engineProvider() {
                return new BoostEngineProvider(){
                    @Override
                    public BoostFlutterEngine createEngine(Context context) {
                        return new BoostFlutterEngine(context, new DartExecutor.DartEntrypoint(
                                context.getResources().getAssets(),
                                FlutterMain.findAppBundlePath(context),
                                "main"),"/");
                    }
                };
Jidong Chen's avatar
init  
Jidong Chen committed
174 175 176
            }

            @Override
yangwu.jia's avatar
yangwu.jia committed
177 178
            public int whenEngineStart() {
                return ANY_ACTIVITY_CREATED;
Jidong Chen's avatar
init  
Jidong Chen committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
            }
        });
    }
```

# 基本用法
## 概念

所有页面路由请求都将发送到Native路由器。Native路由器与Native Container Manager通信,Native Container Manager负责构建和销毁Native Containers。

## 使用Flutter Boost Native Container用Native代码打开Flutter页面。

```objc
 FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
        [vc setName:name params:params];
        [self.navigationController presentViewController:vc animated:animated completion:^{}];
```

余玠's avatar
余玠 committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
但是,这种方式无法获取页面返回的数据,建议你按上面的example实现类似于PlatformRouterImp这样的路由器,然后通过以下方式来打开/关闭页面

```objc
//push the page
[FlutterBoostPlugin open:@"first" urlParams:@{kPageCallBackId:@"MycallbackId#1"} exts:@{@"animated":@(YES)} onPageFinished:^(NSDictionary *result) {
        NSLog(@"call me when page finished, and your result is:%@", result);
    } completion:^(BOOL f) {
        NSLog(@"page is opened");
    }];
//prsent the page
[FlutterBoostPlugin open:@"second" urlParams:@{@"present":@(YES),kPageCallBackId:@"MycallbackId#2"} exts:@{@"animated":@(YES)} onPageFinished:^(NSDictionary *result) {
        NSLog(@"call me when page finished, and your result is:%@", result);
    } completion:^(BOOL f) {
        NSLog(@"page is presented");
    }];
//close the page
[FlutterBoostPlugin close:yourUniqueId result:yourdata exts:exts completion:nil];
```
Jidong Chen's avatar
init  
Jidong Chen committed
215 216 217 218 219 220 221
Android

```java
public class FlutterPageActivity extends BoostFlutterActivity {


    @Override
yangwu.jia's avatar
yangwu.jia committed
222
    public String getContainerUrl() {
Jidong Chen's avatar
init  
Jidong Chen committed
223 224 225 226 227
        //specify the page name register in FlutterBoost
        return "sample://firstPage";
    }

    @Override
yangwu.jia's avatar
yangwu.jia committed
228
    public Map getContainerUrlParams() {
Jidong Chen's avatar
init  
Jidong Chen committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242
        //params of the page
        Map<String,String> params = new HashMap<>();
        params.put("key","value");
        return params;
    }
}
```

或者用Fragment

```java
public class FlutterFragment extends BoostFlutterFragment {

    @Override
yangwu.jia's avatar
yangwu.jia committed
243
    public String getContainerUrl() {
Jidong Chen's avatar
init  
Jidong Chen committed
244 245 246 247
        return "sample://firstPage";
    }

    @Override
yangwu.jia's avatar
yangwu.jia committed
248
    public Map getContainerUrlParams() {
Jidong Chen's avatar
init  
Jidong Chen committed
249 250 251 252 253 254 255 256 257 258 259 260
        Map<String,String> params = new HashMap<>();
        params.put("key","value");
        return params;
    }
}
```


## 使用Flutter Boost在dart代码打开页面。
Dart

```java
yangwu.jia's avatar
yangwu.jia committed
261 262 263 264

 FlutterBoost.singleton
                .open("sample://flutterFragmentPage")

Jidong Chen's avatar
init  
Jidong Chen committed
265 266 267 268 269 270
```


## 使用Flutter Boost在dart代码关闭页面。

```java
yangwu.jia's avatar
yangwu.jia committed
271
 FlutterBoost.singleton.close(uniqueId);
Jidong Chen's avatar
init  
Jidong Chen committed
272 273 274 275 276 277 278 279
```

# Examples
更详细的使用例子请参考Demo

# 许可证
该项目根据MIT许可证授权 - 有关详细信息,请参阅[LICENSE.md](LICENSE.md)文件
<a name="Acknowledgments"> </a>
tino.wjf's avatar
tino.wjf committed
280

yangwu.jia's avatar
yangwu.jia committed
281 282 283 284 285
# 问题反馈群(钉钉群)

<img width="200" src="https://img.alicdn.com/tfs/TB1JSzVeYY1gK0jSZTEXXXDQVXa-892-1213.jpg">


tino.wjf's avatar
tino.wjf committed
286 287 288 289 290 291 292 293 294 295 296 297 298
## 关于我们
阿里巴巴-闲鱼技术是国内最早也是最大规模线上运行Flutter的团队。

我们在公众号中为你精选了Flutter独家干货,全面而深入。

内容包括:Flutter的接入、规模化应用、引擎探秘、工程体系、创新技术等教程和开源信息。

**架构/服务端/客户端/前端/算法/质量工程师 在公众号中投递简历,名额不限哦**

欢迎来闲鱼做一个好奇、幸福、有影响力的程序员,简历投递:tino.wjf@alibaba-inc.com

订阅地址

tino.wjf's avatar
tino.wjf committed
299
<img src="https://img.alicdn.com/tfs/TB17Ki5XubviK0jSZFNXXaApXXa-656-656.png" width="328px" height="328px">
tino.wjf's avatar
tino.wjf committed
300 301

[For English](https://twitter.com/xianyutech "For English")