product_list.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:flutter/material.dart';
  2. import 'package:twong/api/index.dart';
  3. import 'package:twong/utils/index.dart';
  4. import 'package:twong/router/index.dart';
  5. import 'package:twong/models/index.dart';
  6. import 'package:twong/widgets/sliver_bar.dart';
  7. import 'package:twong/widgets/search_bar.dart';
  8. import 'package:twong/widgets/product_item.dart';
  9. class ProductListPage extends StatefulWidget {
  10. final String _keyword;
  11. ProductListPage(this._keyword, {Key key}): super(key: key);
  12. @override
  13. State<StatefulWidget> createState() {
  14. return _ProductListPageState(_keyword);
  15. }
  16. }
  17. class _ProductListPageState extends State<ProductListPage> {
  18. final String _keyword;
  19. ScrollController _scrollController = ScrollController();
  20. _ProductListPageState(this._keyword);
  21. Future<List<Product>> _getSearchData() {
  22. return Network.inst.search(_keyword);
  23. }
  24. Widget _buildList () {
  25. return FutureBuilder(
  26. future: _getSearchData(),
  27. builder: (BuildContext context, AsyncSnapshot snapshot) {
  28. if (snapshot.connectionState == ConnectionState.done) {
  29. List<Product> data = List<Product>();
  30. data = snapshot.data;
  31. if (data.length > 0) {
  32. return SliverPadding(
  33. padding: EdgeInsets.all(2),
  34. sliver: SliverGrid(
  35. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  36. crossAxisCount: 2,
  37. childAspectRatio: 5 / 6,
  38. ),
  39. delegate: SliverChildBuilderDelegate((context, index) =>
  40. ProductItem(index, data), childCount: data.length),
  41. ),
  42. );
  43. } else {
  44. return SliverToBoxAdapter(
  45. child: Container(
  46. padding: EdgeInsets.only(top: 200),
  47. child: Text('抱歉,没有找到您想要的宝贝!', textAlign: TextAlign.center,),
  48. )
  49. );
  50. }
  51. }else {
  52. return SliverToBoxAdapter(
  53. child: Container(
  54. padding: EdgeInsets.only(top: 200),
  55. child: Center(
  56. child: CircularProgressIndicator(),
  57. ),
  58. ),
  59. );
  60. }
  61. },
  62. );
  63. }
  64. @override
  65. Widget build(BuildContext context) {
  66. return Scaffold(
  67. appBar: SearchBar(GestureDetector(
  68. onTap: () { Navigator.pushNamed(context, RouteNames.home); },
  69. child: Container(
  70. child: Center(child: Icon(Icons.arrow_back_ios_outlined, color: Colors.white, size: 20.px)),
  71. ),
  72. )),
  73. body: CustomScrollView(
  74. controller: _scrollController,
  75. slivers: <Widget>[
  76. SliverBar(),
  77. _buildList(),
  78. ],
  79. ),
  80. floatingActionButton: Container(
  81. width: 40,
  82. height: 35,
  83. decoration: BoxDecoration(
  84. borderRadius: BorderRadius.circular(3),
  85. color: Color.fromRGBO(12, 12, 12, 0.6),
  86. ),
  87. child: IconButton(
  88. icon: Icon(Icons.arrow_upward, color: Colors.white),
  89. onPressed: () {
  90. _scrollController.animateTo(0, duration: Duration(seconds: 1), curve: Curves.easeInOut);
  91. },
  92. ),
  93. ),
  94. );
  95. }
  96. }