/*
 * @Author: WuWei
 * @Date: 2020-05-14 11:24:05
 * @LastEditors: wjc
 * @LastEditTime: 2023-04-03 11:56:08
 * @Description:
 */

import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/news_list_store.dart';

class IndexPage extends StatelessWidget {
  const IndexPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => NewsListStore(context),
      child: _PageView(),
      lazy: false,
    );
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  NewsListStore get store => Provider.of(context, listen: false);
  @override
//void initState() {
//super.initState();
// Future.microtask(() {
//store.xxx();
// });
//}

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText('${store.args['appBarTitle']}',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: WisScrollView(
        key: store.scorllViewKey,
        // onRefresh: () => store.getList(isLoad: false),
        onInit: () => store.getList(isLoad: false),
        onLoad: () => store.getList(),
        slivers: <Widget>[
          _ListComponent(),
        ],
      ),
    );
  }
}

class _ListComponent extends StatelessWidget {
  const _ListComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<NewsListStore>(context);
    return WisSliverList.childBuilder(
      childCount: store.list.length,
      builder: (_, i) {
        var item = store.list[i];
        return InkWell(
          onTap: () => store.route2detail(item),
          borderRadius: BorderRadius.circular(5.pt),
          child: WisCard(
            margin: EdgeInsets.only(left: 15.pt, right: 15.pt, top: 10.pt),
            color: Colors.white,
            child: Wisdom.column(
              padding: EdgeInsets.all(20.pt),
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 11.5.pt),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Expanded(
                        child: WisText.c000(item.title ?? '',
                            size: 18.pt, bold: true),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 24.5.pt),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      WisText.c000(item.publishTime ?? '',
                          size: 14.pt, color: Color.fromRGBO(153, 153, 153, 1)),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.fromLTRB(0, 0, 0, 23.pt),
                  width: MediaQuery.of(context).size.width,
                  child: WImage.network(
                    (UrlFunc.urlHasMatch(item.thumb!)
                        ? item.thumb
                        : AppParseUtil.image('${item.thumb}'))!,
                    height: 163.pt,
                    fit: BoxFit.fill,
                    borderRadius: BorderRadius.circular(4.0),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 18.5.pt),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Expanded(
                        child: WisText.c000(
                          item.summary ?? '',
                          size: 15.pt,
                          color: Color.fromRGBO(102, 102, 102, 1),
                        ),
                      ),
                    ],
                  ),
                ),
                Divider(),
                Container(
                  padding: EdgeInsets.fromLTRB(0, 15.pt, 0, 0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      WisText('查看详情', size: 14.pt, color: Color(0xff1E95FF)),
                      Icon(Icons.chevron_right, color: Color(0xff1E95FF)),
                    ],
                  ),
                )
              ],
            ),
          ),
        );
      },
    );
  }
}
