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-15 20:04:05 +08:00
|
|
|
import 'common.dart';
|
2020-11-19 00:32:46 +08:00
|
|
|
import 'model.dart';
|
2020-11-17 01:22:14 +08:00
|
|
|
import 'remote_page.dart';
|
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> {
|
2020-11-17 01:22:14 +08:00
|
|
|
final _idController = TextEditingController();
|
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>(
|
|
|
|
child: Text('ID/Relay Server'), value: 'server'),
|
|
|
|
],
|
|
|
|
elevation: 8,
|
|
|
|
);
|
|
|
|
if (value == 'server') {
|
|
|
|
showServer(context);
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
})
|
|
|
|
],
|
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>[
|
|
|
|
getSearchBarUI(),
|
2020-11-25 18:33:09 +08:00
|
|
|
getPeers(),
|
2020-11-16 01:13:26 +08:00
|
|
|
]),
|
|
|
|
padding: const EdgeInsets.fromLTRB(16.0, 0.0, 16.0, 0.0),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
void onConnect() {
|
2020-11-17 01:22:14 +08:00
|
|
|
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;
|
2020-11-25 18:33:09 +08:00
|
|
|
() async {
|
|
|
|
await Navigator.push<dynamic>(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute<dynamic>(
|
|
|
|
builder: (BuildContext context) => RemotePage(id: id),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
setState(() {});
|
|
|
|
}();
|
2020-11-24 11:25:56 +08:00
|
|
|
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-16 01:13:26 +08:00
|
|
|
padding: const EdgeInsets.only(top: 8.0),
|
|
|
|
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-11-16 01:13:26 +08:00
|
|
|
style: TextStyle(
|
|
|
|
fontFamily: 'WorkSans',
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
fontSize: 30,
|
|
|
|
color: Color(0xFF00B6F0),
|
|
|
|
),
|
2020-11-20 17:51:49 +08:00
|
|
|
// keyboardType: TextInputType.number,
|
2020-11-16 01:13:26 +08:00
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: '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
|
|
|
),
|
|
|
|
),
|
2020-11-24 11:25:56 +08:00
|
|
|
autofocus: _idController.text.isEmpty,
|
2020-11-17 01:22:14 +08:00
|
|
|
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,
|
2020-11-24 11:25:56 +08:00
|
|
|
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() {
|
2020-11-17 01:22:14 +08:00
|
|
|
_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: 36, height: 36);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget getPeers() {
|
|
|
|
final cards = <Widget>[];
|
|
|
|
var peers = FFI.peers();
|
|
|
|
peers.forEach((p) {
|
|
|
|
cards.add(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('Remove'), value: 'remove'),
|
|
|
|
],
|
|
|
|
elevation: 8,
|
|
|
|
);
|
|
|
|
if (value == 'remove') {
|
|
|
|
setState(() => FFI.setByName('remove', '${p.id}'));
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
},
|
|
|
|
child: ListTile(
|
|
|
|
subtitle: Text('${p.username}@${p.hostname}'),
|
|
|
|
title: Text('${p.id}'),
|
|
|
|
leading: Container(
|
|
|
|
padding: const EdgeInsets.all(6),
|
|
|
|
child: getPlatformImage('${p.platform}'),
|
|
|
|
color: str2color('${p.id}${p.platform}', 0x77)),
|
|
|
|
))));
|
|
|
|
});
|
|
|
|
return Wrap(children: cards);
|
|
|
|
}
|
2020-11-15 20:04:05 +08:00
|
|
|
}
|
2020-11-26 00:33:45 +08:00
|
|
|
|
|
|
|
void showServer(BuildContext context) {
|
|
|
|
showAlertDialog(
|
|
|
|
context,
|
|
|
|
(setState) => Tuple3(
|
|
|
|
Text('ID/Relay Server'),
|
|
|
|
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
|
|
|
|
TextFormField(
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'ID Server',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'Relay Server',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
[
|
|
|
|
FlatButton(
|
|
|
|
textColor: MyTheme.accent,
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
child: Text('Cancel'),
|
|
|
|
),
|
|
|
|
FlatButton(
|
|
|
|
textColor: MyTheme.accent,
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
child: Text('OK'),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
() async => true,
|
|
|
|
true);
|
|
|
|
}
|