add provider

This commit is contained in:
open-trade 2020-11-15 20:04:05 +08:00
parent 489ccdb53f
commit e4681f7d9a
5 changed files with 203 additions and 125 deletions

View File

@ -0,0 +1,48 @@
import 'package:flutter/material.dart';
import 'package:ffi/ffi.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'dart:ffi';
import 'dart:async';
class HexColor extends Color {
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll('#', '');
if (hexColor.length == 6) {
hexColor = 'FF' + hexColor;
}
return int.parse(hexColor, radix: 16);
}
}
// https://juejin.im/post/6844903864852807694
class FfiModel with ChangeNotifier {
var _connectRemote;
FfiModel() {
initialzeFFI();
}
void addRemote() {
notifyListeners();
}
void connect(String id) {
_connectRemote(Utf8.toUtf8(id));
}
Future<Null> initialzeFFI() async {
final dylib = Platform.isAndroid
? DynamicLibrary.open('librustdesk.so')
: DynamicLibrary.process();
final initialize = dylib.lookupFunction<Void Function(Pointer<Utf8>),
void Function(Pointer<Utf8>)>('initialize');
_connectRemote = dylib.lookupFunction<Void Function(Pointer<Utf8>),
void Function(Pointer<Utf8>)>('connect_remote');
final dir = (await getApplicationDocumentsDirectory()).path;
initialize(Utf8.toUtf8(dir));
notifyListeners();
}
}

View File

@ -0,0 +1,122 @@
import 'package:flutter/material.dart';
import 'common.dart';
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget getSearchBarUI() {
return Padding(
padding: const EdgeInsets.only(top: 8.0, left: 18),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.75,
height: 64,
child: Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8),
child: Container(
decoration: BoxDecoration(
color: HexColor('#F8FAFB'),
borderRadius: const BorderRadius.only(
bottomRight: Radius.circular(13.0),
bottomLeft: Radius.circular(13.0),
topLeft: Radius.circular(13.0),
topRight: Radius.circular(13.0),
),
),
child: Row(
children: <Widget>[
Expanded(
child: Container(
padding: const EdgeInsets.only(left: 16, right: 16),
child: TextFormField(
style: TextStyle(
fontFamily: 'WorkSans',
fontWeight: FontWeight.bold,
fontSize: 16,
color: Color(0xFF00B6F0),
),
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: 'Search for course',
border: InputBorder.none,
helperStyle: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: HexColor('#B9BABC'),
),
labelStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
letterSpacing: 0.2,
color: HexColor('#B9BABC'),
),
),
onEditingComplete: () {},
),
),
),
SizedBox(
width: 60,
height: 60,
child: Icon(Icons.search, color: HexColor('#B9BABC')),
)
],
),
),
),
),
const Expanded(
child: SizedBox(),
)
],
),
);
}
}

View File

@ -1,132 +1,25 @@
import 'package:ffi/ffi.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'dart:ffi';
import 'dart:async';
import 'package:provider/provider.dart';
import 'common.dart';
import 'home_page.dart';
void main() {
// final connect_remote = dylib.lookupFunction<Void Function(Pointer<Utf8>), void Function(Pointer<Utf8>)>('connect_remote');
// connect_remote(Utf8.toUtf8('test'));
runApp(MyApp());
runApp(_App());
}
Future<Null> initialzeFFI() async {
final dylib = Platform.isAndroid ? DynamicLibrary.open('librustdesk.so') : DynamicLibrary.process();
String dir = (await getApplicationDocumentsDirectory()).path;
final initialize = dylib.lookupFunction<Void Function(Pointer<Utf8>), void Function(Pointer<Utf8>)>('initialize');
initialize(Utf8.toUtf8(dir));
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
class _App extends StatelessWidget {
@override
Widget build(BuildContext context) {
initialzeFFI();
return MaterialApp(
title: 'Flutter Demo',
var ffi = FfiModel();
return ChangeNotifierProvider.value(
value: ffi,
child: MaterialApp(
title: 'RustDesk',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
home: HomePage(title: 'RustDesk'),
));
}
}

View File

@ -102,6 +102,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0-nullsafety.3"
nested:
dependency: transitive
description:
name: nested
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.4"
path:
dependency: transitive
description:
@ -165,6 +172,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.13"
provider:
dependency: "direct main"
description:
name: provider
url: "https://pub.dartlang.org"
source: hosted
version: "4.3.2+2"
sky_engine:
dependency: transitive
description: flutter
@ -232,7 +246,7 @@ packages:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.3"
version: "1.7.4"
xdg_directories:
dependency: transitive
description:
@ -242,4 +256,4 @@ packages:
version: "0.1.2"
sdks:
dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
flutter: ">=1.16.0 <2.0.0"

View File

@ -30,6 +30,7 @@ dependencies:
cupertino_icons: ^1.0.0
ffi: ^0.1.3
path_provider: ^1.6.24
provider: ^4.3.2+2
dev_dependencies:
flutter_test: