Home > Blockchain >  Slider not updating, and will not move when touched
Slider not updating, and will not move when touched

Time:01-19

I am having an issue with a slider for an audioplayer in flutter. It is not updating while audio is playing, and cannot be moved at all. The debugprint for position is always showing 0:00.

Here is where the slider is built:

Future<Widget> slider() async {
  var player = AudioPlayer();
  AudioPlayerController apc = AudioPlayerController(selectedPath);
  await apc.setSource(selectedPath);

  var _position = apc.position;
  var _duration = apc.duration;
  // var _position =
  debugPrint('$_position');
  debugPrint('$_duration');

  return Slider(
      activeColor: AppColor.AppDarkBlueColor,
      divisions: _duration?.inSeconds.toInt() ?? 1,
      value: _position.inMilliseconds.toDouble() * .001,
      min: 0.0,
      max: _duration?.inSeconds.toDouble() ?? 0.0,
      onChanged: (double value1) {
        Duration _newPosition = Duration(seconds: value1.toInt());
        setState(() {
          player.seek(_newPosition);
          _position = _newPosition;
          debugPrint('$_position');
        });
      });
}

And Here is where the slider is implemented into the page:

bottomNavigationBar: Container(
            decoration: const BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(20)),
                color: AppColor.AppLightBlueColor),
            height: (MediaQuery.of(context).size.height / 6),
            child: Container(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                  Container(
                      child: FutureBuilder(
                    future: slider(),
                    builder: (context, data) {
                      if (data.hasData) {
                        dynamic slide = data.data ?? [];
                        return (Container(
                          child: slide,
                        ));
                      } else {
                        return Text("");
                      }
                    },
                  )),

I can see the position is updating elsewhere in the code, but I am not sure why it isn't updating with the slider. The position is set using audioplayer's .position argument.

CodePudding user response:

I don't think it can work this way: A Future only returns a single event, so there will never be any updates.

I think what you need here is a StateFulWidget to hold the player, and a StreamBuilder or ValueListanableBuilder to update your slider.

  • Related