我正在开发一个Ffltter应用程序,其中我有一个管理文件上传过程的ChangeNotifier服务.此服务具有Boolean isUploding属性,可跟踪当前是否正在上载文件.我试图通过更改基于isUploding的FileViewPage小部件AppBar中的图标来反映UI中的上传状态.
我的应用程序 struct 在Main函数的顶层包含一个多提供者:
void main() { // Main function is the base of the app I need the icon change in the next page not here.
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => SharedFilesProvider()),
ChangeNotifierProvider(create: (_) => CloudService()),
],
child: MyApp(), // MyApp is the root widget of your application
),
);
}
在下一页(这是进入应用程序的几个导航步骤),我试图使用iscurrenting的状态在AppBar中的两个图标之间切换:
final isUpdating = context.watch<CloudService>().isUploading ?? false;
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
icon: Icon(
isUpdating ? Icons.cloud_upload_outlined : Icons.cloud_queue_outlined,
color: Colors.white,
),
onPressed: () => {},
),
],
),
);
在CloudService中,在presChanges方法中isUpload状态发生变化,该方法执行异步文件上传,并相应地更新isUploding:
class CloudService extends ChangeNotifier {
bool? _isUploading;
bool? get isUploading => _isUploading;
// Method to push changes, updates _isUploading and calls notifyListeners()
Future<void> pushChanges() async {
_isUploading = true;
notifyListeners();
// Simulated file upload process
await Future.delayed(Duration(seconds: 2));
_isUploading = false;
notifyListeners();
}
}
当isUpload更改时,图标不会动态更新.我希望图标在isUploading值为True时切换到Icon.Cloud_Upload_Outline,在值为False时切换回Icon.Cloud_Queue_Outline,但一旦加载页面,该图标就保持不变.
我执行了一次热重启,以确保这不是一个热重新加载问题,并且我已经通过调试确认,当isUpload更改时,可以像预期的那样调用NotifyListeners().
我想我无法将提供者与appBar连接起来. 上传时不会将False更改为True.