fipy/lib/screens/home.dart

252 lines
9.6 KiB
Dart

// ignore_for_file: prefer_const_literals_to_create_immutables, avoid_print
import 'dart:convert';
import 'package:fipy/globals.dart';
import 'package:fipy/models/track.dart';
import 'package:fipy/providers/home.dart';
import 'package:fipy/providers/player.dart';
import 'package:fipy/widgets/download_track.dart';
import 'package:fipy/widgets/player/player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String radio = 'fip_groove';
@override
Widget build(BuildContext context) {
homeContext = context;
HomeProvider hp = Provider.of<HomeProvider>(context, listen: false);
return RawKeyboardListener(
autofocus: true,
focusNode: FocusNode(),
onKey: (RawKeyEvent event) {
// log.d(event.data.logicalKey.keyId);
if (event.runtimeType == RawKeyDownEvent) //Enter Key ID from keyboard
{
switch (event.data.logicalKey.keyId) {
case 32:
case 112:
case 4294969861:
hp.resumePlay();
break;
case 110:
case 94489280688:
hp.nextTrack();
break;
case 98:
case 94489280689:
hp.backTrack();
break;
}
}
},
child: Stack(
children: <Widget>[
Scaffold(
backgroundColor: Colors.black,
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
color: Colors.grey[900],
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownButton(
dropdownColor: Colors.grey[900],
value: radio,
style: TextStyle(
fontSize: 15, color: Colors.grey[300]),
underline: const SizedBox(),
iconSize: 22,
onChanged: (String? newRadio) {
setState(() {
radio = newRadio!;
});
},
items: hp.radioList),
const SizedBox(width: 50),
DropdownButton(
dropdownColor: Colors.grey[900],
value: hp.userPageNbr.toString(),
style: TextStyle(
fontSize: 15, color: Colors.grey[300]),
underline: const SizedBox(),
iconSize: 22,
onChanged: (String? newPageNumber) {
setState(() {
hp.userPageNbr = int.parse(newPageNumber!);
});
},
items: hp.pageList),
]),
),
FutureBuilder<List<Track>>(
future: hp.getTracks(radio),
builder: (
BuildContext context,
AsyncSnapshot<List<Track>> snapshot,
) {
print(snapshot.connectionState);
if (snapshot.connectionState == ConnectionState.waiting) {
return Stack(children: [
Container(
height: 10000,
width: 10000,
color: const Color(0xFF121212)),
Center(
child: Column(children: [
const SizedBox(height: 20),
SizedBox(
height: 30,
width: 30,
child: CircularProgressIndicator(
color: Colors.amber[100],
),
),
]),
),
]);
} else if (snapshot.connectionState ==
ConnectionState.done) {
if (snapshot.hasError) {
return Stack(children: [
Container(
height: 10000,
width: 10000,
color: const Color(0xFF121212)),
Center(
child: Column(children: [
const SizedBox(height: 20),
Text(
'Error: ${snapshot.error}',
style: TextStyle(color: Colors.grey[500]),
),
]),
),
]);
} else if (snapshot.hasData) {
return Table(
columnWidths: {
0: const FlexColumnWidth(4),
1: const FlexColumnWidth(2),
2: const FlexColumnWidth(1),
},
defaultVerticalAlignment:
TableCellVerticalAlignment.middle,
children: snapshot.data!
.map((item) => _buildTableRow(item))
.toList()
..insert(
0,
_buildTableRow(Track(
number: -1,
title: 'TITRE',
artiste: 'ARTISTE',
album: 'ALBUM',
id: 'URL')),
),
);
} else {
return const Text('Empty data');
}
} else {
return Text('State: ${snapshot.connectionState}');
}
},
),
const SizedBox(height: 70)
],
),
),
),
const Player()
],
),
);
}
}
TableRow _buildTableRow(Track track) {
final hp = Provider.of<HomeProvider>(homeContext, listen: false);
final textStyle = TextStyle(
fontWeight: track.number == -1 ? FontWeight.w200 : FontWeight.normal,
color: Colors.grey[500],
fontSize: track.number == -1 ? 15 : 14);
const rowPadding = EdgeInsets.all(10);
return TableRow(
decoration: const BoxDecoration(
color: Color(0xFF121212),
),
children: [
TableCell(
child: Padding(
padding: rowPadding,
child: InkWell(
onTap: () async {
if (track.number != -1) {
hp.playTrack(track);
}
},
child: Row(children: [
track.number != -1
? track.image == ''
? const SizedBox(width: 40)
: Image.memory(base64Decode(track.image!), height: 40)
: const SizedBox(),
const SizedBox(width: 10),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Consumer<PlayerProvider>(
builder: (context, playerProvider, _) {
return Text(track.title,
style: TextStyle(
fontWeight: FontWeight.normal,
color:
hp.currentTrack?.title == track.title &&
hp.currentTrack?.artiste ==
track.artiste
? Colors.green
: Colors.grey[350],
fontSize: 14));
}),
const SizedBox(height: 2),
if (track.number != -1)
Text(track.artiste,
style: TextStyle(
fontWeight: FontWeight.normal,
color: Colors.grey[500],
fontSize: 13))
]),
]),
)),
),
TableCell(
child: Padding(
padding: rowPadding,
child: Text(track.album ?? '', style: textStyle),
),
),
TableCell(
child: Padding(
padding: rowPadding,
child: DownloadTrack(track: track),
),
),
]);
}