Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I rebuild only single widgets of my app #25

Closed
md186 opened this issue May 11, 2020 · 2 comments
Closed

How can I rebuild only single widgets of my app #25

md186 opened this issue May 11, 2020 · 2 comments

Comments

@md186
Copy link

md186 commented May 11, 2020

Hey there Hope you can help me out. I’m using the following code in my main.dart file:

String _platformVersion = 'Unknown';
bool isPlaying = false;
Duration _duration;
Duration _position;
double _slider;
double _sliderVolume;
String _error;
num curIndex = 0;
PlayMode playMode = AudioManager.instance.playMode;

final list = [
{
"title": "Assets",
"desc": "local assets playback",
"url": "assets/audio.mp3",
"coverUrl": "assets/ic_launcher.png"
},
{
"title": "network",
"desc": "network resouce playback",
"url": "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.m4a",
"coverUrl": "https://homepages.cae.wisc.edu/~ece533/images/airplane.png"
}
];

@OverRide
void initState() {
super.initState();

initPlatformState();
setupAudio();
// loadFile();

}

@OverRide
void dispose() {
// 释放所有资源
AudioManager.instance.stop();
super.dispose();
}

void setupAudio() {
List _list = [];
list.forEach((item) => _list.add(AudioInfo(item["url"],
title: item["title"], desc: item["desc"], coverUrl: item["coverUrl"])));

AudioManager.instance.audioList = _list;
AudioManager.instance.intercepter = true;
AudioManager.instance.play(auto: false);

AudioManager.instance.onEvents((events, args) {
  print("$events, $args");
  switch (events) {
    case AudioManagerEvents.start:
      print("start load data callback");
      _position = AudioManager.instance.position;
      _duration = AudioManager.instance.duration;
      _slider = 0;
      setState(() {});
      break;
    case AudioManagerEvents.ready:
      print("ready to play");
      _error = null;
      _sliderVolume = AudioManager.instance.volume;
      _position = AudioManager.instance.position;
      _duration = AudioManager.instance.duration;
      setState(() {});
      // if you need to seek times, must after AudioManagerEvents.ready event invoked
      // AudioManager.instance.seekTo(Duration(seconds: 10));
      break;
    case AudioManagerEvents.seekComplete:
      _position = AudioManager.instance.position;
      _slider = _position.inMilliseconds / _duration.inMilliseconds;
      setState(() {});
      print("seek event is completed. position is [$args]/ms");
      break;
    case AudioManagerEvents.buffering:
      print("buffering $args");
      break;
    case AudioManagerEvents.playstatus:
      isPlaying = AudioManager.instance.isPlaying;
      setState(() {});
      break;
    case AudioManagerEvents.timeupdate:
      _position = AudioManager.instance.position;
      _slider = _position.inMilliseconds / _duration.inMilliseconds;
      setState(() {});
      AudioManager.instance.updateLrc(args["position"].toString());
      break;
    case AudioManagerEvents.error:
      _error = args;
      setState(() {});
      break;
    case AudioManagerEvents.ended:
      AudioManager.instance.next();
      break;
    case AudioManagerEvents.volumeChange:
      _sliderVolume = AudioManager.instance.volume;
      setState(() {});
      break;
    default:
      break;
  }
});

}

Now when I’m playing next song for example setstate will refresh my whole app. How can I only rebuild a specific file of my project when event is triggered?

For example let’s say we have main.dart file home.dart , news.dart and on event AudioManagerEvents.next I only want setstate for my file News.dart, how to do this?

Thank you for the help!

@jeromexiong
Copy link
Owner

jeromexiong commented May 11, 2020 via email

@md186
Copy link
Author

md186 commented May 12, 2020

@jeromexiong thank you for the reply, sounds good. I’m using provider package from google, can you give me an example how it would look line with provider when we want to put AudioManagerEvents.instance.next in provider for example?

Would be great if you give me an example so I can learn from it

@md186 md186 closed this as completed May 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants