Flutter 如何在课堂上使用setState或其他东西
颤振状态并没有随班级而改变。我从类中获取布尔值,但问题是它并没有改变。我需要刷新或保存屏幕,然后才能看到更改 我的代码Flutter 如何在课堂上使用setState或其他东西,flutter,dart,Flutter,Dart,颤振状态并没有随班级而改变。我从类中获取布尔值,但问题是它并没有改变。我需要刷新或保存屏幕,然后才能看到更改 我的代码 class _PlayerScreenState extends State<PlayerScreen> { @override void initState() { super.initState(); context.read<PlayerProvider>().link = widget.episode.contentUrl
class _PlayerScreenState extends State<PlayerScreen> {
@override
void initState() {
super.initState();
context.read<PlayerProvider>().link = widget.episode.contentUrl;
context.read<PlayerProvider>().playerLength = widget.episode.duration;
context.read<PlayerProvider>().episodeName = widget.episode.title;
context.read<PlayerProvider>().episodeThumbnail =
widget.podcastInfo.artworkUrl100;
context.read<PlayerProvider>().episode = widget.episode;
context.read<PlayerProvider>().podcastInfo = widget.podcastInfo;
if (widget.startAgain) context.read<PlayerProvider>().play();
}
@override
Widget build(BuildContext context) {
return BaseScreen(
//Dont show Bottom Nav Bar
showNavbar: false,
child: Consumer<PlayerProvider>(
builder: (context, model, child) {
// model.link = widget.episode.contentUrl;
// model.playerLength = widget.episode.duration;
// model.play();
return Container(
child: Column(
children: [
//Player Controls
Container(
padding: EdgeInsets.fromLTRB(40, 15, 40, 10),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20),
topLeft: Radius.circular(20)),
),
height: 160,
child: Column(
children: [
//Progree bar
Row(
children: [
//current time
Text(
// "01:27",
model.playerTimeNow.inMinutes.toString() +
":" +
(model.playerTimeNow.inSeconds % 60).toString(),
style: TextStyle(
fontSize: 13,
color: Colors.white,
),
),
//Slider
Expanded(
child: Slider(
activeColor: Color(0xffe7ad29),
inactiveColor: Color(0xFF707070),
value: model.playerBarValue,
onChanged: (val) {
model.seekFromBar(val);
},
),
),
//Remaining time
Text(
model.playerLength.inMinutes.toString() +
":" +
(model.playerLength.inSeconds % 60).toString(),
style: TextStyle(
fontSize: 13,
color: Colors.white,
),
),
],
),
Container(
child: Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
iconSize: 70,
icon: Container(
width: 70,
height: 70,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(70),
),
border: Border.all(
color: Color(0xffe7ad29),
),
),
child: Icon(
model.isPlaying
? Icons.pause
: Icons.play_arrow,
color: Colors.white,
size: 40,
),
),
onPressed: () {
if (model.isPlaying) {
model.pause();
print("Paused");
} else {
model.resume();
print("Played");
}
},
),
//Next Button
],
),
),
)
],
),
),
],
),
);
},
),
);
}
}
class\u播放器屏幕状态扩展状态{
@凌驾
void initState(){
super.initState();
context.read().link=widget.插曲.contentUrl;
context.read().playerLength=widget.eption.duration;
context.read();
context.read().eposode缩略图=
widget.podcastInfo.artworkUrl100;
context.read();
context.read().podcastInfo=widget.podcastInfo;
if(widget.startAgain)context.read().play();
}
@凌驾
小部件构建(构建上下文){
返回基本屏幕(
//不显示底部导航栏
showNavbar:false,
儿童:消费者(
生成器:(上下文、模型、子对象){
//model.link=widget.插曲.contentUrl;
//model.playerLength=widget.eption.duration;
//model.play();
返回容器(
子:列(
儿童:[
//玩家控制
容器(
填充:来自LTRB(40,15,40,10)的边缘设置,
装饰:盒子装饰(
颜色:颜色,黑色,
borderRadius:仅限borderRadius(
右上角:半径。圆形(20),
左上:半径。圆形(20)),
),
身高:160,
子:列(
儿童:[
//进度条
划船(
儿童:[
//当前时间
正文(
// "01:27",
model.playerTimeNow.inMinutes.toString()中的+
":" +
(model.playerTimeNow.unseconds%60).toString(),
样式:TextStyle(
尺寸:13,
颜色:颜色,白色,
),
),
//滑块
扩大(
子:滑块(
activeColor:Color(0xffe7ad29),
不活动颜色:颜色(0xFF707070),
值:model.playerBarValue,
一旦更改:(val){
模型。参见“滚动条”(val);
},
),
),
//剩余时间
正文(
model.playerLength.inMinutes.toString()+
":" +
(model.playerLength.unseconds%60).toString(),
样式:TextStyle(
尺寸:13,
颜色:颜色,白色,
),
),
],
),
容器(
儿童:扩大(
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
图标按钮(
iconSize:70,
图标:容器(
宽度:70,
身高:70,
装饰:盒子装饰(
borderRadius:borderRadius.all(
圆形半径(70),
),
边界:边界(
颜色:颜色(0xffe7ad29),
),
),
子:图标(
模型显示
?图标。暂停
:Icons.play_箭头,
颜色:颜色,白色,
尺码:40,
),
),
已按下:(){
if(显示模式){
model.pause();
打印(“暂停”);
}否则{
model.resume();
打印(“播放”);
}
},
),
//下一个按钮
],
),
),
)
],
),
),
],
),
);
},
),
);
}
}
你可以看到我正在显示暂停,如果isPlaying为true,如果为false,则显示其他图标
它来自另一个班级
class PlayerProvider extends ChangeNotifier {
final player = AssetsAudioPlayer();
String link;
Duration playerTimeNow = Duration(seconds: 0);
Duration playerLength;
double playerBarValue = 0.0;
Episode episode;
Item podcastInfo;
String episodeName, episodeThumbnail;
bool isPlaying = false;
PlayerProvider() {
updateState();
}
play() async {
print("Started Playing");
// Stop previous playing
player.stop();
playerTimeNow = Duration(seconds: 0);
isPlaying = false;
// link = updateLinkToHttps(link);
print(link);
final audio = Audio.network(
link,
metas: Metas(
title: podcastInfo.collectionName,
artist: podcastInfo.artistName,
album: podcastInfo.trackName,
image: MetasImage.network(
podcastInfo.artworkUrl600), //can be MetasImage.network
),
);
var duration = await player.open(
audio,
showNotification: true,
notificationSettings: NotificationSettings(),
);
isPlaying = true;
// player.play(); // Usually you don't want to wait for playback to finish.
print("started");
}
pause() async {
await player.pause();
isPlaying = false;
print("paused");
}
resume() async {
//TODO: Setup resume
await player.seek(playerTimeNow);
player.play();
isPlaying = true;
}
speed(double val) async {
print(val);
//TODO: Setup resume
await player.forwardOrRewind(val);
isPlaying = true;
}
updateState() {
player.currentPosition.listen((event) {
playerTimeNow = event;
updatePlayerBar();
});
}
updatePlayerBar() {
int totalLengthInMilliSeconds = playerLength.inMilliseconds;
int totalPlayedInMilliSeconds = playerTimeNow.inMilliseconds;
double newPlayerBarValue =
totalPlayedInMilliSeconds / totalLengthInMilliSeconds;
playerBarValue = newPlayerBarValue;
notifyListeners();
}
forward() async {
//TODO: Check if at-least 10 seconds are left;
if (playerTimeNow + Duration(seconds: 10) < playerLength)
await player.seek(playerTimeNow + Duration(seconds: 10));
else
await player.seek(playerLength);
print("Forwarded 10 seconds");
}
backword() async {
Duration back = playerTimeNow.inSeconds > 10
? playerTimeNow - Duration(seconds: 10)
: Duration(seconds: 0);
await player.seek(back);
print("Backwarded 10 seconds");
}
seekFromBar(double val) async {
double totalMillis = playerLength.inMilliseconds * val;
int newMillis = totalMillis.toInt();
Duration newSeekLocations = Duration(milliseconds: newMillis);
await player.seek(newSeekLocations);
print("Seek from Bar");
}
setState() {
notifyListeners();
}
}
class PlayerProvider扩展了ChangeNotifier{
最终玩家=AssetAudioPlayer();
串链;
持续时间playerTimeNow=持续时间(秒:0);
持续时间播放长度;
双人播放器右值=0.0;
插曲;
项目信息;
字符串eposodename,eposode缩略图;
布尔显示=假;
PlayerProvider(){
不动产();
}
play()异步{
打印(“开始播放”);
//停止以前的播放
player.stop();
playerTimeNow=持续时间(秒:0);
isplay=false;
//link=更新的linktohtps(link);
打印(链接);
final provider = Provider.of<PlayerProvider>(context);