Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

flutter - Slider widget does not work on iOS and Web

I have an audio player in my app. I used Slider.adaptive to make the seek bar. Although It works well on Android, it gives me this error on iOS and the Web:

════════ Exception caught by widgets library ═══════════════════════════════════ Assertion failed: ../…/cupertino/slider.dart:332 value != null && value >= 0.0 && value <= 1.0 is not true

The relevant error-causing widget was Slider

Here is how my code looks like:

class PlayBackButtons extends StatefulWidget {
  @override
  _PlayBackButtonsState createState() => _PlayBackButtonsState();
}

class _PlayBackButtonsState extends State<PlayBackButtons> {
  bool _isPlaying = false;
  AudioPlayer _audioPlayer;
  Duration _duration = new Duration();
  Duration _position = new Duration();
  int result = 0;
  int tutsLenght;
  int index;
  @override
  void initState() {
    super.initState();
    _audioPlayer = AudioPlayer();
    Future.delayed(Duration.zero, () {
      Provider.of<Tutorials>(context, listen: false).resetIndex();
    });
  }

  @override
  void dispose() {
    _cleanup();
    super.dispose();
  }

   double progress() => max() > (_position?.inMilliseconds ?? 0).toDouble()
      ? (_position?.inMilliseconds ?? 0).toDouble()
      : 0.0;

  double max() => (_duration.inMilliseconds ?? 0.0).toDouble(); 

  void _cleanup() {
    if (result == 1) {
      _audioPlayer.stop();
      _audioPlayer.dispose();
      Provider.of<Tutorials>(context).resetIndex();
    }
  }

  void _play(String url) async {
    result = await _audioPlayer.play(url);
    setState(() => _isPlaying = true);
    if (result == 1) {
      _audioPlayer.onDurationChanged.listen((Duration dd) {
        setState(() {
          _duration = dd;
        });
      });
      _audioPlayer.onAudioPositionChanged.listen((Duration dd) {
        setState(() {
          _position = dd;
        });
      });
    } else {
      print('audio player crashed');
    }
  }

  void _pause() async {
    await _audioPlayer.pause();
    setState(() => _isPlaying = false);
  }

  void _next() async {
    await _audioPlayer.stop();
    setState(() => _isPlaying = false);
    if (Provider.of<Tutorials>(context).index < tutsLenght) {
    } else {
      Navigator.pop(context);
    }
  }

  void _previous() {
    if (Provider.of<Tutorials>(context).index != 0) {
      _audioPlayer.stop();
      setState(() => _isPlaying = false);
      Provider.of<Tutorials>(context).decreamentIndex();
    }
  }

  @override
  Widget build(BuildContext context) {
    index = Provider.of<Tutorials>(context).index;
    final tuts = Provider.of<Tutorials>(context, listen: false).tutsOfASection;
    tutsLenght = tuts.length;
    final url = tuts[index].audio;
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Slider.adaptive(
          min: 0.0,
          value: progress(),//_position.inSeconds.toDouble(),
          max: max(),//_duration.inSeconds.toDouble(),
          
          onChanged: (double value) {
            setState(() => _audioPlayer.seek(Duration(seconds: value.toInt())));
          },
        ),
        Row(
          children: [
            IconButton(
              icon: Icon(Icons.skip_previous),
              onPressed: () => _previous(),
            ),
            IconButton(
              icon: _isPlaying ? Icon(Icons.pause) : Icon(Icons.play_arrow),
              onPressed: () {
                if (_isPlaying) {
                  _pause();
                } else {
                  _play(url);
                }
              },
            ),
            IconButton(
              icon: Icon(Icons.skip_next),
              onPressed: () => _next(),
            ),
          ],
          mainAxisAlignment: MainAxisAlignment.center,
        ),
      ],
    );
  }
}
question from:https://stackoverflow.com/questions/65859412/slider-widget-does-not-work-on-ios-and-web

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...