
import 'package:flutter/material.dart';

import 'package:twong/api/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/router/index.dart';
import 'package:twong/models/index.dart';
import 'package:twong/widgets/sliver_bar.dart';
import 'package:twong/widgets/search_bar.dart';
import 'package:twong/widgets/product_item.dart';

class ProductListPage extends StatefulWidget {
  final String _keyword;

  ProductListPage(this._keyword, {Key key}): super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _ProductListPageState(_keyword);
  }
}

class _ProductListPageState extends State<ProductListPage> {
  final String _keyword;
  ScrollController _scrollController = ScrollController();

  _ProductListPageState(this._keyword);

  Future<List<Product>> _getSearchData() {
    return Network.inst.search(_keyword);
  }

  Widget _buildList () {
    return FutureBuilder(
      future: _getSearchData(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          List<Product> data = List<Product>();
          data = snapshot.data;
          if (data.length > 0) {
            return SliverPadding(
              padding: EdgeInsets.all(2),
              sliver: SliverList(
                delegate: SliverChildBuilderDelegate((context, index) =>
                    ProductItem(data[index]), childCount: data.length),
              ),
            );
          } else {
            return SliverToBoxAdapter(
                child: Container(
                  padding: EdgeInsets.only(top: 200),
                  child: Text('抱歉，没有找到您想要的宝贝！', textAlign: TextAlign.center,),
                )
            );
          }
        }else {
          return SliverToBoxAdapter(
            child: Container(
              padding: EdgeInsets.only(top: 200),
              child: Center(
                child: Utils.loadingWidget,
              ),
            ),
          );
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: DColors.back,
      appBar: SearchBar(GestureDetector(
        onTap: () {
          Navigator.popUntil(context, ModalRoute.withName(RouteNames.home));
        },
        child: Container(
          child: Center(child: Icon(
              Icons.arrow_back_ios_outlined, color: Colors.white, size: 20.px)),
        ),
      )),
      body: CustomScrollView(
        controller: _scrollController,
        slivers: <Widget>[
          SliverBar(),
          _buildList(),
        ],
      ),
      floatingActionButton: InkWell(
        onTap: () => _scrollController.animateTo(0, curve: Curves.ease,
                duration: Duration(milliseconds: 600)),
        child: Container(
          width: 32.px,
          height: 32.px,
          child: Icon(
              Icons.keyboard_arrow_up_sharp, color: Colors.white),
          decoration: BoxDecoration(
              color: Color.fromARGB(128, 243, 0, 0),
              borderRadius: BorderRadius.circular(10.px)
          ),
        ),
      ),
    );
  }
}