请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

iOS混合开发调用flutter_module用swift5语言怎么写哒?

如题,劳烦老师看看?oc我是小白。

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var testBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    

    @IBAction func clickTest(_ sender: UIButton) {
        print("clickTest~~~~")
    }
    
}

正在回答 回答被采纳积分+3

1回答

提问者 小葱与奥特曼 2019-05-17 08:56:26

跟着官方github,有点奇怪,有问题。麻烦解答一下?

https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps

import UIKit
import Flutter
import FlutterPluginRegistrant // Only if you have Flutter Plugins.

@UIApplicationMain
class AppDelegate: FlutterAppDelegate {

    var flutterEngine : FlutterEngine?;
    // Only if you have Flutter plugins.
    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.flutterEngine = FlutterEngine(name: "io.flutter", project: nil);
        self.flutterEngine?.run(withEntrypoint: nil);
        GeneratedPluginRegistrant.register(with: self.flutterEngine);
        return super.application(application, didFinishLaunchingWithOptions: launchOptions);
    }


}
import UIKit
import Flutter

class ViewController: UIViewController {

    @IBOutlet weak var testBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        let button = UIButton(type:UIButton.ButtonType.custom)
        button.addTarget(self, action: #selector(handleButtonAction), for: .touchUpInside)
        button.setTitle("Press me", 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 handleButtonAction() {
        let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine;
        let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)!;
        flutterViewController.setInitialRoute("route1")
        self.present(flutterViewController, animated: false, completion: nil)
        

    }
    

    @IBAction func clickTest(_ sender: UIButton) {
        print("clickTest~~~~")
    }
    
}

点击press me,出来的似乎不是自己的代码。是一个默认的界面

//img1.sycdn.imooc.com//szimg/5cde0667000101eb09381862.jpg

而我的代码是用Android时的flutter_module...

import 'dart:ui';

import 'package:flutter/material.dart';

void main() => runApp(MyApp(initParams: window.defaultRouteName,));

class MyApp extends StatelessWidget {

  final String initParams;

  const MyApp({Key key, this.initParams}) : super(key: key);
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter 混合开发',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: '22Flutter 混合开发哦', initParams: initParams,),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title, this.initParams}) : super(key: key);

  final String title;
  final String initParams;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'initParams:${widget.initParams}',
              style: TextStyle(color: Colors.red,fontSize: 20),
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}


0 回复 有任何疑惑可以回复我~
  • 从代码上看没发现问题,可以根据课程的混合开发的项目结构检查下,看是不是结构不一样呢:https://git.imooc.com/coding-321/flutter_trip
    回复 有任何疑惑可以回复我~ 2019-05-18 21:57:11
  • 提问者 小葱与奥特曼 回复 CrazyCodeBoy #2
    哦。我重新运行下dart部分的flutter_module,显示的“22Flutter 混合开发哦”标题正常了。
    只是,也出现了老师所说的使用flutterEngine(方式二)时,flutter SDK的bug(window.defaultRouteName是/),现在有修复这个问题啦吗?
    回复 有任何疑惑可以回复我~ 2019-05-19 17:41:35
  • 提问者 小葱与奥特曼 回复 CrazyCodeBoy #3
    能用了。
    1、先在AppDelegate移除代码self.flutterEngine?.run(withEntrypoint: nil);
    2、viewController中
    let flutterViewController = FlutterViewController(nibName: nil, bundle: nil)
    3、再setInitialRoute
    4、最后 flutterEngine?.run(withEntrypoint: nil).
    就好了。
    回复 有任何疑惑可以回复我~ 2019-05-19 17:55:42
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信