This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
flutter-unity-view-widget/lib/flutter_unity_widget.dart

68 lines
1.7 KiB
Dart
Raw Normal View History

2019-03-09 10:47:09 -05:00
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
typedef void FlutterUnityWidgetCreatedCallback(UnityWidgetController controller);
class UnityWidgetController {
static MethodChannel _channel =
const MethodChannel('flutter_unity_widget');
UnityWidgetController();
init(int id) {
_channel = new MethodChannel('nativeweb_$id');
}
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}
class UnityWidget extends StatefulWidget {
UnityWidgetController onUnityViewCreated;
UnityWidget({
Key key,
@required this.onUnityViewCreated,
});
@override
_UnityWidgetState createState() => _UnityWidgetState();
}
class _UnityWidgetState extends State<UnityWidget> {
@override
Widget build(BuildContext context) {
if(defaultTargetPlatform == TargetPlatform.android) {
return AndroidView(
viewType: 'unityview',
onPlatformViewCreated: onPlatformViewCreated,
creationParamsCodec: const StandardMessageCodec(),
);
} else if(defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
viewType: 'unityview',
onPlatformViewCreated: onPlatformViewCreated,
creationParamsCodec: const StandardMessageCodec(),
);
}
return new Text('$defaultTargetPlatform is not yet supported by this plugin');
}
Future<void> onPlatformViewCreated(id) async {
if (widget.onUnityViewCreated == null) {
return;
}
widget.onUnityViewCreated = new UnityWidgetController().init(id);
}
}