flutter_miniplayer/example/lib/main.dart

97 lines
2.8 KiB
Dart
Raw Normal View History

2020-08-27 11:27:25 +02:00
import 'package:flutter/material.dart';
2021-03-06 11:48:10 +01:00
import 'screens/audio_screen.dart';
import 'widgets/player.dart';
import 'utils.dart';
2020-08-27 11:27:25 +02:00
2021-03-06 15:26:00 +01:00
ValueNotifier<AudioObject?> currentlyPlaying = ValueNotifier(null);
2021-03-06 11:48:10 +01:00
const double playerMinHeight = 70;
const double playerMaxHeight = 370;
const miniplayerPercentageDeclaration = 0.2;
2020-08-27 11:27:25 +02:00
2021-03-06 11:48:10 +01:00
void main() => runApp(MyApp());
2020-08-27 11:27:25 +02:00
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
2021-03-06 11:48:10 +01:00
title: 'Miniplayer Demo',
2020-08-27 11:27:25 +02:00
theme: ThemeData(
2021-03-06 11:48:10 +01:00
primaryColor: Colors.grey[50],
visualDensity: VisualDensity.adaptivePlatformDensity,
2020-08-27 11:27:25 +02:00
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
2021-03-06 11:48:10 +01:00
return Scaffold(
body: Stack(
children: [
Column(
children: [
AppBar(title: Text('Miniplayer Demo')),
Expanded(
child: AudioUi(
onTap: (audioObject) => currentlyPlaying.value = audioObject,
),
),
],
),
ValueListenableBuilder(
valueListenable: currentlyPlaying,
2021-03-06 15:26:00 +01:00
builder: (BuildContext context, AudioObject? audioObject,
Widget? child) =>
audioObject != null
? DetailedPlayer(audioObject: audioObject)
: Container(),
2021-03-06 11:48:10 +01:00
),
],
),
bottomNavigationBar: ValueListenableBuilder(
valueListenable: playerExpandProgress,
2021-03-06 15:26:00 +01:00
builder: (BuildContext context, double height, Widget? child) {
2021-03-06 11:48:10 +01:00
final value = percentageFromValueInRange(
min: playerMinHeight, max: playerMaxHeight, value: height);
2020-08-27 11:27:25 +02:00
2021-03-06 11:48:10 +01:00
var opacity = 1 - value;
if (opacity < 0) opacity = 0;
if (opacity > 1) opacity = 1;
return SizedBox(
height:
kBottomNavigationBarHeight - kBottomNavigationBarHeight * value,
child: Transform.translate(
offset: Offset(0.0, kBottomNavigationBarHeight * value * 0.5),
2021-09-29 11:38:29 +02:00
child: Opacity(
opacity: opacity,
child: OverflowBox(
maxHeight: kBottomNavigationBarHeight,
child: child,
),
),
2020-08-27 11:27:25 +02:00
),
2021-03-06 11:48:10 +01:00
);
},
2021-03-06 15:26:00 +01:00
child: BottomNavigationBar(
2020-08-27 11:27:25 +02:00
currentIndex: 0,
2021-03-06 15:26:00 +01:00
selectedItemColor: Colors.blue,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Feed'),
2020-08-27 11:27:25 +02:00
BottomNavigationBarItem(
2021-03-06 15:26:00 +01:00
icon: Icon(Icons.library_books), label: 'Library'),
2020-08-27 11:27:25 +02:00
],
),
),
);
}
}