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

标签 android flutter dart provider

如何使用主选项卡页中提供的提供程序值来更新另一页?使用提供程序将进度值传递到第二个选项卡。 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/

相关文章:

android - 在 Android 上的 wifidirect 中更改设备名称

android - 具有多行项目的微调器与 Froyo 上的选定项目显示重叠

flutter - 如何在 flutter 中向圆形容器添加线性渐变?

dart - 如何在执行 POST 请求时解决 Flutter CERTIFICATE_VERIFY_FAILED 错误?

flutter - 我需要在选项卡中添加一些东西 我应该在代码中的什么地方添加

Android 模拟器相机 - 网络摄像头方向

android - 使用 "if (convertView == null)"时 GridView 和图标困惑

flutter - 文档与集合

firebase - 删除云 Firestore 文档中的特定字段时出错

php - 我的 PHP/Mysql JSON 输出格式与 flutter 应用程序所需的示例 JSON 页面的格式不同