Created
November 7, 2019 06:45
-
-
Save cmengler/7c9689c8c5d5e94b5063d2c4ab03eb14 to your computer and use it in GitHub Desktop.
Flutter WebView example code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface AppDelegate : FlutterAppDelegate | |
@property (nonatomic, strong) FlutterViewController *rootViewController; | |
@property (nonatomic, strong) NSMutableArray *flutterViewControllers; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application | |
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
[GeneratedPluginRegistrant registerWithRegistry:self]; | |
self.rootViewController = (FlutterViewController*) self.window.rootViewController; | |
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(200, 100, 200, 80)]; | |
[button setTitle:@"OPEN" forState:UIControlStateNormal]; | |
[button setBackgroundColor:[UIColor redColor]]; | |
[button addTarget:self action:@selector(launchNewFlutterViewController) forControlEvents:UIControlEventTouchUpInside]; | |
[[self.rootViewController view] addSubview:button]; | |
return [super application:application didFinishLaunchingWithOptions:launchOptions]; | |
} | |
- (void)initFlutterViewControllers { | |
_flutterViewControllers = [NSMutableArray array]; | |
for (int i = 0; i < 2; i++) { | |
FlutterViewController* flutterViewController = [[FlutterViewController alloc] init]; | |
[GeneratedPluginRegistrant registerWithRegistry:flutterViewController.pluginRegistry]; | |
[flutterViewController setSplashScreenView:nil]; | |
[_flutterViewControllers addObject:flutterViewController]; | |
} | |
} | |
- (void)launchNewFlutterViewController { | |
[self initFlutterViewControllers]; | |
// _flutterViewControllers[0] = Not working | |
// _flutterViewControllers[1] = Working (last FVC with FlutterPlatformViewController instantiated) | |
[self.rootViewController presentViewController:_flutterViewControllers[1] | |
animated:YES | |
completion:nil]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ui'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/gestures.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:webview_flutter/webview_flutter.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final size = window.physicalSize; | |
final devicePixelRatio = window.devicePixelRatio; | |
final double screenWidth = size.width / devicePixelRatio; | |
final double screenHeight = size.height / devicePixelRatio; | |
return MaterialApp( | |
title: 'WebView Test', | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('WebView Test'), | |
), | |
body: Center( | |
child: SingleChildScrollView( | |
child: SizedBox.fromSize( | |
size: Size(screenWidth * .9, screenHeight * .6), | |
child: Container( | |
decoration: BoxDecoration(border: Border.all(color: Colors.blue)), | |
child: WebView( | |
initialUrl: 'https://flutter.dev', | |
javascriptMode: JavascriptMode.unrestricted, | |
gestureRecognizers: {}..add(Factory<VerticalDragGestureRecognizer>(() => VerticalDragGestureRecognizer())), | |
), | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment