Skip to content

Instantly share code, notes, and snippets.

@cmengler
Created November 7, 2019 06:45
Show Gist options
  • Save cmengler/7c9689c8c5d5e94b5063d2c4ab03eb14 to your computer and use it in GitHub Desktop.
Save cmengler/7c9689c8c5d5e94b5063d2c4ab03eb14 to your computer and use it in GitHub Desktop.
Flutter WebView example code
@interface AppDelegate : FlutterAppDelegate
@property (nonatomic, strong) FlutterViewController *rootViewController;
@property (nonatomic, strong) NSMutableArray *flutterViewControllers;
@end
@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
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