text
stringlengths
1
372
let window = windowScene.windows.first(where: \.iskeywindow),
let rootViewController = window.rootViewController
else { return }
// create the FlutterViewController.
let flutterViewController = FlutterViewController(
engine: flutterDependencies.flutterEngine,
nibName: nil,
bundle: nil)
flutterViewController.modalPresentationStyle = .overcurrentcontext
flutterViewController.isViewOpaque = false
rootViewController.present(flutterViewController, animated: true)
}
}
<code_end>
the following example shows a generic ViewController with a
UIButton hooked to present a FlutterViewController.
the FlutterViewController uses the FlutterEngine instance
created in the AppDelegate.
<code_start>
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)
}
}
<code_end>
the following example shows a generic ViewController with a
UIButton hooked to present a FlutterViewController.
the FlutterViewController uses the FlutterEngine instance
created in the AppDelegate.
<code_start>
@import flutter;
#import "appdelegate.h"
#import "viewcontroller.h"
@implementation ViewController
- (void)viewdidload {
[super viewDidLoad];
// make a button to call the showFlutter function when pressed.
UIButton *button = [uibutton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(showFlutter)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show flutter!" forState:UIControlStateNormal];
button.backgroundColor = UIColor.blueColor;
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
}
- (void)showflutter {
FlutterEngine *flutterengine =
((appdelegate *)uiapplication.sharedapplication.delegate).flutterengine;
FlutterViewController *flutterviewcontroller =
[[flutterviewcontroller alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
[self presentViewController:flutterViewController animated:YES completion:nil];
}
@end
<code_end>
now, you have a flutter screen embedded in your iOS app.
info note
using the previous example, the default main()
entrypoint function of your default dart library
would run when calling run on the
FlutterEngine created in the AppDelegate.
<topic_end>
<topic_start>
alternatively - create a FlutterViewController with an implicit FlutterEngine
as an alternative to the previous example, you can let the
FlutterViewController implicitly create its own FlutterEngine without
pre-warming one ahead of time.
this is not usually recommended because creating a
FlutterEngine on-demand could introduce a noticeable
latency between when the FlutterViewController is
presented and when it renders its first frame. this could, however, be
useful if the flutter screen is rarely shown, when there are no good
heuristics to determine when the dart VM should be started, and when flutter
doesn’t need to persist state between view controllers.
to let the FlutterViewController present without an existing
FlutterEngine, omit the FlutterEngine construction, and create the
FlutterViewController without an engine reference.
<code_start>
// existing code omitted.
func showFlutter() {
let flutterViewController = FlutterViewController(project: nil, nibName: nil, bundle: nil)
present(flutterViewController, animated: true, completion: nil)
}
<code_end>
<code_start>