如何使用提供者值更新其他页面?
How can other page be updated with provider value?
如何使用主标签页提供的提供者值更新其他页面?使用提供程序将进度值传递到第二个选项卡。第二个标签无法更新。怎么了?什么可以纠正?
这是 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}'),
),
);
}
}
如何使用主标签页提供的提供者值更新其他页面?使用提供程序将进度值传递到第二个选项卡。第二个标签无法更新。怎么了?什么可以纠正?
这是 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}'),
),
);
}
}