softplayer-web/lib/main.dart

150 lines
4.6 KiB
Dart
Raw Normal View History

2024-04-19 11:19:44 +00:00
import 'dart:html';
2024-03-21 08:39:25 +00:00
import 'package:flutter/material.dart';
2024-04-04 14:35:33 +00:00
import 'package:grpc/grpc_web.dart';
2024-04-05 15:14:56 +00:00
import 'package:softplayer_web/api/grpc/accounts.dart';
2024-05-02 21:56:55 +00:00
import 'package:softplayer_web/api/grpc/creds.dart';
2024-04-30 15:31:53 +00:00
import 'package:softplayer_web/components/create_env_form.dart';
2024-04-19 11:19:44 +00:00
import 'package:softplayer_web/components/environments.dart';
import 'package:softplayer_web/components/login_form.dart';
2024-03-21 08:39:25 +00:00
void main() async {
2024-04-04 14:35:33 +00:00
const String backendURL = String.fromEnvironment(
'SOFTPLAYER_BACKEND_URL',
2024-04-05 15:14:56 +00:00
defaultValue: 'https://softplayer-backend.badhouseplants.net:8080',
2024-04-04 14:35:33 +00:00
);
2024-04-04 16:15:30 +00:00
GrpcWebClientChannel grpcChannel =
GrpcWebClientChannel.xhr(Uri.parse(backendURL));
2024-04-04 14:35:33 +00:00
runApp(MyApp(channel: grpcChannel));
2024-03-21 08:39:25 +00:00
}
class MyApp extends StatelessWidget {
2024-04-05 15:14:56 +00:00
MyApp({super.key, required this.channel});
2024-04-04 14:35:33 +00:00
final GrpcWebClientChannel channel;
2024-04-05 15:14:56 +00:00
late final AccountsGrpc accountsGrpc = AccountsGrpc(channel: channel);
2024-03-21 08:39:25 +00:00
@override
Widget build(BuildContext context) {
2024-04-05 15:14:56 +00:00
accountsGrpc.init();
2024-03-21 08:39:25 +00:00
return MaterialApp(
2024-03-26 16:17:04 +00:00
debugShowCheckedModeBanner: false,
2024-04-04 14:35:33 +00:00
title: 'Softplayer',
2024-04-29 10:42:16 +00:00
home: RootWidget(channel: channel),
2024-03-21 08:39:25 +00:00
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
);
}
}
2024-04-19 11:19:44 +00:00
2024-04-29 10:42:16 +00:00
class RootWidget extends StatefulWidget {
2024-04-19 11:19:44 +00:00
final GrpcWebClientChannel channel;
2024-04-29 10:42:16 +00:00
RootWidget({super.key, required this.channel});
2024-04-19 11:19:44 +00:00
late final AccountsGrpc accountsGrpc = AccountsGrpc(channel: channel);
2024-04-29 10:42:16 +00:00
@override
@override
State<StatefulWidget> createState() => _StateRootWidget();
}
class _StateRootWidget extends State<RootWidget> {
refresh() {
setState(() {});
}
2024-04-19 11:19:44 +00:00
bool isSignedIn() {
return window.localStorage.containsKey("token");
}
2024-05-02 21:56:55 +00:00
final GlobalKey<ScaffoldState> _key = GlobalKey(); // Create a key
2024-04-19 11:19:44 +00:00
@override
Widget build(BuildContext context) {
if (!isSignedIn()) {
2024-04-29 10:42:16 +00:00
return Scaffold(
body: Container(
2024-04-30 15:31:53 +00:00
width: double.infinity,
height: double.infinity,
2024-04-19 11:19:44 +00:00
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/login_background.jpg"),
2024-04-30 15:31:53 +00:00
fit: BoxFit.cover,
2024-04-19 11:19:44 +00:00
),
),
2024-04-29 10:42:16 +00:00
child: LoginForm(
grpcChannel: widget.channel,
notifyParent: refresh,
),
2024-04-19 11:41:50 +00:00
));
2024-04-19 11:19:44 +00:00
} else {
2024-04-30 15:31:53 +00:00
EnvirnomentList envList = EnvirnomentList(channel: widget.channel);
2024-05-02 21:56:55 +00:00
return DefaultTabController(
length: 3,
initialIndex: 0,
child: Scaffold(
key: _key,
endDrawer: const Drawer(child: Text("text")),
body: TabBarView(children: [envList, envList]),
2024-05-02 17:09:44 +00:00
appBar: AppBar(
2024-05-02 21:56:55 +00:00
backgroundColor: Colors.grey,
centerTitle: false,
title: const Text("Softplayer"),
bottom: const TabBar(
tabs: <Widget>[
Tab(
icon: Icon(Icons.computer),
text: "Environments",
),
Tab(
icon: Icon(Icons.install_desktop),
text: "Your applications",
),
Tab(
icon: Icon(Icons.list),
text: "Application Catalog",
),
],
),
actions: [
PopupMenuButton(
child: const Row(children: [
Icon(Icons.account_circle),
Text("account"),
]),
itemBuilder: (context) => [
const PopupMenuItem(
child: Row(
children: [Icon(Icons.settings), Text("Settings")],
),
),
const PopupMenuItem(
child: Row(children: [
Icon(Icons.monetization_on),
Text("Invoices"),
])),
PopupMenuItem(
child: const Row(
children: [Icon(Icons.logout), Text("Sign out")]),
onTap: () {
SoftplayerCredsHelpers().cleanupLocalStorate();
refresh();
},
),
])
],
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () => showDialog(
context: context,
builder: (context) => CreateEnvForm(widget.channel),
),
2024-04-30 15:31:53 +00:00
),
2024-04-19 11:41:50 +00:00
),
);
2024-04-19 11:19:44 +00:00
}
}
}