原有Android/IOS项目集成flutter功能

Android项目

首先要先要明确一下我们的目标
想要实现一个Android/IOS项目中集成flutter的功能,在某些场景下,比如一些界面可以用flutter来显示,大大简化了双移动端人员的工作
flutter官网

Android

我们先as创建一个Android的项目(一些flutter的开发环境配置就不说了)
  1. 我们先采用命令的方式(推荐第二种)
  • 因为后面可以让ios项目来使用,所以在同级创建
flutter create -t module module_flutter
  • 在setting.gradle文件中添加
// Include the host app project.
include ':app'                                    // assumed existing content
setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
  settingsDir.parentFile,                                              // new
  'module_flutter/.android/include_flutter.groovy'                         // new
))                                                                     // new

  • 再在app的build.gradle文件添加
implementation project(':flutter')
  1. 采用Android Studio自带的(推荐,实不相瞒,上面的命令行的方式,我也是搞了好久才成功,貌似存在一定问题)

new -> new module ->Flutter Module

  1. 跳转到flutter项目中
  • 跳转到默认的界面
// MainActivity
// 在Android项目中找个按钮
// 打开默认的main
//            FlutterActivity.createDefaultIntent(this)
  • 跳转到指定的界面(实际上也是到默认main方法)
// MainActivity
// 打开指定的widget
            startActivity(FlutterActivity
                .withNewEngine()
                .initialRoute("welcome")
                .build(this))

// flutter 的main文件
void main() => runApp(MyApp(window.defaultRouteName));

class MyApp extends StatelessWidget {
  String name;

  MyApp(this.name);

  @override
  Widget build(BuildContext context) {
    print("flutter_name111$name");
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: getWidgetName(name),
    );
  }
}

Widget getWidgetName(String name) {
  switch(name) {
    case "welcome":
      return WelcomeWidget();
      break;
    case "hahaha":
      return MyHomePage(title: "hahaha",);
      break;
    default:
      return MyHomePage(title: "hahaha",);
      break;
  }
}
  1. 如果没有成功,多看看官网,书中自由颜如玉,书中自有黄金屋

IOS

由于也是接触ios开发一段时间,请多包含了

我们刚才Android已经创建了一个flutter module,这个时候我们ios就可以直接使用那个module了
  1. 这里我们就直接采用CocoaPods的方式集成(问我为什么,因为他最方便。。。先一点一点来嘛对不对)Embed with CocoaPods and the Flutter SDK
  2. 先把项目用集成CocoaPods,问我怎么搞官网
  3. 新建一个Podfile到你项目中去
flutter_application_path = '../module_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target '你的项目名' do
  install_all_flutter_pods(flutter_application_path)
end

命令行到该项目中

pod install
  1. 跳转到flutter项目中(FlutterEngine和FlutterViewController)
  • 这里有两种方式跳转到flutter (FlutterEngine)
AppDelegate.swift中

import UIKit
import Flutter
// Used to connect plugins (only if you have plugins with iOS platform code).
import FlutterPluginRegistrant

@UIApplicationMain
class AppDelegate: FlutterAppDelegate { // More on the FlutterAppDelegate.
  lazy var flutterEngine = FlutterEngine(name: "my flutter engine")

  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Runs the default Dart entrypoint with a default Flutter route.
    flutterEngine.run();
    // Used to connect plugins (only if you have plugins with iOS platform code).
    GeneratedPluginRegistrant.register(with: self.flutterEngine);
    return super.application(application, didFinishLaunchingWithOptions: launchOptions);
  }
}

ViewController.swift中

import UIKit
import Flutter

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()

    // Make a button to call the showFlutter function when pressed.
    let button = UIButton(type:UIButton.ButtonType.custom)
    button.addTarget(self, action: #selector(showFlutter), for: .touchUpInside)
    button.setTitle("Show Flutter!", for: UIControl.State.normal)
    button.frame = CGRect(x: 80.0, y: 210.0, width: 160.0, height: 40.0)
    button.backgroundColor = UIColor.blue
    self.view.addSubview(button)
  }

  @objc func showFlutter() {
    let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
    let flutterViewController =
        FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
    present(flutterViewController, animated: true, completion: nil)
  }
}
  • 使用FlutterViewController跳转(FlutterViewController)(推荐这种方式)
// Existing code omitted.
func showFlutter() {
  let flutterViewController = FlutterViewController(project: nil, nibName: nil, bundle: nil)
  present(flutterViewController, animated: true, completion: nil)
}

  • 跳转到指定的界面
let flutterViewController = FlutterViewController(project: nil, nibName: nil, bundle: nil)
flutterViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen// 全屏
flutterViewController.setInitialRoute("welcome")
present(flutterViewController, animated: true, completion: nil)
  1. 如果没有成功,多看看官网,书中自由颜如玉,书中自有黄金屋
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android Studio 中部署 Flutter 项目iOS 设备上,需要遵循以下步骤: 1. 在 Flutter 项目中打开终端,并运行 `flutter build ios` 命令,以构建 iOS 应用程序。 2. 在 iOS 设备上安装 Xcode,打开 Xcode 并选择 `File -> New -> Project` 菜单项。 3. 在弹出的菜单中,选择 `Application -> Single View Application`,并输入应用程序的名称和组织标识符。 4. 在 `Product` 菜单中选择 `Destination`,并选择要部署到的 iOS 设备。 5. 在 `File` 菜单中选择 `Add Files to "Project Name"`,并选择 Flutter 项目中的 `ios` 文件夹。 6. 在弹出的菜单中,选择 `Copy items if needed`,并点击 `Finish`。 7. 在 `AppDelegate.swift` 文件中,添加以下代码: ``` import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } } ``` 8. 在 `Info.plist` 文件中,添加以下权限: ``` <key>NSCameraUsageDescription</key> <string>Camera Permission</string> <key>NSMicrophoneUsageDescription</key> <string>Microphone Permission</string> <key>NSPhotoLibraryUsageDescription</key> <string>Photo Library Permission</string> ``` 9. 在 Xcode 中,选择 `Product -> Build` 菜单项,以构建 iOS 应用程序。 以上就是在 Android Studio 中部署 Flutter 项目iOS 设备上的步骤。注意,在构建 iOS 应用程序之前,需要在 Mac 上安装并配置 Xcode 和 iOS 开发人员工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值