import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/views/wechat_mini_program/wx_public_widgets/custom_image_file_loader.dart';
import 'package:flyinsono/views/wechat_mini_program/wx_public_widgets/wx_scaffold.dart';
import 'package:get/get.dart';

import 'index.dart';

class WxReportPreviewPage extends FisView<WxReportPreviewController> {
  const WxReportPreviewPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    return Obx(() => ListView.builder(
          itemCount: controller.state.reportImagesList.length,
          itemBuilder: (context, index) {
            return KeepAliveReportImage(
              fileToken: controller.state.reportImagesList[index].fileUrl ?? '',
            );
          },
        ));
  }

  @override
  FWidget build(BuildContext context) {
    return FisBuilder<WxReportPreviewController>(
      builder: (_) {
        return WxScaffold(
          body: _buildView(),
          title: "报告预览",
        );
      },
    );
  }
}

/// 重写不会被销毁的报告预览控件
class KeepAliveReportImage extends StatefulWidget {
  final String fileToken;
  KeepAliveReportImage({
    Key? key,
    required this.fileToken,
  }) : super(key: key);
  @override
  _KeepAliveReportImageState createState() => _KeepAliveReportImageState();
}

class _KeepAliveReportImageState extends State<KeepAliveReportImage>
    with AutomaticKeepAliveClientMixin<KeepAliveReportImage> {
  // 实现 wantKeepAlive 方法并返回true，这样就能让子项保持活动状态
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context); // 确保将调用super.build(context)

    return Container(
      margin: EdgeInsets.only(bottom: 10),
      child: CustomImageLoader(
        fileToken: widget.fileToken,
      ),
    );
  }
}
