Last active
July 11, 2020 09:44
-
-
Save najeira/c9ac5ae0519cc01635d234bfd7465451 to your computer and use it in GitHub Desktop.
Flutter with platform
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 UIKit | |
import Flutter | |
@UIApplicationMain | |
@objc class AppDelegate: FlutterAppDelegate, MyViewDelegate { | |
var flutterViewController: FlutterViewController? | |
var flutterResult: FlutterResult? | |
override func application( | |
_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | |
) -> Bool { | |
let fvc : FlutterViewController = window?.rootViewController as! FlutterViewController | |
let channel = FlutterMethodChannel(name: "my channel", binaryMessenger: fvc.binaryMessenger) | |
channel.setMethodCallHandler(self.handle) | |
flutterViewController = fvc | |
let nav = UINavigationController.init(rootViewController: fvc) | |
nav.isNavigationBarHidden = true | |
window.rootViewController = nav | |
GeneratedPluginRegistrant.register(with: self) | |
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | |
} | |
func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) -> Void { | |
switch call.method { | |
case "hello": | |
result("iOS!") | |
case "text": | |
flutterResult = result | |
let mvc = MyViewController() | |
mvc.delegate = self | |
let nav = window?.rootViewController as! UINavigationController | |
nav.present(mvc, animated: true, completion: nil) | |
default: | |
result(FlutterMethodNotImplemented) | |
} | |
} | |
func textDidEdit(_ text: String) { | |
flutterResult?(text) | |
flutterResult = nil | |
} | |
override func registrar(forPlugin: String) -> FlutterPluginRegistrar { | |
return (self.flutterViewController?.registrar(forPlugin: forPlugin))! | |
} | |
override func hasPlugin(_ pluginKey: String) -> Bool { | |
return (self.flutterViewController?.hasPlugin(pluginKey))! | |
} | |
override func valuePublished(byPlugin pluginKey: String) -> NSObject { | |
return (self.flutterViewController?.valuePublished(byPlugin: pluginKey))! | |
} | |
} | |
protocol MyViewDelegate { | |
func textDidEdit(_ text: String) | |
} | |
class MyViewController: UIViewController, UITextViewDelegate { | |
var delegate: MyViewDelegate? | |
let editText = UITextView.init(frame: .zero) | |
let submitButton = UIButton.init(frame: .zero) | |
override func viewDidLoad() { | |
self.view.backgroundColor = .white | |
editText.font = UIFont.systemFont(ofSize: 16.0) | |
editText.frame = CGRect(x: 8.0, y: 48.0, width: self.view.frame.width - 16.0, height: 200.0) | |
editText.delegate = self | |
editText.keyboardType = .default | |
editText.returnKeyType = .default | |
editText.backgroundColor = .white | |
submitButton.setTitle("OK", for: .normal) | |
submitButton.frame = CGRect(x: 8.0, y: 208.0, width: self.view.frame.width - 16.0, height: 40.0) | |
submitButton.addTarget(self, action: #selector(self.submitButtonDidTouch(sender:)), for: .touchUpInside) | |
submitButton.backgroundColor = .systemBlue | |
self.view.addSubview(editText) | |
self.view.addSubview(submitButton) | |
} | |
@objc func submitButtonDidTouch(sender: UIButton) { | |
editText.resignFirstResponder() | |
delegate?.textDidEdit(editText.text) | |
if let nav = self.navigationController { | |
nav.dismiss(animated: true) | |
} else { | |
self.dismiss(animated: true, completion: nil) | |
} | |
} | |
} |
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:io'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
const channel = MethodChannel('my channel'); | |
void main() { | |
//runApp(MyApp()); | |
runApp(MyApp2()); | |
//runApp(MyApp3()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold( | |
appBar: AppBar(), | |
body: Center( | |
child: FutureBuilder<String>( | |
future: channel.invokeMethod<String>('hello'), | |
builder: (BuildContext context, AsyncSnapshot<String> snapshot) { | |
return Text(snapshot.data ?? '...'); | |
}, | |
), | |
), | |
), | |
); | |
} | |
} | |
class MyApp2 extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold( | |
appBar: AppBar(), | |
body: FutureBuilder<String>( | |
future: channel.invokeMethod<String>('text'), | |
builder: (BuildContext context, AsyncSnapshot<String> snapshot) { | |
return Text(snapshot.data ?? '...'); | |
}, | |
), | |
), | |
); | |
} | |
} |
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
package com.example.myapp; | |
import android.content.Intent; | |
import androidx.annotation.NonNull; | |
import io.flutter.embedding.android.FlutterActivity; | |
import io.flutter.embedding.engine.FlutterEngine; | |
import io.flutter.plugin.common.MethodCall; | |
import io.flutter.plugin.common.MethodChannel; | |
public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler { | |
static private final int REQUEST_CODE = 0xf1677e4; | |
private MethodChannel.Result mResult; | |
@Override | |
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { | |
super.configureFlutterEngine(flutterEngine); | |
MethodChannel channel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "my channel"); | |
channel.setMethodCallHandler(this); | |
} | |
@Override | |
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { | |
switch (call.method) { | |
case "hello": | |
result.success("Android!"); | |
break; | |
case "text": | |
mResult = result; | |
Intent intent = new Intent(this, MyActivity.class); | |
startActivityForResult(intent, REQUEST_CODE); | |
break; | |
default: | |
result.notImplemented(); | |
break; | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_CODE) { | |
if (resultCode == RESULT_OK) { | |
mResult.success(data.getStringExtra("data")); | |
} else { | |
mResult.success("cancel"); | |
} | |
mResult = null; | |
} else { | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
} | |
} |
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
package com.example.myapp; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.LinearLayout; | |
public class MyActivity extends Activity implements View.OnClickListener { | |
private EditText editText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
LinearLayout layout = new LinearLayout(this); | |
layout.setOrientation(LinearLayout.VERTICAL); | |
editText = new EditText(this); | |
layout.addView(editText, new LinearLayout.LayoutParams( | |
LinearLayout.LayoutParams.MATCH_PARENT, | |
500 | |
)); | |
Button btn = new Button(this); | |
btn.setText("OK"); | |
btn.setOnClickListener(this); | |
layout.addView(btn, new LinearLayout.LayoutParams( | |
LinearLayout.LayoutParams.MATCH_PARENT, | |
LinearLayout.LayoutParams.WRAP_CONTENT | |
)); | |
setContentView(layout); | |
} | |
@Override | |
public void onClick(View view) { | |
final Intent intent = new Intent(); | |
intent.putExtra("data", editText.getText().toString()); | |
setResult(RESULT_OK, intent); | |
finish(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment