MusicPlayer/lib/main.dart

95 lines
2.1 KiB
Dart
Raw Normal View History

2022-03-23 18:38:58 +00:00
// ignore_for_file: avoid_print
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
2022-03-23 17:49:04 +00:00
import 'package:flutter/material.dart';
2022-03-23 18:38:58 +00:00
import 'package:music_web_player/api.dart';
2022-03-23 18:08:04 +00:00
import 'package:music_web_player/config.dart';
2022-03-23 17:49:04 +00:00
void main() {
2022-03-23 18:08:04 +00:00
loadConfig();
runApp(const PlayerApp());
2022-03-23 17:49:04 +00:00
}
2022-03-23 18:08:04 +00:00
class PlayerApp extends StatelessWidget {
const PlayerApp({Key? key}) : super(key: key);
2022-03-23 17:49:04 +00:00
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
2022-03-23 18:08:04 +00:00
title: 'Music player',
debugShowCheckedModeBanner: false,
2022-03-23 17:49:04 +00:00
theme: ThemeData(
2022-03-23 18:08:04 +00:00
primarySwatch: Colors.green,
brightness: Brightness.dark,
2022-03-23 17:49:04 +00:00
),
2022-03-23 18:08:04 +00:00
home: const AppHome(),
2022-03-23 17:49:04 +00:00
);
}
}
2022-03-23 18:08:04 +00:00
class AppHome extends StatefulWidget {
const AppHome({Key? key}) : super(key: key);
2022-03-23 17:49:04 +00:00
@override
2022-03-23 18:08:04 +00:00
State<AppHome> createState() => _AppHomeState();
2022-03-23 17:49:04 +00:00
}
2022-03-23 18:08:04 +00:00
class _AppHomeState extends State<AppHome> {
2022-03-23 18:38:58 +00:00
var error = false;
List<MusicEntry>? musics;
Future<void> load() async {
try {
setState(() => error = false);
musics = await API.getList();
setState(() {});
} catch (e, s) {
print("$e $s");
setState(() => error = true);
}
}
@override
void initState() {
super.initState();
load();
}
2022-03-23 17:49:04 +00:00
@override
Widget build(BuildContext context) {
2022-03-23 18:38:58 +00:00
return Scaffold(body: buildBody(context));
}
Widget buildBody(BuildContext context) {
if (error) {
return Center(
child: IntrinsicHeight(
child: Column(
children: [
const Icon(FluentIcons.warning_24_regular, size: 50),
const SizedBox(height: 50),
const Text("Failed to load musics list!"),
const SizedBox(height: 50),
ElevatedButton(
onPressed: load,
child: Text("Try again".toUpperCase()),
),
],
),
));
}
if (musics == null) {
return const Center(child: CircularProgressIndicator());
}
if (musics!.isEmpty) {
return const Center(child: Text("Musics list is empty!"));
}
// TODO : go on
return Text(musics![0].artist);
2022-03-23 17:49:04 +00:00
}
}