softplayer-web/lib/main.dart

97 lines
2.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-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");
}
@override
Widget build(BuildContext context) {
if (!isSignedIn()) {
2024-04-29 10:42:16 +00:00
return Scaffold(
body: Container(
2024-04-19 11:19:44 +00:00
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/login_background.jpg"),
fit: BoxFit.fill,
),
),
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-19 11:41:50 +00:00
return Scaffold(
2024-04-29 10:42:16 +00:00
body: EnvirnomentList(channel: widget.channel),
appBar: AppBar(
title: const Text("Softplayer"),
actions: [
TextButton(
onPressed: () {
window.localStorage.remove("token");
window.localStorage.remove("uuid");
refresh();
},
child: const Text("sign out"))
],
),
2024-04-19 11:41:50 +00:00
floatingActionButton: FloatingActionButton(
onPressed: () => print("1"),
),
);
2024-04-19 11:19:44 +00:00
}
}
}