upload_page.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:chicken_farm/core/services/upload_service.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:go_router/go_router.dart';
  4. class UploadPage extends StatefulWidget {
  5. const UploadPage({super.key});
  6. @override
  7. State<UploadPage> createState() => _UploadPageState();
  8. }
  9. class _UploadPageState extends State<UploadPage> {
  10. late UploadService _uploadService;
  11. int _totalOperations = 0;
  12. int _uploadedOperations = 0;
  13. String _status = '准备上传...';
  14. bool _isUploading = false;
  15. bool _uploadCompleted = false;
  16. @override
  17. void initState() {
  18. super.initState();
  19. _uploadService = UploadService();
  20. _startUpload();
  21. }
  22. Future<void> _startUpload() async {
  23. setState(() {
  24. _isUploading = true;
  25. _status = '开始上传...';
  26. });
  27. try {
  28. await _uploadService.uploadPendingOperations(
  29. context: context, // 传递 context 给 upload service
  30. onProgress: (uploaded, total, status) {
  31. setState(() {
  32. _uploadedOperations = uploaded;
  33. _totalOperations = total;
  34. _status = status;
  35. });
  36. },
  37. onComplete: () {
  38. setState(() {
  39. _isUploading = false;
  40. _uploadCompleted = true;
  41. _status = '上传完成';
  42. });
  43. },
  44. onError: (message) {
  45. setState(() {
  46. _isUploading = false;
  47. _status = '上传出错: $message';
  48. });
  49. },
  50. );
  51. } catch (e) {
  52. setState(() {
  53. _isUploading = false;
  54. _status = '上传异常: ${e.toString()}';
  55. });
  56. }
  57. }
  58. // 后台上传
  59. void _backgroundUpload() {
  60. // 返回上一页并传递false表示后台上传
  61. context.pop(false);
  62. }
  63. @override
  64. Widget build(BuildContext context) {
  65. return PopScope(
  66. canPop: !_isUploading, // 只有在不上传时才能返回
  67. child: Scaffold(
  68. appBar: AppBar(
  69. title: const Text('数据上传'),
  70. automaticallyImplyLeading: false, // 禁用返回按钮
  71. ),
  72. body: Padding(
  73. padding: const EdgeInsets.all(16.0),
  74. child: Column(
  75. mainAxisAlignment: MainAxisAlignment.center,
  76. children: [
  77. Text(
  78. _status,
  79. style: Theme.of(context).textTheme.headlineSmall,
  80. textAlign: TextAlign.center,
  81. ),
  82. const SizedBox(height: 30),
  83. if (_totalOperations > 0) ...[
  84. LinearProgressIndicator(
  85. value: _uploadedOperations / _totalOperations,
  86. minHeight: 10,
  87. ),
  88. const SizedBox(height: 10),
  89. Text('$_uploadedOperations / $_totalOperations'),
  90. const SizedBox(height: 30),
  91. ],
  92. if (_isUploading) ...[
  93. ElevatedButton(
  94. onPressed: _backgroundUpload,
  95. child: const Text('后台上传'),
  96. ),
  97. ] else if (_uploadCompleted) ...[
  98. ElevatedButton(
  99. onPressed: () {
  100. context.pop(true); // 返回上一页并传递结果
  101. },
  102. child: const Text('返回'),
  103. ),
  104. ] else ...[
  105. ElevatedButton(
  106. onPressed: () {
  107. context.pop(false); // 返回上一页并传递结果
  108. },
  109. child: const Text('退出'),
  110. ),
  111. ],
  112. ],
  113. ),
  114. ),
  115. ),
  116. );
  117. }
  118. }