/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-07-20 17:49:20
 * @LastEditTime : 2021-07-20 18:10:05
 * @Description  : 任务管理--顶部滚动消息
 */
part of '../index.dart';

class _BuildNotice extends StatefulWidget {
  _BuildNotice({Key? key, this.count, this.itemBuilder}) : super(key: key);
  final int? count; // 子视图数量
  final IndexedWidgetBuilder? itemBuilder; // 子视图构造器

  @override
  _BuildNoticeState createState() => _BuildNoticeState();
}

class _BuildNoticeState extends State<_BuildNotice> {
  PageController? _controller;
  late Timer _timer;

  @override
  void initState() {
    super.initState();

    _controller = PageController();
    _timer = Timer.periodic(Duration(seconds: 2), (timer) {
      // 如果当前位于最后一页，则直接跳转到第一页，两者内容相同，跳转时视觉上无感知
      if (_controller!.page!.round() >= widget.count!) {
        _controller!.jumpToPage(0);
      }
      _controller!.nextPage(
          duration: Duration(seconds: 1), curve: Curves.linear);
    });
  }

  @override
  Widget build(BuildContext context) {
    return PageView.builder(
      scrollDirection: Axis.vertical,
      controller: _controller,
      itemBuilder: (buildContext, index) {
        if (index < widget.count!) {
          return widget.itemBuilder!(buildContext, index);
        } else {
          return widget.itemBuilder!(buildContext, 0);
        }
      },
      itemCount: widget.count! + 1, // 在原数据末尾添加一笔数据(即第一笔数据)，用于实现无限循环滚动效果
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller!.dispose();
    _timer.cancel();
  }
}
