import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
// import 'package:flutter_svg/flutter_svg.dart';
import 'package:gmalpha_flutter/NewMessageModel/page/AttentionModel.dart';
import 'package:gmalpha_flutter/NewMessageModel/page/AttentionListItem.dart';
import 'package:gmalpha_flutter/NewMessageModel/service/remote/entity/AttentionEntity.dart';

class AttentionPage extends StatefulWidget {
  AttentionModel _model;
  AttentionPage() {
    _model = AttentionModel(1, 10);
  }
  @override
  _AttentionPageState createState() => _AttentionPageState(_model);
}

class _AttentionPageState extends State<AttentionPage> {
  List _noticeLists = [];
  List _newList = [];
  AttentionModel _model;
  _AttentionPageState(this._model);

  ScrollController _scrollController = ScrollController();
  bool isPerformingRequest = false;

  @override
  void reassemble() {
    super.reassemble();
    _model.init(context);
    setState(() {
      _noticeLists = [];
    });
  }

  @override
  void initState() {
    super.initState();
    _model.init(context);
    _scrollController.addListener(() {
      if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
        _model.more(context);
      }
    });
  }

  Widget buildNoticeitem(BuildContext context, int index) {
    if (index == _noticeLists.length && _newList.length == 0) {
      return Padding(
        padding: const EdgeInsets.all(10.0),
        child: Center(
          child: Text('暂时无更多数据'),
        ),
      );
    }
    if (index == _noticeLists.length) {
      return _buildProgressIndicator();
    }
    return Padding(
      padding: const EdgeInsets.only(bottom: 21.0),
      child: AttentionListItem(_noticeLists[index]),
    );
  }

  Widget _buildProgressIndicator() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            width: 15.0,
            height: 15.0,
            child: CircularProgressIndicator(strokeWidth: 2.0),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 5.0),
            child: Text('加载中...'),
          )
        ],
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        leading: GestureDetector(
          onTap: () {
            FlutterBoost.singleton.closePageForContext(context);
          },
          child: Center(
            // child: SvgPicture.asset("images/left_arrow.svg", color: Color(0xff323232)),
          ),
        ),
      ),
      body: StreamBuilder<AttentionEntity>(
        stream: _model.zzgLive.stream,
        initialData: _model.zzgLive.data,
        builder: (context, data) {
          _newList = data.data.data;
          for (var item in data.data.data) {
            _noticeLists.add(item);
          }
          isPerformingRequest = false;
          return ListView.builder(
            controller: _scrollController,
            itemCount: _noticeLists.length + 1,
            itemBuilder: buildNoticeitem
          );
        },
      )
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}