mirror of
https://github.com/MAGICGrants/flutter_libsparkmobile.git
synced 2026-01-09 21:17:56 -05:00
bind library to dart and demo in app
This commit is contained in:
@@ -8,12 +8,17 @@ sudo apt install libboost-thread-dev
|
||||
```
|
||||
|
||||
## Build
|
||||
Build:
|
||||
Build the native library for your platform like:
|
||||
```sh
|
||||
cd flutter_libsparkmobile/scripts/linux
|
||||
./build_all.sh
|
||||
```
|
||||
|
||||
## Example
|
||||
See the [example](example) directory for a Flutter app that uses `flutter_libsparkmobile`.
|
||||
|
||||
You must have the native library built for your platform before running the example app. See the [Build](#build) section above.
|
||||
|
||||
## Development
|
||||
### Bindings generation (`dart ffigen`)
|
||||
Bindings are generated using [ffigen](https://pub.dev/packages/ffigen). All of the individual build scripts in your platform's `build_all.sh` script must be run up to and including `copyCMakeLists.sh` in order for the header referenced in `pubspec.yaml`'s `ffigen` section to exist.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_libsparkmobile/flutter_libsparkmobile.dart';
|
||||
|
||||
@@ -17,7 +19,26 @@ class MyApp extends StatefulWidget {
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
String _platformVersion = 'Unknown';
|
||||
final _flutterLibsparkmobilePlugin = FlutterLibsparkmobile();
|
||||
final FlutterLibsparkmobile _flutterLibsparkmobilePlugin;
|
||||
|
||||
_MyAppState()
|
||||
: _flutterLibsparkmobilePlugin = FlutterLibsparkmobile(_loadLibrary());
|
||||
|
||||
static DynamicLibrary _loadLibrary() {
|
||||
if (Platform.isLinux) {
|
||||
return DynamicLibrary.open('libsparkmobile.so');
|
||||
} else if (Platform.isAndroid) {
|
||||
// return DynamicLibrary.open('libsparkmobile.so');
|
||||
} else if (Platform.isIOS) {
|
||||
// return DynamicLibrary.open('libsparkmobile.dylib');
|
||||
} else if (Platform.isMacOS) {
|
||||
// return DynamicLibrary.open('libsparkmobile.dylib');
|
||||
} else if (Platform.isWindows) {
|
||||
// return DynamicLibrary.open('sparkmobile.dll');
|
||||
}
|
||||
// Optionally throw an error or handle other platforms
|
||||
throw UnsupportedError('This platform is not supported');
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -32,7 +53,8 @@ class _MyAppState extends State<MyApp> {
|
||||
// We also handle the message potentially returning null.
|
||||
try {
|
||||
platformVersion =
|
||||
await _flutterLibsparkmobilePlugin.getPlatformVersion() ?? 'Unknown platform version';
|
||||
await _flutterLibsparkmobilePlugin.getPlatformVersion() ??
|
||||
'Unknown platform version';
|
||||
} on PlatformException {
|
||||
platformVersion = 'Failed to get platform version.';
|
||||
}
|
||||
@@ -55,7 +77,23 @@ class _MyAppState extends State<MyApp> {
|
||||
title: const Text('Plugin example app'),
|
||||
),
|
||||
body: Center(
|
||||
child: Text('Running on: $_platformVersion\n'),
|
||||
child: Column(
|
||||
children: [
|
||||
Text('Running on: $_platformVersion\n'),
|
||||
// A button that, when pressed, generates a new Spark spend key.
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
final spendKey = await _flutterLibsparkmobilePlugin
|
||||
.generateSpendKey()
|
||||
.catchError((error) {
|
||||
print(error);
|
||||
});
|
||||
print(spendKey);
|
||||
},
|
||||
child: const Text('Generate spend key'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -119,6 +119,10 @@ install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}
|
||||
install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
|
||||
COMPONENT Runtime)
|
||||
|
||||
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/../../scripts/linux/build/libsparkmobile.so" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
|
||||
COMPONENT Runtime)
|
||||
# Must have ran build script(s) in order for this library to exist.
|
||||
|
||||
foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES})
|
||||
install(FILES "${bundled_library}"
|
||||
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
|
||||
|
||||
@@ -1,8 +1,27 @@
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
import 'flutter_libsparkmobile_bindings.dart';
|
||||
import 'flutter_libsparkmobile_platform_interface.dart';
|
||||
|
||||
class FlutterLibsparkmobile {
|
||||
final SparkMobileBindings _bindings;
|
||||
|
||||
FlutterLibsparkmobile(DynamicLibrary dynamicLibrary)
|
||||
: _bindings = SparkMobileBindings(dynamicLibrary);
|
||||
|
||||
Future<String?> getPlatformVersion() {
|
||||
return FlutterLibsparkmobilePlatform.instance.getPlatformVersion();
|
||||
}
|
||||
|
||||
// SparkMobileBindings methods
|
||||
Future<String> generateSpendKey() {
|
||||
Pointer<Char> key = _bindings.generateSpendKey();
|
||||
|
||||
// Cast the Pointer<Char> to a Dart String.
|
||||
final keyString = key.cast<Utf8>().toDartString();
|
||||
|
||||
return Future.value(keyString);
|
||||
}
|
||||
}
|
||||
|
||||
34
lib/flutter_libsparkmobile_bindings.dart
Normal file
34
lib/flutter_libsparkmobile_bindings.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
// ignore_for_file: camel_case_types, non_constant_identifier_names, unused_element, unused_field, return_of_invalid_type, void_checks, annotate_overrides, no_leading_underscores_for_local_identifiers, library_private_types_in_public_api
|
||||
|
||||
// AUTO GENERATED FILE, DO NOT EDIT.
|
||||
//
|
||||
// Generated by `package:ffigen`.
|
||||
// ignore_for_file: type=lint
|
||||
import 'dart:ffi' as ffi;
|
||||
|
||||
/// Bindings for sparkmobile.
|
||||
class SparkMobileBindings {
|
||||
/// Holds the symbol lookup function.
|
||||
final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
|
||||
_lookup;
|
||||
|
||||
/// The symbols are looked up in [dynamicLibrary].
|
||||
SparkMobileBindings(ffi.DynamicLibrary dynamicLibrary)
|
||||
: _lookup = dynamicLibrary.lookup;
|
||||
|
||||
/// The symbols are looked up with [lookup].
|
||||
SparkMobileBindings.fromLookup(
|
||||
ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
|
||||
lookup)
|
||||
: _lookup = lookup;
|
||||
|
||||
ffi.Pointer<ffi.Char> generateSpendKey() {
|
||||
return _generateSpendKey();
|
||||
}
|
||||
|
||||
late final _generateSpendKeyPtr =
|
||||
_lookup<ffi.NativeFunction<ffi.Pointer<ffi.Char> Function()>>(
|
||||
'generateSpendKey');
|
||||
late final _generateSpendKey =
|
||||
_generateSpendKeyPtr.asFunction<ffi.Pointer<ffi.Char> Function()>();
|
||||
}
|
||||
@@ -8,10 +8,12 @@ environment:
|
||||
flutter: ">=3.3.0"
|
||||
|
||||
dependencies:
|
||||
ffi: ^2.1.0
|
||||
flutter:
|
||||
sdk: flutter
|
||||
plugin_platform_interface: ^2.0.2
|
||||
test: ^1.24.1
|
||||
typed_data: ^1.3.2
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
@@ -80,10 +82,10 @@ flutter:
|
||||
# https://flutter.dev/custom-fonts/#from-packages
|
||||
|
||||
ffigen:
|
||||
name: flutter_libsparkmobile
|
||||
name: SparkMobileBindings
|
||||
description: Bindings for sparkmobile.
|
||||
language: c
|
||||
output: 'lib/sparkmobile_bindings.dart'
|
||||
output: 'lib/flutter_libsparkmobile_bindings.dart'
|
||||
headers:
|
||||
entry-points:
|
||||
- 'scripts/linux/sparkmobile/src/dart_interface.h'
|
||||
|
||||
Reference in New Issue
Block a user