From 2beccd070a504ede35e41089131e0acc8861495b Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Mon, 2 Dec 2019 21:38:47 +0100 Subject: [PATCH] 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. --- lib/screens/program.dart | 63 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/screens/program.dart b/lib/screens/program.dart index 8eacec6..508ee02 100644 --- a/lib/screens/program.dart +++ b/lib/screens/program.dart @@ -1,6 +1,39 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; -class ProgramScreen extends StatelessWidget { +import '../backend.dart'; + +class ProgramScreen extends StatefulWidget { + @override + _ProgramScreenState createState() => _ProgramScreenState(); +} + +class _ProgramScreenState extends State { + Backend backend; + StreamSubscription 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 Widget build(BuildContext context) { return Scaffold( @@ -11,6 +44,34 @@ class ProgramScreen extends StatelessWidget { ), title: Text('Program'), ), + bottomNavigationBar: BottomAppBar( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + 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(); + } }