Faraday 中文文档
Flutter plugin for integrate flutter to existing app
-
ios
、android
、flutter
Native route apis - Hybrid stack(native -> flutter -> native)
- Initial Page Support
- ChildViewController、Fragment Support
- Alert Page Support
- CallBack
- iOS NavigationBar Auto hide/show
- WillPopScope Handle
onBackPress
- Global Notification
- Custom Page Transition
- Unit Test
- Complete Documentation(7/10)
- avoid dependency platform implementation
- avoid modify existing code
- follow platform api design guidance
- Flutter 2.5.0 • channel stable
- iOS 10.0+ Xcode 12.0+ Swift 5.1+
- Android minSdkVersion 16 Kotlin 1.4.10+
g_faraday | flutter | cocoapods | remark |
---|---|---|---|
^0.7.2 | Flutter 2.5.0 • channel stable • https://github.com/flutter/flutter.git |
any | recommend |
^0.7.0 | Flutter 2.0.0 • channel stable | any | not recommend |
^0.5.1-nullsafety.0 | Flutter 1.24.0-10.2.pre • channel beta | any | not recommend |
^0.5.0-nullsafety.0 | Flutter 1.24.0-10.2.pre • channel beta | any | not recommend |
^4.0.0 | Flutter 1.24.0-10.2.pre • channel beta | any | not recommend |
dependencies:
# please confirm flutter channel >> supported: flutter channel beta, 1.25.0-8.1.pre
g_faraday: ^0.5.1-nullsafety.0
Register route which will be open from native.
// 0x00 define route
final route = faraday.wrapper((settings) {
switch (settings.name) {
case 'first_page':
return CupertinoPageRoute(builder: (context) => Text('First Page'));
case 'second_page':
return CupertinoPageRoute(builder: (context) => Text('Second Page'));
}
return CupertinoPageRoute(builder: (context) => Text(settings.name));
});
// 0x01 set route to your `App` widget
CupertinoApp(onGenerateRoute: (_) => route);
Provider a navigator to open flutter
page.
// 0x00 implement `FaradayNavigationDelegate`
extension AppDelegate: FaradayNavigationDelegate {
func push(_ name: String, arguments: Any?, options: [String : Any]?, callback token: CallbackToken) {
let isFlutter = options?["flutter"] as? Bool ?? false
let isPresent = options?["present"] as? Bool ?? false
let vc = isFlutter ? FaradayFlutterViewController(name, arguments: arguments) : FirstViewController(name, arguments: arguments)
let topMost = UIViewController.fa.topMost
if (isPresent) {
topMost?.present(vc, animated: true, completion: nil)
} else {
topMost?.navigationController?.pushViewController(vc, animated: true)
}
// IMPORTANT
vc.fa.enableCallback(with: token)
}
}
// 0x01 start flutter Engine in `application: didFinishLaunchingWithOptions`
Faraday.default.startFlutterEngine(navigatorDelegate: self)
// 0x02 open flutter page
let vc = FaradayFlutterViewController("first_page", arguments: nil)
navigationController?.pushViewController(vc, animated: true)
Provider a navigator to open flutter
page.
// 0x00 implement `FaradayNavigator`
class SimpleFlutterNavigator : FaradayNavigator {
companion object {
const val KEY_ARGS = "_args"
}
override fun create(name: String, arguments: Serializable?, options: HashMap<String, *>?): Intent? {
val context = Faraday.getCurrentActivity() ?: return null
val isFlutterRoute = options?.get("flutter") == true
if (isFlutterRoute) {
// singleTask mode
val builder = FaradayActivity.builder(name, arguments)
builder.backgroundColor = Color.WHITE
builder.activityClass = SingleTaskFlutterActivity::class.java
return builder.build(context);
}
when (name) {
"flutter2native" -> {
return Intent(context, FlutterToNativeActivity::class.java)
}
"native2flutter" -> {
return Intent(context, Native2FlutterActivity::class.java)
}
"tabContainer" -> {
return Intent(context, TabContainerActivity::class.java)
}
else -> {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(name)
intent.putExtra(KEY_ARGS, arguments)
return intent
}
}
}
override fun pop(result: Serializable?) {
val activity = Faraday.getCurrentActivity() ?: return
if (result != null) {
activity.setResult(Activity.RESULT_OK, Intent().apply { putExtra(KEY_ARGS, result) })
}
activity.finish()
}
override fun enableSwipeBack(enable: Boolean) {
}
}
// 0x01 start flutter engine in Application.onCrate
if (!Faraday.startFlutterEngine(this, SimpleFlutterNavigator())) {
GeneratedPluginRegistrant.registerWith(Faraday.engine)
}
// 0x02 open flutter page
val intent = FaradayActivity.build(context, routeName, params)
context.startActivity(intent)
If you wish to contribute a change to any of the existing plugins in this repo, please review our contribution guide and open a pull request.
g_faraday is released under the MIT license. See LICENSE for details.