Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created April 15, 2018 21:02
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save branflake2267/5d68ad16193fe5ef86f37c0a4a4e8abc to your computer and use it in GitHub Desktop.
Save branflake2267/5d68ad16193fe5ef86f37c0a4a4e8abc to your computer and use it in GitHub Desktop.
Flutter - Native Platform Interactions - Code for the youtube video.
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
// Override point for customization after application launch.
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"demo.gawkat.com/info"
binaryMessenger:controller];
[channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
NSString *from = call.arguments[@"from"];
if ([@"getMessage" isEqualToString:call.method]) {
//NSString *message = @"iOS says greetings";
//NSString *returnMessage = [message stringByAppendingString:from];
//result(returnMessage);
UIApplication *mySafari = [UIApplication sharedApplication];
NSURL *myURL = [[NSURL alloc]initWithString:@"https://flutter.io"];
[mySafari openURL:myURL];
}
}];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.lightGreen,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const platform = const MethodChannel('demo.gawkat.com/info');
String _message = "No messages yet...";
@override
void initState() {
// fetch and change the message from the platform
_getMessage().then((String message) {
setState(() {
_message = message;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Home"),
),
body: new ListView(
children: <Widget>[
new ListTile(
title: new Text(_message),
)
],
),
);
}
Future<String> _getMessage() async {
var sendMap = <String, dynamic> {
'from' : 'Brandon',
};
String value;
try {
value = await platform.invokeMethod('getMessage', sendMap);
} catch (e) {
print(e);
}
return value;
}
}
package com.gawkat.flutterplatforminteractions;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import java.util.Map;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "demo.gawkat.com/info";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
final Map<String, Object> arguments = methodCall.arguments();
if (methodCall.method.equals("getMessage")) {
// String from = (String) arguments.get("from");
//
// String message = "Android say hi " + from;
//
// result.success(message);
openWebPage("https://flutter.io");
}
}
});
}
public void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
@branflake2267
Copy link
Author

The video that covered this source: https://youtu.be/0nJrGKyRlPQ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment