gpt4 book ai didi

android - 如何用提供者的值(value)更新其他页面?

转载 作者:行者123 更新时间:2023-12-03 04:34:09 26 4
gpt4 key购买 nike

如何使用主选项卡页中提供的提供程序值来更新另一页?使用提供程序将进度值传递到第二个选项卡。 secondtab无法更新。怎么了?哪些可以纠正?
这是main.dart文件:

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<ShareProgress>(
create: (_) => ShareProgress(),
child: MaterialApp(
title: 'VideoTube',
theme: ThemeData(
primarySwatch: Colors.blue,
accentColor: Colors.red[400]
),
debugShowCheckedModeBanner: false,
home: App(),
),
);
}
}
这是ShareProgress.dart文件:
class ShareProgress extends ChangeNotifier {
int _progress = 0;



void setprogress(int newProgress) {
_progress = newProgress;

notifyListeners();
}

int get progress => _progress;
}
这是主标签:
onReceiveProgress: (count, total) {
var percentage = count / total * 100;



ShareProgress().setprogress(count);
setState(() {
downloadMessage = 'downloadin.... ${percentage.floor()}%';


// print(downloadMessage);
});
ShareProgress().setprogress(count);
},
这是第二个选项卡:
class _SecondTabState extends State<SecondTab> {
@override
Widget build(BuildContext context) {
final progress = Provider.of<ShareProgress>(context);
print(progress.progress);
return Container(
child: Text('second ${progress.progress}'),
);
}
}

最佳答案

您必须使用使用者来重建窗口小部件:

class _SecondTabState extends State<SecondTab> {
@override
Widget build(BuildContext context) {
return Consumer<KeyboardNotifier>(
builder: (context, value, _) => Container(
child: Text('second ${value.progress}'),
),
);
}
}

关于android - 如何用提供者的值(value)更新其他页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64396109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com