product_list.dart 3.2 KB

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