|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +/* |
| 5 | +this is needed atm to let qtdeploy/qtminimal know that this file needs to be analysed |
| 6 | +
|
| 7 | +import ( |
| 8 | + "github.com/therecipe/qt/core" |
| 9 | + "github.com/therecipe/qt/quick" |
| 10 | + "github.com/therecipe/qt/quickcontrols2" |
| 11 | +) |
| 12 | +*/ |
| 13 | + |
| 14 | +import 'qt/interop.dart' as interop; |
| 15 | +import 'qt/core.dart' as core; |
| 16 | +import 'qt/quick.dart' as quick; |
| 17 | +import 'qt/quickcontrols2.dart' as quickcontrols2; |
| 18 | + |
| 19 | +final interopEngine = (!Platform.isWindows) ? interop.PseudoQJSEngine_qjsEngine(null) : null; |
| 20 | +final quickWidget = (!Platform.isWindows) ? quick.NewQQuickWidget(null) : null; |
| 21 | + |
| 22 | +void main() { |
| 23 | + |
| 24 | + if (!Platform.isWindows) { |
| 25 | + interopEngine.GlobalObject().SetProperty("dartFunction", interopEngine.NewGoType((String s) { print(s); })); |
| 26 | + |
| 27 | + // setup qml widget |
| 28 | + |
| 29 | + quickcontrols2.QQuickStyle_SetStyle("Material"); |
| 30 | + |
| 31 | + quickWidget.Engine().GlobalObject().SetProperty("dartFunction", quickWidget.Engine().NewGoType((String s) { print(s); })); |
| 32 | + quickWidget.Engine().GlobalObject().SetProperty("qml", quickWidget.Engine().NewObject()); //create an empty object here, so we can write to it later from withing Qml since we can't directly add properties to the globalObject from within qml |
| 33 | + quickWidget.SetMinimumSize(core.NewQSize2(400, 300)); |
| 34 | + quickWidget.SetResizeMode(1); //TODO: quick.QQuickWidget__SizeRootObjectToView |
| 35 | + quickWidget.SetSource(core.NewQUrl3("qrc:/qml/main.qml", 0)); |
| 36 | + |
| 37 | + // call into go and let it finish the layout setup |
| 38 | + |
| 39 | + interopEngine.GlobalObject().SetProperty("quickWidget", interopEngine.NewGoType(quickWidget)); |
| 40 | + interopEngine.GlobalObject().Property("goSetupFunction").Call(null); |
| 41 | + } |
| 42 | + |
| 43 | + runApp(MyApp()); |
| 44 | +} |
| 45 | + |
| 46 | +/// This Widget is the main application widget. |
| 47 | +class MyApp extends StatelessWidget { |
| 48 | + static const String _title = 'Dart-Go-Qml Interop'; |
| 49 | + |
| 50 | + @override |
| 51 | + Widget build(BuildContext context) { |
| 52 | + return MaterialApp( |
| 53 | + title: _title, |
| 54 | + home: Scaffold( |
| 55 | + appBar: AppBar(title: const Text(_title)), |
| 56 | + body: MyStatelessWidget(), |
| 57 | + ), |
| 58 | + ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// This is the stateless widget that the main application instantiates. |
| 63 | +class MyStatelessWidget extends StatelessWidget { |
| 64 | + MyStatelessWidget({Key key}) : super(key: key); |
| 65 | + |
| 66 | + @override |
| 67 | + Widget build(BuildContext context) { |
| 68 | + return Center( |
| 69 | + child: Column( |
| 70 | + mainAxisSize: MainAxisSize.min, |
| 71 | + children: <Widget>[ |
| 72 | + RaisedButton( |
| 73 | + onPressed: () { |
| 74 | + if (!Platform.isWindows) { |
| 75 | + interopEngine.GlobalObject().Property("goFunction").Call([interopEngine.NewGoType("Hello from Dart")]); |
| 76 | + } |
| 77 | + }, |
| 78 | + child: const Text('Dart calls Go', style: TextStyle(fontSize: 20)), |
| 79 | + ), |
| 80 | + const SizedBox(height: 30), |
| 81 | + RaisedButton( |
| 82 | + onPressed: () { |
| 83 | + if (!Platform.isWindows) { |
| 84 | + quickWidget.Engine().GlobalObject().Property("qml").Property("qmlFunction").Call([quickWidget.Engine().NewGoType("Hello from Dart")]); |
| 85 | + } |
| 86 | + }, |
| 87 | + child: const Text('Dart calls Qml', style: TextStyle(fontSize: 20)), |
| 88 | + ), |
| 89 | + ], |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments