Dart and Flutter upgrade; More things done
This commit is contained in:
parent
9b09f4797f
commit
7eeba12968
10 changed files with 338 additions and 89 deletions
200
lib/main.dart
200
lib/main.dart
|
|
@ -1,3 +1,5 @@
|
|||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:socket_io_client/socket_io_client.dart' as io;
|
||||
|
|
@ -174,27 +176,209 @@ class HomePage extends StatefulWidget {
|
|||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
late io.Socket socket;
|
||||
late AuthData authData = Get.find();
|
||||
|
||||
var guessersPlayers = <String>[].obs;
|
||||
var suggestersPlayers = <String>[].obs;
|
||||
var incommingRequests = <String>[].obs;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
socket = Get.find();
|
||||
|
||||
socket.on("hello", (idky) {
|
||||
socket.dispose();
|
||||
Get.offAllNamed("/auth");
|
||||
});
|
||||
|
||||
socket.on("update", (update) {
|
||||
bool ok = update[0];
|
||||
if (ok) {
|
||||
var data = update[1];
|
||||
guessersPlayers.value = (data["guessers"] as List<dynamic>? ?? [])
|
||||
.map((e) => e.toString())
|
||||
.toList(growable: false);
|
||||
suggestersPlayers.value = (data["suggesters"] as List<dynamic>? ?? [])
|
||||
.map((e) => e.toString())
|
||||
.toList(growable: false);
|
||||
} else {
|
||||
Get.snackbar("Error", "Update failed with message: ${update[1]}");
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("updateNeeded", (data) {
|
||||
socket.emit("getUpdate");
|
||||
});
|
||||
|
||||
socket.on("gameRequest", (requestFrom) {
|
||||
setState(() {
|
||||
incommingRequests.add(requestFrom);
|
||||
});
|
||||
});
|
||||
|
||||
socket.emit("getUpdate");
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final shortestSide = MediaQuery.of(context).size.shortestSide;
|
||||
final useMobileLayout = shortestSide < 600;
|
||||
final elements = [
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: _buildPlayerList(
|
||||
guessersPlayers,
|
||||
"Guessers",
|
||||
"guessers",
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: _buildPlayerList(
|
||||
suggestersPlayers,
|
||||
"Suggesters",
|
||||
"suggesters",
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("HUACU"),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text("Hello, ${Get.find<AuthData>().login}"),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: useMobileLayout
|
||||
? Column(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: elements,
|
||||
)
|
||||
: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: elements,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPlayerList(RxList<String> players, String title, String team) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title),
|
||||
Obx(() => Text("Count: ${players.length}")),
|
||||
Expanded(
|
||||
child: ObxValue(
|
||||
(data) => ListView.builder(
|
||||
itemCount: data.length,
|
||||
itemBuilder: (context, index) {
|
||||
return Card(
|
||||
child: ListTile(
|
||||
title: Text(
|
||||
"${data[index]} ${authData.login == data[index] ? "(you)" : ""}"
|
||||
.trim(),
|
||||
),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (incommingRequests.contains(data[index]))
|
||||
InkResponse(
|
||||
onTap: () =>
|
||||
_handleRequestResponseAction(data[index], true),
|
||||
child: const Icon(Icons.check),
|
||||
),
|
||||
if (incommingRequests.contains(data[index]))
|
||||
InkResponse(
|
||||
onTap: () => _handleRequestResponseAction(
|
||||
data[index], false),
|
||||
child: const Icon(Icons.close),
|
||||
),
|
||||
if (authData.login != data[index])
|
||||
InkResponse(
|
||||
onTap: () => _handleSendRequestClick(data[index]),
|
||||
child: const Icon(Icons.connect_without_contact),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
players,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Expanded(
|
||||
child: ObxValue(
|
||||
(data) => players.contains(authData.login)
|
||||
? ElevatedButton(
|
||||
onPressed: () => _handleLeaveClick(team),
|
||||
child: const Text("Leave"),
|
||||
)
|
||||
: ElevatedButton(
|
||||
onPressed: () => _handleJoinClick(team),
|
||||
child: const Text("Join"),
|
||||
),
|
||||
guessersPlayers,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _joinResponseHandler(dynamic data) {
|
||||
bool ok = data[0];
|
||||
if (!ok) {
|
||||
int status = data[1];
|
||||
Get.snackbar("Error", "Join failed with status $status");
|
||||
}
|
||||
socket.off("joinResponse", _joinResponseHandler);
|
||||
}
|
||||
|
||||
void _handleJoinClick(String side) {
|
||||
socket.on("joinResponse", _joinResponseHandler);
|
||||
socket.emit("join", side);
|
||||
}
|
||||
|
||||
void _leaveResponseHandler(dynamic data) {
|
||||
bool ok = data[0];
|
||||
if (!ok) {
|
||||
int status = data[1];
|
||||
Get.snackbar("Error", "Leaving failed with status $status");
|
||||
}
|
||||
socket.off("leaveResponse", _leaveResponseHandler);
|
||||
}
|
||||
|
||||
void _handleLeaveClick(String side) {
|
||||
socket.on("leaveResponse", _leaveResponseHandler);
|
||||
socket.emit("leave", side);
|
||||
}
|
||||
|
||||
void _sendRequestResponseHandler(dynamic data) {
|
||||
bool ok = data[0];
|
||||
if (ok) {
|
||||
Get.snackbar("Success", "Request sent");
|
||||
} else {
|
||||
Get.snackbar("Error", "Request failed");
|
||||
}
|
||||
socket.off("sendRequestResponse", _leaveResponseHandler);
|
||||
}
|
||||
|
||||
void _handleSendRequestClick(String player) {
|
||||
socket.on("sendRequestResponse", _sendRequestResponseHandler);
|
||||
socket.emit("sendRequest", player);
|
||||
}
|
||||
|
||||
void _handleRequestResponseAction(String data, bool bool) {}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue