rustdesk/lib/home_page.dart

387 lines
13 KiB
Dart
Raw Normal View History

2020-11-15 20:04:05 +08:00
import 'package:flutter/material.dart';
2020-11-16 01:13:26 +08:00
import 'package:provider/provider.dart';
2020-11-26 00:33:45 +08:00
import 'package:tuple/tuple.dart';
2020-11-30 11:35:43 +08:00
import 'package:package_info/package_info.dart';
import 'package:url_launcher/url_launcher.dart';
2020-11-30 15:04:59 +08:00
import 'dart:async';
2020-11-15 20:04:05 +08:00
import 'common.dart';
2022-02-02 17:25:56 +08:00
import 'main.dart';
2020-11-19 00:32:46 +08:00
import 'model.dart';
import 'remote_page.dart';
2021-08-14 14:14:01 +08:00
import 'dart:io';
2020-11-15 20:04:05 +08:00
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _idController = TextEditingController();
2020-11-30 15:04:59 +08:00
var _updateUrl = '';
@override
void initState() {
super.initState();
2022-02-02 17:25:56 +08:00
nowCtx = context;
2021-08-14 14:14:01 +08:00
if (Platform.isAndroid) {
Timer(Duration(seconds: 5), () {
_updateUrl = FFI.getByName('software_update_url');
if (_updateUrl.isNotEmpty) setState(() {});
});
}
2020-11-30 15:04:59 +08:00
}
2020-11-15 20:04:05 +08:00
@override
Widget build(BuildContext context) {
2020-11-17 11:12:55 +08:00
Provider.of<FfiModel>(context);
2020-11-18 12:49:43 +08:00
if (_idController.text.isEmpty) _idController.text = FFI.getId();
2020-11-15 20:04:05 +08:00
// This method is rerun every time setState is called
return Scaffold(
2020-11-19 18:22:06 +08:00
backgroundColor: MyTheme.grayBg,
2020-11-16 01:13:26 +08:00
appBar: AppBar(
2020-11-26 00:33:45 +08:00
centerTitle: true,
actions: [
IconButton(
icon: Icon(Icons.more_vert),
onPressed: () {
() async {
var value = await showMenu(
context: context,
position: RelativeRect.fromLTRB(3000, 70, 3000, 70),
items: [
PopupMenuItem<String>(
2021-08-06 22:45:45 +08:00
child: Text(translate('ID Server')),
2022-01-23 21:37:19 +08:00
value: 'id_server'),
2022-02-02 17:25:56 +08:00
Platform.isAndroid
? PopupMenuItem<String>(
child: Text(translate('Share My Screen')),
value: 'server')
: null,
2020-11-30 11:35:43 +08:00
PopupMenuItem<String>(
child: Text(translate('About') + ' RustDesk'),
value: 'about'),
2020-11-26 00:33:45 +08:00
],
elevation: 8,
);
2022-01-23 21:37:19 +08:00
if (value == 'id_server') {
2020-11-26 00:33:45 +08:00
showServer(context);
2022-01-23 21:37:19 +08:00
} else if (value == 'server') {
Navigator.pushNamed(context, "server_page");
2020-11-30 11:35:43 +08:00
} else if (value == 'about') {
showAbout(context);
2020-11-26 00:33:45 +08:00
}
}();
})
],
2020-11-16 01:13:26 +08:00
title: Text(widget.title),
2020-11-15 20:04:05 +08:00
),
2020-11-26 00:06:57 +08:00
body: SingleChildScrollView(
2020-11-16 01:13:26 +08:00
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
2020-11-30 15:04:59 +08:00
_updateUrl.isEmpty
? SizedBox(height: 0)
: InkWell(
onTap: () async {
final url = _updateUrl + '.apk';
if (await canLaunch(url)) {
await launch(url);
}
},
child: Container(
alignment: AlignmentDirectional.center,
width: double.infinity,
color: Colors.pinkAccent,
padding: EdgeInsets.symmetric(vertical: 12),
2021-08-14 14:14:01 +08:00
child: Text(translate('Download new version'),
2020-11-30 15:04:59 +08:00
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold)))),
2020-11-16 01:13:26 +08:00
getSearchBarUI(),
2020-11-25 18:33:09 +08:00
getPeers(),
2020-11-16 01:13:26 +08:00
]),
));
}
void onConnect() {
var id = _idController.text.trim();
2020-11-25 18:33:09 +08:00
connect(id);
}
void connect(String id) {
2020-11-18 00:28:55 +08:00
if (id == '') return;
id = id.replaceAll(' ', '');
2020-11-25 18:33:09 +08:00
() async {
await Navigator.push<dynamic>(
context,
MaterialPageRoute<dynamic>(
builder: (BuildContext context) => RemotePage(id: id),
),
);
setState(() {});
}();
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
2020-11-15 20:04:05 +08:00
}
Widget getSearchBarUI() {
2020-11-24 22:03:04 +08:00
if (!FFI.ffiModel.initialized) {
return Container();
}
2020-11-15 20:04:05 +08:00
return Padding(
2020-11-30 15:04:59 +08:00
padding: const EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 0.0),
2020-11-16 01:13:26 +08:00
child: Container(
height: 84,
child: Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8),
child: Container(
decoration: BoxDecoration(
color: MyTheme.white,
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),
2020-11-23 01:16:17 +08:00
child: TextField(
autocorrect: false,
enableSuggestions: false,
2020-12-22 14:41:14 +08:00
keyboardType: TextInputType.visiblePassword,
// keyboardType: TextInputType.number,
2020-11-16 01:13:26 +08:00
style: TextStyle(
fontFamily: 'WorkSans',
fontWeight: FontWeight.bold,
fontSize: 30,
color: Color(0xFF00B6F0),
),
decoration: InputDecoration(
labelText: translate('Remote ID'),
2020-11-23 01:16:17 +08:00
// hintText: 'Enter your remote ID',
2020-11-16 01:13:26 +08:00
border: InputBorder.none,
helperStyle: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
2020-11-23 01:16:17 +08:00
color: Color(0xFFB9BABC),
2020-11-16 01:13:26 +08:00
),
labelStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
letterSpacing: 0.2,
2020-11-23 01:16:17 +08:00
color: Color(0xFFB9BABC),
2020-11-15 20:04:05 +08:00
),
),
autofocus: _idController.text.isEmpty,
controller: _idController,
2020-11-15 20:04:05 +08:00
),
2020-11-16 01:13:26 +08:00
),
2020-11-15 20:04:05 +08:00
),
2020-11-16 01:13:26 +08:00
SizedBox(
width: 60,
height: 60,
child: IconButton(
icon: Icon(Icons.arrow_forward,
2020-11-23 01:16:17 +08:00
color: Color(0xFFB9BABC), size: 45),
2020-11-16 01:13:26 +08:00
onPressed: onConnect,
autofocus: _idController.text.isNotEmpty,
2020-11-16 01:13:26 +08:00
),
)
],
2020-11-15 20:04:05 +08:00
),
),
2020-11-16 01:13:26 +08:00
),
2020-11-15 20:04:05 +08:00
),
);
}
2020-11-16 01:13:26 +08:00
@override
void dispose() {
_idController.dispose();
2020-11-16 01:13:26 +08:00
super.dispose();
}
2020-11-25 18:33:09 +08:00
Widget getPlatformImage(String platform) {
platform = platform.toLowerCase();
2020-11-25 20:39:08 +08:00
if (platform == 'mac os')
2020-11-25 18:33:09 +08:00
platform = 'mac';
else if (platform != 'linux') platform = 'win';
return Image.asset('assets/$platform.png', width: 24, height: 24);
2020-11-25 18:33:09 +08:00
}
Widget getPeers() {
if (!FFI.ffiModel.initialized) {
return Container();
}
2020-11-25 18:33:09 +08:00
final cards = <Widget>[];
var peers = FFI.peers();
peers.forEach((p) {
2020-11-30 21:14:23 +08:00
cards.add(Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: Card(
child: GestureDetector(
onTap: () => connect('${p.id}'),
onLongPressStart: (details) {
var x = details.globalPosition.dx;
var y = details.globalPosition.dy;
() async {
var value = await showMenu(
context: context,
position: RelativeRect.fromLTRB(x, y, x, y),
items: [
PopupMenuItem<String>(
child: Text(translate('Remove')),
value: 'remove'),
2020-11-30 21:14:23 +08:00
],
elevation: 8,
);
if (value == 'remove') {
setState(() => FFI.setByName('remove', '${p.id}'));
2021-08-06 21:18:06 +08:00
() async {
removePreference(p.id);
}();
2020-11-30 21:14:23 +08:00
}
}();
},
child: ListTile(
subtitle: Text('${p.username}@${p.hostname}'),
title: Text('${p.id}'),
leading: Container(
padding: const EdgeInsets.all(6),
child: getPlatformImage('${p.platform}'),
2021-08-14 14:14:01 +08:00
color: str2color('${p.id}${p.platform}', 0x7f)),
2020-11-30 21:14:23 +08:00
)))));
2020-11-25 18:33:09 +08:00
});
return Wrap(children: cards);
}
2020-11-15 20:04:05 +08:00
}
2020-11-26 00:33:45 +08:00
void showServer(BuildContext context) {
2020-11-26 00:47:52 +08:00
final formKey = GlobalKey<FormState>();
2021-08-06 22:29:11 +08:00
final id0 = FFI.getByName('option', 'custom-rendezvous-server');
final relay0 = FFI.getByName('option', 'relay-server');
final key0 = FFI.getByName('option', 'key');
2020-11-26 00:47:52 +08:00
var id = '';
var relay = '';
2021-04-01 16:59:42 +08:00
var key = '';
2020-11-26 00:33:45 +08:00
showAlertDialog(
context,
(setState) => Tuple3(
2021-08-06 22:45:45 +08:00
Text(translate('ID Server')),
2020-11-26 00:47:52 +08:00
Form(
key: formKey,
child:
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
TextFormField(
initialValue: id0,
2020-11-26 00:47:52 +08:00
decoration: InputDecoration(
labelText: translate('ID Server'),
2020-11-26 00:47:52 +08:00
),
validator: validate,
2020-11-26 00:47:52 +08:00
onSaved: (String value) {
id = value.trim();
2020-11-26 00:47:52 +08:00
},
),
2021-08-06 22:45:45 +08:00
/*
2020-11-26 00:47:52 +08:00
TextFormField(
initialValue: relay0,
2020-11-26 00:47:52 +08:00
decoration: InputDecoration(
labelText: translate('Relay Server'),
2020-11-26 00:47:52 +08:00
),
validator: validate,
2020-11-26 00:47:52 +08:00
onSaved: (String value) {
relay = value.trim();
2020-11-26 00:47:52 +08:00
},
),
2021-08-06 22:45:45 +08:00
*/
2021-04-01 16:59:42 +08:00
TextFormField(
initialValue: key0,
decoration: InputDecoration(
labelText: 'Key',
),
validator: null,
onSaved: (String value) {
key = value.trim();
},
),
2020-11-26 00:47:52 +08:00
])),
2020-11-26 00:33:45 +08:00
[
2021-08-02 20:54:56 +08:00
TextButton(
style: flatButtonStyle,
2020-11-26 00:33:45 +08:00
onPressed: () {
Navigator.pop(context);
},
2021-04-25 12:47:20 +08:00
child: Text(translate('Cancel')),
2020-11-26 00:33:45 +08:00
),
2021-08-02 20:54:56 +08:00
TextButton(
style: flatButtonStyle,
2020-11-26 00:33:45 +08:00
onPressed: () {
2020-11-26 00:47:52 +08:00
if (formKey.currentState.validate()) {
formKey.currentState.save();
if (id != id0)
2021-08-06 22:29:11 +08:00
FFI.setByName('option',
'{"name": "custom-rendezvous-server", "value": "${id}"}');
if (relay != relay0)
FFI.setByName('option',
'{"name": "relay-server", "value": "${relay}"}');
if (key != key0)
FFI.setByName(
'option', '{"name": "key", "value": "${key}"}');
2020-11-26 00:47:52 +08:00
Navigator.pop(context);
}
2020-11-26 00:33:45 +08:00
},
child: Text(translate('OK')),
2020-11-26 00:33:45 +08:00
),
],
2020-11-30 11:35:43 +08:00
));
}
Future<Null> showAbout(BuildContext context) async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
showAlertDialog(
context,
(setState) => Tuple3(
null,
Wrap(direction: Axis.vertical, spacing: 12, children: [
Text('Version: ${packageInfo.version}'),
InkWell(
onTap: () async {
2021-04-07 21:58:30 +08:00
const url = 'https://rustdesk.com/';
2020-11-30 11:35:43 +08:00
if (await canLaunch(url)) {
await launch(url);
}
},
child: Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: Text('Support',
style: TextStyle(
decoration: TextDecoration.underline,
)),
)),
]),
null),
2020-11-26 00:33:45 +08:00
() async => true,
true);
}
String validate(value) {
value = value.trim();
if (value.isEmpty) {
return null;
}
final res = FFI.getByName('test_if_valid_server', value);
return res.isEmpty ? null : res;
}