├── .gitignore ├── .metadata ├── CHANGELOG.md ├── LICENSE ├── README.md ├── lib ├── flutter_lua.dart ├── flutter_widget.dart └── widget │ ├── _flex.dart │ ├── column.dart │ ├── enumerate.dart │ ├── gesture_detector.dart │ ├── image.dart │ ├── parameter_exception.dart │ ├── row.dart │ └── text.dart ├── pubspec.lock ├── pubspec.yaml └── test ├── flutter_lua_test.dart ├── person.dart └── test.lua /.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | .lock 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | .dart_tool/ 26 | .flutter-plugins 27 | .flutter-plugins-dependencies 28 | .packages 29 | .pub-cache/ 30 | .pub/ 31 | build/ 32 | 33 | # Android related 34 | **/android/**/gradle-wrapper.jar 35 | **/android/.gradle 36 | **/android/captures/ 37 | **/android/gradlew 38 | **/android/gradlew.bat 39 | **/android/local.properties 40 | **/android/**/GeneratedPluginRegistrant.java 41 | 42 | # iOS/XCode related 43 | **/ios/**/*.mode1v3 44 | **/ios/**/*.mode2v3 45 | **/ios/**/*.moved-aside 46 | **/ios/**/*.pbxuser 47 | **/ios/**/*.perspectivev3 48 | **/ios/**/*sync/ 49 | **/ios/**/.sconsign.dblite 50 | **/ios/**/.tags* 51 | **/ios/**/.vagrant/ 52 | **/ios/**/DerivedData/ 53 | **/ios/**/Icon? 54 | **/ios/**/Pods/ 55 | **/ios/**/.symlinks/ 56 | **/ios/**/profile 57 | **/ios/**/xcuserdata 58 | **/ios/.generated/ 59 | **/ios/Flutter/App.framework 60 | **/ios/Flutter/Flutter.framework 61 | **/ios/Flutter/Flutter.podspec 62 | **/ios/Flutter/Generated.xcconfig 63 | **/ios/Flutter/app.flx 64 | **/ios/Flutter/app.zip 65 | **/ios/Flutter/flutter_assets/ 66 | **/ios/Flutter/flutter_export_environment.sh 67 | **/ios/ServiceDefinitions.json 68 | **/ios/Runner/GeneratedPluginRegistrant.* 69 | 70 | # Exceptions to above rules. 71 | !**/ios/**/default.mode1v3 72 | !**/ios/**/default.mode2v3 73 | !**/ios/**/default.pbxuser 74 | !**/ios/**/default.perspectivev3 75 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: d408d302e22179d598f467e11da5dd968dbdc9ec 8 | channel: stable 9 | 10 | project_type: package 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.0.2 2 | * Wrapping several widgets for Lua 3 | * Add example 4 | 5 | ## 0.0.1 6 | * Initializing the project 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2021 YingShuKun 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flutter_lua_dardo 2 | 3 | This is an extension of the [LuaDardo](https://github.com/arcticfox1919/LuaDardo) library.LuaDardo library allows Flutter to execute Lua scripts, and this library which mainly wraps some of Flutter's interfaces, which gives Flutter the ability to dynamically update and generate interfaces using remote scripts in places where UI styles need to be changed frequently. 4 | 5 | Please note that this is an experimental exploration. It only encapsulates a few simple Widgets. Others are welcome to encapsulate more Widgets for Lua. 6 | 7 | ![](https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/GIF_2021-5-11_21-44-49.gif) 8 | 9 | ## Usage 10 | 11 | New Lua script `test.lua`: 12 | 13 | 14 | ```lua 15 | function getContent1() 16 | return Row:new({ 17 | children={ 18 | GestureDetector:new({ 19 | onTap=function() 20 | flutter.debugPrint("--------------onTap--------------") 21 | end, 22 | 23 | child=Text:new("click here")}), 24 | Text:new("label1"), 25 | Text:new("label2"), 26 | Text:new("label3"), 27 | }, 28 | mainAxisAlign=MainAxisAlign.spaceEvenly, 29 | }) 30 | end 31 | 32 | function getContent2() 33 | return Column:new({ 34 | children={ 35 | Row:new({ 36 | children={Text:new("Hello"),Text:new("Flutter")}, 37 | mainAxisAlign=MainAxisAlign.spaceAround 38 | }), 39 | Image:network('https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/flutter_lua_test.png' 40 | ,{fit=BoxFit.cover}) 41 | }, 42 | mainAxisSize=MainAxisSize.min, 43 | crossAxisAlign=CrossAxisAlign.center 44 | }) 45 | end 46 | ``` 47 | 48 | In your Flutter project, add the dependency. `pubspec.yaml` 49 | 50 | ```yaml 51 | dependencies: 52 | flutter_lua_dardo: ^0.0.2 53 | ``` 54 | 55 | Add Dart code: 56 | 57 | ```dart 58 | class _MyHomePageState extends State { 59 | LuaState _ls; 60 | bool isChange = false; 61 | 62 | @override 63 | void initState() { 64 | loadLua(); 65 | super.initState(); 66 | } 67 | 68 | void loadLua() async { 69 | String src = await rootBundle.loadString('assets/test.lua'); 70 | try { 71 | LuaState ls = LuaState.newState(); 72 | ls.openLibs(); 73 | FlutterLua.open(ls); 74 | FlutterWidget.open(ls); 75 | ls.doString(src); 76 | setState(() { 77 | _ls = ls; 78 | }); 79 | } catch (e) { 80 | print(e); 81 | } 82 | } 83 | 84 | @override 85 | Widget build(BuildContext context) { 86 | return Scaffold( 87 | body: Container( 88 | alignment: Alignment.center, 89 | child: _ls == null 90 | ? CircularProgressIndicator() 91 | // call the specified Lua function 92 | : FlutterWidget.findViewByName( 93 | _ls, isChange ? "getContent2" : "getContent1"), 94 | ), 95 | floatingActionButton: FloatingActionButton( 96 | child: Icon(CupertinoIcons.arrow_swap), 97 | onPressed: (){ 98 | setState(() { 99 | isChange = !isChange; 100 | }); 101 | }, 102 | ), 103 | ); 104 | } 105 | } 106 | ``` 107 | 108 | **To learn how to bind the Dart class to Lua, you can view this [example](https://github.com/arcticfox1919/flutter_lua_dardo/tree/master/test).** 109 | 110 | For usage of LuaDardo, see [here](https://github.com/arcticfox1919/libd/blob/main/README.md). 111 | 112 | -------------------------------------------------------------------------------- /lib/flutter_lua.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:lua_dardo/lua.dart'; 3 | 4 | 5 | 6 | class FlutterLua{ 7 | 8 | static final Map _registry = { 9 | "debugPrint":_debugPrintWrap, 10 | }; 11 | 12 | static int _openFlutterLib(LuaState ls) { 13 | ls.newLib(_registry); 14 | // ls.newTable(); 15 | // ls.setField(-2, "widget"); 16 | return 1; 17 | } 18 | 19 | static void open(LuaState ls){ 20 | ls.requireF("flutter", _openFlutterLib, true); 21 | ls.pop(1); 22 | } 23 | 24 | static int _debugPrintWrap(LuaState ls){ 25 | String s = ls.checkString(1); 26 | debugPrint(s); 27 | return 0; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/flutter_widget.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:flutter_lua_dardo/widget/column.dart'; 3 | import 'package:flutter_lua_dardo/widget/enumerate.dart'; 4 | import 'package:flutter_lua_dardo/widget/gesture_detector.dart'; 5 | import 'package:flutter_lua_dardo/widget/image.dart'; 6 | import 'package:flutter_lua_dardo/widget/row.dart'; 7 | import 'package:flutter_lua_dardo/widget/text.dart'; 8 | import 'package:lua_dardo/lua.dart'; 9 | 10 | 11 | 12 | 13 | class FlutterWidget{ 14 | 15 | static void open(LuaState ls){ 16 | FlutterCrossAxisAlign.require(ls); 17 | FlutterMainAxisAlign.require(ls); 18 | FlutterMainAxisSize.require(ls); 19 | FlutterBoxFit.require(ls); 20 | FlutterRow.require(ls); 21 | FlutterColumn.require(ls); 22 | FlutterText.require(ls); 23 | FlutterImage.require(ls); 24 | FlutterGestureDetector.require(ls); 25 | } 26 | 27 | 28 | static T findViewByName(LuaState ls,String name){ 29 | ls.getGlobal(name); 30 | ls.pCall(0, 1, 1); 31 | if(ls.isUserdata(-1)){ 32 | var w = ls.toUserdata(-1).data; 33 | ls.setTop(0); 34 | return w; 35 | } 36 | throw Exception("Cannot find $name, " 37 | "please check the function name in the Lua script."); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /lib/widget/_flex.dart: -------------------------------------------------------------------------------- 1 | 2 | 3 | import 'package:flutter/widgets.dart'; 4 | import 'package:flutter_lua_dardo/widget/parameter_exception.dart'; 5 | import 'package:lua_dardo/lua.dart'; 6 | 7 | import 'enumerate.dart'; 8 | 9 | 10 | class FlutterFlex{ 11 | 12 | static const className = "FlexClass"; 13 | 14 | static const Map _flexMembers = { 15 | "id": _id}; 16 | 17 | static int _id(LuaState ls) { 18 | Widget p = ls.toUserdata(1).data; 19 | ls.pushInteger(identityHashCode(p)); 20 | return 1; 21 | } 22 | 23 | static void openFlex(LuaState ls) { 24 | ls.newMetatable(className); 25 | ls.pushValue(-1); 26 | ls.setField(-2, "__index"); 27 | ls.setFuncs(_flexMembers, 0); 28 | } 29 | 30 | static bool newFlex(LuaState ls,[String subclass]){ 31 | var args = ls.getTop(); 32 | if( args == 0 || !ls.isTable(-1)){ 33 | return false; 34 | } 35 | 36 | MainAxisSize mainAxisSize; 37 | MainAxisAlignment mainAxisAlignment; 38 | CrossAxisAlignment crossAxisAlignment; 39 | List children = []; 40 | 41 | var fieldType = ls.getField(-1, "mainAxisSize"); 42 | if(fieldType == LuaType.luaNumber){ 43 | mainAxisSize = FlutterMainAxisSize.get(ls.toIntegerX(-1)); 44 | ls.pop(1); 45 | }else if(fieldType == LuaType.luaNil){ 46 | mainAxisSize = MainAxisSize.max; 47 | ls.pop(1); 48 | }else{ 49 | throw ParameterError(name: 'mainAxisSize', 50 | type: ls.typeName(fieldType), 51 | expected: "int", 52 | source: subclass+" newFlex" 53 | ); 54 | } 55 | 56 | fieldType = ls.getField(-1, "mainAxisAlign"); 57 | if(fieldType == LuaType.luaNumber){ 58 | mainAxisAlignment = FlutterMainAxisAlign.get(ls.toIntegerX(-1)); 59 | ls.pop(1); 60 | }else if(fieldType == LuaType.luaNil){ 61 | mainAxisAlignment = MainAxisAlignment.start; 62 | ls.pop(1); 63 | }else{ 64 | throw ParameterError(name: 'mainAxisAlign', 65 | type: ls.typeName(fieldType), 66 | expected: "int", 67 | source:subclass+" newFlex" 68 | ); 69 | } 70 | 71 | fieldType = ls.getField(-1, "crossAxisAlign"); 72 | if(fieldType == LuaType.luaNumber){ 73 | crossAxisAlignment = FlutterCrossAxisAlign.get(ls.toIntegerX(-1)); 74 | ls.pop(1); 75 | }else if(fieldType == LuaType.luaNil){ 76 | crossAxisAlignment = CrossAxisAlignment.center; 77 | ls.pop(1); 78 | }else{ 79 | throw ParameterError(name: 'crossAxisAlign', 80 | type: ls.typeName(fieldType), 81 | expected: "int", 82 | source: subclass+" newFlex" 83 | ); 84 | } 85 | 86 | fieldType = ls.getField(-1, "children"); 87 | if(fieldType == LuaType.luaTable){ 88 | var len = ls.len2(-1); 89 | 90 | for(int i = 1;i<=len;i++){ 91 | if(ls.rawGetI(-1, i) == LuaType.luaUserdata){ 92 | children.add(ls.toUserdata(-1).data as Widget); 93 | } 94 | 95 | ls.pop(1); 96 | } 97 | }else if(fieldType == LuaType.luaNil){ 98 | ls.pop(1); 99 | }else{ 100 | throw ParameterError(name: 'children', 101 | type: ls.typeName(fieldType), 102 | expected: "List", 103 | source:subclass+" newFlex" 104 | ); 105 | } 106 | 107 | Userdata u = ls.newUserdata(); 108 | if(T == Column){ 109 | u.data = Column(children: children,mainAxisAlignment: mainAxisAlignment, 110 | crossAxisAlignment: crossAxisAlignment,mainAxisSize: mainAxisSize,); 111 | }else if(T == Row){ 112 | u.data = Row(children: children,mainAxisAlignment: mainAxisAlignment, 113 | crossAxisAlignment: crossAxisAlignment,mainAxisSize: mainAxisSize,); 114 | } 115 | 116 | ls.getMetatableAux(className); 117 | ls.setMetatable(-2); 118 | 119 | return true; 120 | } 121 | } -------------------------------------------------------------------------------- /lib/widget/column.dart: -------------------------------------------------------------------------------- 1 | 2 | import 'package:flutter/widgets.dart'; 3 | import 'package:flutter_lua_dardo/widget/_flex.dart'; 4 | import 'package:lua_dardo/lua.dart'; 5 | 6 | class FlutterColumn{ 7 | 8 | static const Map _columnFunc = { 9 | "new": _newColumn, 10 | }; 11 | 12 | static int _newColumn(LuaState ls){ 13 | if(FlutterFlex.newFlex(ls,"FlutterColumn")){ 14 | return 1; 15 | } 16 | throw Exception("Failed to instantiate FlutterFlex!"); 17 | } 18 | 19 | static int _openRowLib(LuaState ls) { 20 | FlutterFlex.openFlex(ls); 21 | 22 | ls.newLib(_columnFunc); 23 | return 1; 24 | } 25 | 26 | static void require(LuaState ls){ 27 | ls.requireF("Column", _openRowLib, true); 28 | ls.pop(1); 29 | } 30 | } -------------------------------------------------------------------------------- /lib/widget/enumerate.dart: -------------------------------------------------------------------------------- 1 | 2 | import 'package:flutter/widgets.dart'; 3 | import 'package:lua_dardo/lua.dart'; 4 | 5 | class FlutterMainAxisSize{ 6 | static const List _members = [ 7 | "min", 8 | "max", 9 | ]; 10 | 11 | static void require(LuaState ls){ 12 | ls.newTable(); 13 | 14 | for(var i = 0;i<_members.length ;i++){ 15 | ls.pushInteger(i); 16 | ls.setField(-2, _members[i]); 17 | } 18 | 19 | ls.setGlobal("MainAxisSize"); 20 | } 21 | 22 | static MainAxisSize get(int idx){ 23 | if(idx == null || idx < 0 || idx >= _members.length){ 24 | return MainAxisSize.max; 25 | } 26 | return MainAxisSize.values[idx]; 27 | } 28 | } 29 | 30 | class FlutterMainAxisAlign{ 31 | static const List _members = [ 32 | "start", 33 | "end", 34 | "center", 35 | "spaceBetween", 36 | "spaceAround", 37 | "spaceEvenly" 38 | ]; 39 | 40 | static void require(LuaState ls){ 41 | ls.newTable(); 42 | 43 | for(var i = 0;i<_members.length ;i++){ 44 | ls.pushInteger(i); 45 | ls.setField(-2, _members[i]); 46 | } 47 | 48 | ls.setGlobal("MainAxisAlign"); 49 | } 50 | 51 | static MainAxisAlignment get(int idx){ 52 | if(idx == null || idx < 0 || idx >= _members.length){ 53 | return MainAxisAlignment.start; 54 | } 55 | return MainAxisAlignment.values[idx]; 56 | } 57 | } 58 | 59 | class FlutterCrossAxisAlign{ 60 | 61 | static const List _members = [ 62 | "start", 63 | "end", 64 | "center", 65 | "stretch", 66 | "baseline" 67 | ]; 68 | 69 | static void require(LuaState ls){ 70 | ls.newTable(); 71 | 72 | for(var i = 0;i<_members.length ;i++){ 73 | ls.pushInteger(i); 74 | ls.setField(-2, _members[i]); 75 | } 76 | 77 | ls.setGlobal("CrossAxisAlign"); 78 | } 79 | 80 | static CrossAxisAlignment get(int idx){ 81 | if(idx == null || idx < 0 || idx >= _members.length){ 82 | return CrossAxisAlignment.center; 83 | } 84 | return CrossAxisAlignment.values[idx]; 85 | } 86 | } 87 | 88 | class FlutterBoxFit{ 89 | static const List _members = [ 90 | "fill", 91 | "contain", 92 | "cover", 93 | "fitWidth", 94 | "fitHeight", 95 | "none", 96 | "scaleDown", 97 | ]; 98 | 99 | static void require(LuaState ls){ 100 | ls.newTable(); 101 | 102 | for(var i = 0;i<_members.length ;i++){ 103 | ls.pushInteger(i); 104 | ls.setField(-2, _members[i]); 105 | } 106 | 107 | ls.setGlobal("BoxFit"); 108 | } 109 | 110 | static BoxFit get(int idx){ 111 | if(idx == null || idx < 0 || idx >= _members.length){ 112 | return BoxFit.fill; 113 | } 114 | return BoxFit.values[idx]; 115 | } 116 | } -------------------------------------------------------------------------------- /lib/widget/gesture_detector.dart: -------------------------------------------------------------------------------- 1 | 2 | import 'package:flutter/widgets.dart'; 3 | import 'package:flutter_lua_dardo/widget/parameter_exception.dart'; 4 | import 'package:lua_dardo/lua.dart'; 5 | 6 | class FlutterGestureDetector{ 7 | static const Map _gestureDetectorFunc = { 8 | "new": _newGestureDetector, 9 | }; 10 | 11 | static const Map _gestureMembers = { 12 | "id":null}; 13 | 14 | static int _newGestureDetector(LuaState ls){ 15 | int onTapId = -1; 16 | 17 | if(ls.getTop()>0){ 18 | var fieldType = ls.getField(-1, "onTap"); 19 | if(fieldType == LuaType.luaFunction){ 20 | onTapId = ls.ref(lua_registryindex); 21 | }else if(fieldType == LuaType.luaNil){ 22 | ls.pop(1); 23 | }else{ 24 | throw ParameterError(name: 'onTap', 25 | type: ls.typeName(fieldType), 26 | expected: "Function", 27 | source: "FlutterGestureDetector _newGestureDetector" 28 | ); 29 | } 30 | 31 | Widget child; 32 | fieldType = ls.getField(-1, "child"); 33 | if(fieldType == LuaType.luaUserdata){ 34 | child = ls.toUserdata(-1).data as Widget; 35 | ls.pop(1); 36 | }else if(fieldType == LuaType.luaNil){ 37 | ls.pop(1); 38 | }else{ 39 | throw ParameterError(name: 'child', 40 | type: ls.typeName(fieldType), 41 | expected: "Widget", 42 | source: "FlutterGestureDetector _newGestureDetector" 43 | ); 44 | } 45 | 46 | Userdata u = ls.newUserdata(); 47 | u.data = GestureDetector(child: child,onTap: (){ 48 | if(onTapId != -1){ 49 | ls.rawGetI(lua_registryindex, onTapId); 50 | ls.pCall(0, 0, 1); 51 | } 52 | }); 53 | ls.getMetatableAux('GestureDetectorClass'); 54 | ls.setMetatable(-2); 55 | } 56 | return 1; 57 | } 58 | 59 | static int _openTextLib(LuaState ls) { 60 | ls.newMetatable("GestureDetectorClass"); 61 | ls.pushValue(-1); 62 | ls.setField(-2, "__index"); 63 | ls.setFuncs(_gestureMembers, 0); 64 | 65 | ls.newLib(_gestureDetectorFunc); 66 | return 1; 67 | } 68 | 69 | static void require(LuaState ls){ 70 | ls.requireF("GestureDetector", _openTextLib, true); 71 | ls.pop(1); 72 | } 73 | } -------------------------------------------------------------------------------- /lib/widget/image.dart: -------------------------------------------------------------------------------- 1 | 2 | import 'dart:io'; 3 | 4 | import 'package:flutter/widgets.dart'; 5 | import 'package:flutter_lua_dardo/widget/enumerate.dart'; 6 | import 'package:flutter_lua_dardo/widget/parameter_exception.dart'; 7 | import 'package:lua_dardo/lua.dart'; 8 | 9 | 10 | enum _Image{ 11 | asset, 12 | file, 13 | memory, 14 | network 15 | } 16 | 17 | class FlutterImage{ 18 | static const Map _TextWrap = { 19 | "asset": _asset, 20 | "file":_file, 21 | "memory":_memory, 22 | "network":_network, 23 | "new":null, 24 | }; 25 | 26 | static const Map _TextMembers = { 27 | "id": null}; 28 | 29 | static int _asset(LuaState ls){ 30 | return _init(ls, _Image.asset); 31 | } 32 | 33 | static int _file(LuaState ls){ 34 | return _init(ls, _Image.file); 35 | } 36 | 37 | static int _memory(LuaState ls){ 38 | return 1; 39 | } 40 | 41 | static int _network(LuaState ls){ 42 | return _init(ls, _Image.network); 43 | } 44 | 45 | static int _init(LuaState ls,_Image type){ 46 | if(ls.getTop()>0){ 47 | double width, height; 48 | BoxFit fit; 49 | 50 | var fieldType = ls.getField(-1, "width"); 51 | if(fieldType == LuaType.luaNumber){ 52 | width = ls.toNumber(-1); 53 | ls.pop(1); 54 | }else if(fieldType == LuaType.luaNil){ 55 | ls.pop(1); 56 | }else{ 57 | throw ParameterError(name: 'width', 58 | type: ls.typeName(fieldType), 59 | expected: "double", 60 | source: "FlutterImage _assetText" 61 | ); 62 | } 63 | 64 | fieldType = ls.getField(-1, "height"); 65 | if(fieldType == LuaType.luaNumber){ 66 | height = ls.toNumber(-1); 67 | ls.pop(1); 68 | }else if(fieldType == LuaType.luaNil){ 69 | ls.pop(1); 70 | }else{ 71 | throw ParameterError(name: 'height', 72 | type: ls.typeName(fieldType), 73 | expected: "double", 74 | source: "FlutterImage _assetText" 75 | ); 76 | } 77 | 78 | fieldType = ls.getField(-1, "fit"); 79 | if(fieldType == LuaType.luaNumber){ 80 | fit = FlutterBoxFit.get(ls.toIntegerX(-1)); 81 | ls.pop(1); 82 | }else if(fieldType == LuaType.luaNil){ 83 | ls.pop(1); 84 | }else{ 85 | throw ParameterError(name: 'fit', 86 | type: ls.typeName(fieldType), 87 | expected: "double", 88 | source: "FlutterImage _assetText" 89 | ); 90 | } 91 | 92 | String first; 93 | if(ls.isString(-1)){ 94 | first = ls.toStr(-1); 95 | }else if(ls.isTable(-1)){ 96 | first = ls.toStr(-2); 97 | } else{ 98 | throw ParameterError(name: 'first', 99 | type: ls.typeName(fieldType), 100 | expected: "String", 101 | source: "FlutterImage _assetText" 102 | ); 103 | } 104 | 105 | Userdata u = ls.newUserdata(); 106 | switch(type){ 107 | case _Image.asset: 108 | u.data = Image.asset(first,width: width,height: height,fit: fit); 109 | break; 110 | case _Image.file: 111 | u.data = Image.file(File(first),width: width,height: height,fit: fit); 112 | break; 113 | case _Image.network: 114 | u.data = Image.network(first,width: width,height: height,fit: fit); 115 | break; 116 | } 117 | 118 | ls.getMetatableAux('ImageClass'); 119 | ls.setMetatable(-2); 120 | } 121 | return 1; 122 | } 123 | 124 | static int _openImageLib(LuaState ls) { 125 | ls.newMetatable("ImageClass"); 126 | ls.pushValue(-1); 127 | ls.setField(-2, "__index"); 128 | ls.setFuncs(_TextMembers, 0); 129 | 130 | ls.newLib(_TextWrap); 131 | return 1; 132 | } 133 | 134 | static void require(LuaState ls){ 135 | ls.requireF("Image", _openImageLib, true); 136 | ls.pop(1); 137 | } 138 | } -------------------------------------------------------------------------------- /lib/widget/parameter_exception.dart: -------------------------------------------------------------------------------- 1 | 2 | 3 | import 'package:flutter/cupertino.dart'; 4 | 5 | class ParameterError implements Exception{ 6 | 7 | String name; 8 | String type; 9 | String source; 10 | String expected; 11 | 12 | ParameterError({ 13 | @required this.name, 14 | @required this.type, 15 | @required this.expected, 16 | @required this.source, 17 | }); 18 | 19 | @override 20 | String toString() { 21 | return "The parameter'$name' is wrong, its type is $type, " 22 | "and the expected type is $expected. The error occurs at $source"; 23 | } 24 | } -------------------------------------------------------------------------------- /lib/widget/row.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:flutter_lua_dardo/widget/_flex.dart'; 3 | import 'package:lua_dardo/lua.dart'; 4 | 5 | 6 | 7 | 8 | class FlutterRow{ 9 | 10 | static const Map _rowFunc = { 11 | "new": _newRow, 12 | }; 13 | 14 | static const Map _const = { 15 | 16 | }; 17 | 18 | static int _newRow(LuaState ls){ 19 | if(FlutterFlex.newFlex(ls,"FlutterRow")){ 20 | return 1; 21 | } 22 | throw Exception("Failed to instantiate FlutterFlex!"); 23 | } 24 | 25 | static int _openRowLib(LuaState ls) { 26 | FlutterFlex.openFlex(ls); 27 | 28 | ls.newLib(_rowFunc); 29 | return 1; 30 | } 31 | 32 | static void require(LuaState ls){ 33 | ls.requireF("Row", _openRowLib, true); 34 | ls.pop(1); 35 | } 36 | } -------------------------------------------------------------------------------- /lib/widget/text.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:lua_dardo/lua.dart'; 3 | 4 | 5 | 6 | class FlutterText{ 7 | static const Map _TextWrap = { 8 | "new": _newText, 9 | "rich":null 10 | }; 11 | 12 | static const Map _TextMembers = { 13 | "id": null}; 14 | 15 | static int _newText(LuaState ls){ 16 | if(ls.getTop()>0){ 17 | var data = ls.toStr(-1); 18 | Userdata u = ls.newUserdata(); 19 | u.data = Text(data); 20 | ls.getMetatableAux('TextClass'); 21 | ls.setMetatable(-2); 22 | } 23 | 24 | return 1; 25 | } 26 | 27 | static int _openTextLib(LuaState ls) { 28 | ls.newMetatable("TextClass"); 29 | ls.pushValue(-1); 30 | ls.setField(-2, "__index"); 31 | ls.setFuncs(_TextMembers, 0); 32 | 33 | ls.newLib(_TextWrap); 34 | return 1; 35 | } 36 | 37 | static void require(LuaState ls){ 38 | ls.requireF("Text", _openTextLib, true); 39 | ls.pop(1); 40 | } 41 | } -------------------------------------------------------------------------------- /pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://dart.dev/tools/pub/glossary#lockfile 3 | packages: 4 | async: 5 | dependency: transitive 6 | description: 7 | name: async 8 | url: "https://pub.flutter-io.cn" 9 | source: hosted 10 | version: "2.5.0" 11 | boolean_selector: 12 | dependency: transitive 13 | description: 14 | name: boolean_selector 15 | url: "https://pub.flutter-io.cn" 16 | source: hosted 17 | version: "2.1.0" 18 | characters: 19 | dependency: transitive 20 | description: 21 | name: characters 22 | url: "https://pub.flutter-io.cn" 23 | source: hosted 24 | version: "1.1.0" 25 | charcode: 26 | dependency: transitive 27 | description: 28 | name: charcode 29 | url: "https://pub.flutter-io.cn" 30 | source: hosted 31 | version: "1.2.0" 32 | clock: 33 | dependency: transitive 34 | description: 35 | name: clock 36 | url: "https://pub.flutter-io.cn" 37 | source: hosted 38 | version: "1.1.0" 39 | collection: 40 | dependency: transitive 41 | description: 42 | name: collection 43 | url: "https://pub.flutter-io.cn" 44 | source: hosted 45 | version: "1.15.0" 46 | fake_async: 47 | dependency: transitive 48 | description: 49 | name: fake_async 50 | url: "https://pub.flutter-io.cn" 51 | source: hosted 52 | version: "1.2.0" 53 | flutter: 54 | dependency: "direct main" 55 | description: flutter 56 | source: sdk 57 | version: "0.0.0" 58 | flutter_test: 59 | dependency: "direct dev" 60 | description: flutter 61 | source: sdk 62 | version: "0.0.0" 63 | lua_dardo: 64 | dependency: "direct main" 65 | description: 66 | name: lua_dardo 67 | url: "https://pub.flutter-io.cn" 68 | source: hosted 69 | version: "0.0.3" 70 | matcher: 71 | dependency: transitive 72 | description: 73 | name: matcher 74 | url: "https://pub.flutter-io.cn" 75 | source: hosted 76 | version: "0.12.10" 77 | meta: 78 | dependency: transitive 79 | description: 80 | name: meta 81 | url: "https://pub.flutter-io.cn" 82 | source: hosted 83 | version: "1.3.0" 84 | path: 85 | dependency: transitive 86 | description: 87 | name: path 88 | url: "https://pub.flutter-io.cn" 89 | source: hosted 90 | version: "1.8.0" 91 | sky_engine: 92 | dependency: transitive 93 | description: flutter 94 | source: sdk 95 | version: "0.0.99" 96 | source_span: 97 | dependency: transitive 98 | description: 99 | name: source_span 100 | url: "https://pub.flutter-io.cn" 101 | source: hosted 102 | version: "1.8.0" 103 | sprintf: 104 | dependency: transitive 105 | description: 106 | name: sprintf 107 | url: "https://pub.flutter-io.cn" 108 | source: hosted 109 | version: "5.0.0" 110 | stack_trace: 111 | dependency: transitive 112 | description: 113 | name: stack_trace 114 | url: "https://pub.flutter-io.cn" 115 | source: hosted 116 | version: "1.10.0" 117 | stream_channel: 118 | dependency: transitive 119 | description: 120 | name: stream_channel 121 | url: "https://pub.flutter-io.cn" 122 | source: hosted 123 | version: "2.1.0" 124 | string_scanner: 125 | dependency: transitive 126 | description: 127 | name: string_scanner 128 | url: "https://pub.flutter-io.cn" 129 | source: hosted 130 | version: "1.1.0" 131 | term_glyph: 132 | dependency: transitive 133 | description: 134 | name: term_glyph 135 | url: "https://pub.flutter-io.cn" 136 | source: hosted 137 | version: "1.2.0" 138 | test_api: 139 | dependency: transitive 140 | description: 141 | name: test_api 142 | url: "https://pub.flutter-io.cn" 143 | source: hosted 144 | version: "0.2.19" 145 | typed_data: 146 | dependency: transitive 147 | description: 148 | name: typed_data 149 | url: "https://pub.flutter-io.cn" 150 | source: hosted 151 | version: "1.3.0" 152 | vector_math: 153 | dependency: transitive 154 | description: 155 | name: vector_math 156 | url: "https://pub.flutter-io.cn" 157 | source: hosted 158 | version: "2.1.0" 159 | sdks: 160 | dart: ">=2.12.0-0.0 <3.0.0" 161 | flutter: ">=1.17.0" 162 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flutter_lua_dardo 2 | description: A new Flutter package. 3 | version: 0.0.2 4 | homepage: https://github.com/arcticfox1919/flutter_lua_dardo 5 | 6 | environment: 7 | sdk: ">=2.7.0 <3.0.0" 8 | flutter: ">=1.17.0" 9 | 10 | dependencies: 11 | flutter: 12 | sdk: flutter 13 | lua_dardo: ^0.0.3 14 | 15 | dev_dependencies: 16 | flutter_test: 17 | sdk: flutter 18 | 19 | # For information on the generic Dart part of this file, see the 20 | # following page: https://dart.dev/tools/pub/pubspec 21 | 22 | # The following section is specific to Flutter. 23 | flutter: 24 | 25 | # To add assets to your package, add an assets section, like this: 26 | # assets: 27 | # - images/a_dot_burr.jpeg 28 | # - images/a_dot_ham.jpeg 29 | # 30 | # For details regarding assets in packages, see 31 | # https://flutter.dev/assets-and-images/#from-packages 32 | # 33 | # An image asset can refer to one or more resolution-specific "variants", see 34 | # https://flutter.dev/assets-and-images/#resolution-aware. 35 | 36 | # To add custom fonts to your package, add a fonts section here, 37 | # in this "flutter" section. Each entry in this list should have a 38 | # "family" key with the font family name, and a "fonts" key with a 39 | # list giving the asset and other descriptors for the font. For 40 | # example: 41 | # fonts: 42 | # - family: Schyler 43 | # fonts: 44 | # - asset: fonts/Schyler-Regular.ttf 45 | # - asset: fonts/Schyler-Italic.ttf 46 | # style: italic 47 | # - family: Trajan Pro 48 | # fonts: 49 | # - asset: fonts/TrajanPro.ttf 50 | # - asset: fonts/TrajanPro_Bold.ttf 51 | # weight: 700 52 | # 53 | # For details regarding fonts in packages, see 54 | # https://flutter.dev/custom-fonts/#from-packages 55 | -------------------------------------------------------------------------------- /test/flutter_lua_test.dart: -------------------------------------------------------------------------------- 1 | 2 | import 'package:lua_dardo/lua.dart'; 3 | import 'package:lua_dardo/debug.dart'; 4 | import 'person.dart'; 5 | 6 | /// 7 | /// Example of binding Dart class to Lua 8 | /// 9 | void main() { 10 | LuaState ls = LuaState.newState(); 11 | ls.openLibs(); 12 | Person.require(ls); 13 | ls.loadFile("test.lua"); 14 | // check the stack,only for debugging 15 | ls.printStack(); 16 | ls.pCall(0, 0, 1); 17 | } 18 | -------------------------------------------------------------------------------- /test/person.dart: -------------------------------------------------------------------------------- 1 | import 'package:lua_dardo/lua.dart'; 2 | 3 | 4 | 5 | class Person { 6 | static const Map _registry = { 7 | "new": _newPerson, 8 | }; 9 | 10 | static const Map _personMember = {"sayHi": _sayHi}; 11 | 12 | static int _openPersonLib(LuaState ls) { 13 | ls.newMetatable("PersonClass"); 14 | ls.pushValue(-1); 15 | ls.setField(-2, "__index"); 16 | ls.setFuncs(_personMember, 0); 17 | 18 | ls.newLib(_registry); 19 | return 1; 20 | } 21 | 22 | static int _newPerson(LuaState ls) { 23 | if(ls.getTop()>0) { 24 | var data = ls.toStr(-1); 25 | print("new : $data"); 26 | } 27 | Userdata u = ls.newUserdata(); 28 | u.data = Person(); 29 | ls.getMetatableAux('PersonClass'); 30 | ls.setMetatable(-2); 31 | return 1; 32 | } 33 | 34 | static int _sayHi(LuaState ls) { 35 | Person p = ls.toUserdata(1).data; 36 | if (ls.isString(-1)) { 37 | p.sayHi(ls.toStr(-1)); 38 | } 39 | return 0; 40 | } 41 | 42 | static void require(LuaState ls){ 43 | ls.requireF("Person", _openPersonLib, true); 44 | ls.pop(1); 45 | } 46 | 47 | 48 | void sayHi(String name) { 49 | print("Hi,$name"); 50 | } 51 | } -------------------------------------------------------------------------------- /test/test.lua: -------------------------------------------------------------------------------- 1 | local p = Person:create() 2 | p:sayHi("Bruce") 3 | --------------------------------------------------------------------------------