mhunt_launcher/lib/Widgets/Home/Dashboard.dart
2024-10-28 17:34:48 +05:30

342 lines
13 KiB
Dart

import 'dart:io';
import 'package:filepicker_windows/filepicker_windows.dart';
import 'package:flutter/material.dart';
import '../../Backend/Backend.dart';
import '../../Backend/FileHashEntry.dart';
import '../../Backend/InstallHelper.dart';
import '../../Backend/Structures.dart';
import '../CustomWidgets.dart';
Widget Dashboard({
required BuildContext context,
required Map<int, List<FileHashEntry>> downloadQueue,
required int dashboardSelectedGameIndex,
required List<FileHashEntry> missingFilesForSelectedGame,
required bool calculatingFiles,
required bool isUninstalling,
required bool isRunning,
required Map<String, dynamic> myStatsForSelectedGame,
required List<LeaderboardEntry> leaderboardForSelectedGame,
required List<Map<String, dynamic>> newsForSelectedGame,
required Function(int) onSelectedGameChanged,
required Function onUpdate,
required Function(int) onUninstallClicked,
required dynamic OnGameActionButtonClicked,
}) {
GameData selectedGameData = Backend.Games[dashboardSelectedGameIndex];
String filePath = InstallHelper.GetFilePath(dashboardSelectedGameIndex);
bool folderExists = Directory(filePath).existsSync();
String ActionBtnTxt = "Install";
if (missingFilesForSelectedGame.length <= 0) {
ActionBtnTxt = "Play";
} else {
ActionBtnTxt =
("Download ${FileHashEntry.getTotalSizeInMbytes(missingFilesForSelectedGame).toStringAsFixed(0)} MB");
}
if (calculatingFiles) {
ActionBtnTxt = "Validating";
}
if (downloadQueue.containsKey(dashboardSelectedGameIndex)) {
ActionBtnTxt = "Downloading";
}
if (isUninstalling) {
ActionBtnTxt = "Uninstalling";
}
if(isRunning){
ActionBtnTxt = "Running";
}
Widget ActionButton = GlassButton(
onTap: OnGameActionButtonClicked,
child: Text(ActionBtnTxt),
width: 200,
color: Colors.blue,
opacity: 0.5,
height: 50);
final screenHeight = MediaQuery.of(context).size.height;
List<DataRow> leaderboardList = [];
// leaderboardList.add(TableRow(children: [Text("username"),Text("kills"), Text("deaths")]));
// leaderboardList.add(TableRow());
leaderboardList
.addAll(List.generate(leaderboardForSelectedGame.length, (i) {
return DataRow(cells: <DataCell>[
DataCell(Text(leaderboardForSelectedGame[i].place.toString())),
DataCell(Text(leaderboardForSelectedGame[i].username)),
DataCell(Text(leaderboardForSelectedGame[i].kills.toString())),
DataCell(Text(leaderboardForSelectedGame[i].deaths.toString())),
DataCell(Text(leaderboardForSelectedGame[i].xp.toString())),
]);
}));
return Theme(
data: ThemeData(splashColor: Colors.transparent),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
height: screenHeight * 0.18,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
selectedGameData.name,
style: TextStyle(fontSize: 25),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(Backend.Games.length, (index) {
bool isSelected = index == dashboardSelectedGameIndex;
return InkWell(
onTap: (){
onSelectedGameChanged(index);
},
child: AnimatedContainer(
duration: const Duration(milliseconds: 100),
margin: EdgeInsets.all(9),
height: 80 * (isSelected ? 1.5 : 1),
width: 130 * (isSelected ? 1.5 : 1),
decoration: BoxDecoration(
image: DecorationImage(
image:
AssetImage(Backend.Games[index].imagePath),
fit: BoxFit.fill),
borderRadius: BorderRadius.circular(20)),
),
);
}),
),
],
),
),
!Backend.Games[dashboardSelectedGameIndex].isAvailable
? Center(
child: Text("Coming Soon", style: TextStyle(fontSize: 40)))
: Container(
height: screenHeight * 0.72,
child: Column(
children: [
Container(
padding: EdgeInsets.symmetric(horizontal: 50),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
ActionButton,
PopupMenuButton<String>(
iconColor: Colors.blueGrey,
onSelected: (val) {
if (val.toLowerCase().contains("uninstall")) {
onUninstallClicked(dashboardSelectedGameIndex);
}
},
itemBuilder: (BuildContext context) {
return {'Uninstall'}.map((String choice) {
return PopupMenuItem<String>(
value: choice,
child: Text(choice),
);
}).toList();
},
),
],
),
GlassCard(
color: Colors.green,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Text("My Stats",
style:
TextStyle(color: Colors.blueGrey)),
SizedBox(
width: 50,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Kills : ${(myStatsForSelectedGame['kills'] ?? "0")}"),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Deaths : ${(myStatsForSelectedGame['deaths'] ?? "0")}"),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Assist : ${(myStatsForSelectedGame['assists'] ?? "0")}"),
),
],
),
),
),
Container()
],
),
),
Container(
height: screenHeight * 0.6,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Padding(
padding: const EdgeInsets.all(20.0),
child: GlassCard(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
width: 350,
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(
vertical: 12.0, horizontal: 20),
child: Text(
"News",
style: TextStyle(fontSize: 20),
),
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: List.generate(
newsForSelectedGame.length,
(index) {
return NewsCard(
newsForSelectedGame[index]
['title'],
newsForSelectedGame[index]
['message']);
}),
),
),
)
],
),
)),
),
SizedBox(
width: 50,
),
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(
0, 20, 0, 0),
child: Text(
"Leaderboard",
style: TextStyle(fontSize: 20),
),
),
DataTable(
dataTextStyle:
TextStyle(color: Colors.white),
headingTextStyle:
TextStyle(color: Colors.white),
dividerThickness: 0,
columns: [
DataColumn(
label:
Expanded(child: Text("Place"))),
DataColumn(
label: Expanded(
child: Text("Username"))),
DataColumn(
label:
Expanded(child: Text("Kills"))),
DataColumn(
label: Expanded(
child: Text("Deaths"))),
DataColumn(
label: Expanded(child: Text("XP"))),
],
rows: leaderboardList,
),
],
),
),
],
))
],
),
),
Container(
//Install path footer
padding: EdgeInsets.symmetric(horizontal: 10),
alignment: Alignment.bottomCenter,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(children: [Text("Install path "), Text(filePath)]),
GlassButton(
onTap: () {
DirectoryPicker file = DirectoryPicker();
final dir = file.getDirectory();
Backend.prefs!.setString(
'${selectedGameData.name}_path', dir!.path);
onUpdate();
},
child: Text("Change Install Location"),
width: 250)
],
),
SizedBox(
height: 10,
)
],
),
)
],
),
);
}
Widget NewsCard(String title, String message) {
return Padding(
padding: const EdgeInsets.all(5.0),
child: GlassCard(
child: Row(
children: [
Padding(
padding: const EdgeInsets.all(18.0),
child: Icon(
Icons.newspaper,
color: Colors.blueGrey,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(fontSize: 18),
textAlign: TextAlign.left,
),
Text(
message,
overflow: TextOverflow.clip,
)
],
)
],
)),
);
}