Add playback position slider to program screen

ProgramScreen became a StatefulWidget. It listens to playback position
updates from the backend and updates the slider unless the user is
currently interacting with it.
This commit is contained in:
Elias Projahn 2019-12-02 21:38:47 +01:00
parent 17f3040645
commit 2beccd070a

View file

@ -1,6 +1,39 @@
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class ProgramScreen extends StatelessWidget { import '../backend.dart';
class ProgramScreen extends StatefulWidget {
@override
_ProgramScreenState createState() => _ProgramScreenState();
}
class _ProgramScreenState extends State<ProgramScreen> {
Backend backend;
StreamSubscription<double> positionSubscription;
double position = 0.0;
bool seeking = false;
@override
void didChangeDependencies() {
super.didChangeDependencies();
backend = Backend.of(context);
if (positionSubscription != null) {
positionSubscription.cancel();
}
positionSubscription = backend.position.listen((pos) {
if (!seeking) {
setState(() {
position = pos;
});
}
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -11,6 +44,34 @@ class ProgramScreen extends StatelessWidget {
), ),
title: Text('Program'), title: Text('Program'),
), ),
bottomNavigationBar: BottomAppBar(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Slider(
value: position,
onChangeStart: (_) {
seeking = true;
},
onChangeEnd: (pos) {
seeking = false;
backend.seekTo(pos);
},
onChanged: (pos) {
setState(() {
position = pos;
});
},
),
],
),
),
); );
} }
@override
void dispose() {
super.dispose();
positionSubscription.cancel();
}
} }