bookshelf_scene.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:shuqi/public.dart';
  4. import 'bookshelf_item_view.dart';
  5. import 'bookshelf_header.dart';
  6. class BookshelfScene extends StatefulWidget {
  7. @override
  8. State<StatefulWidget> createState() => BookshelfState();
  9. }
  10. class BookshelfState extends State<BookshelfScene> with RouteAware {
  11. List<Novel> favoriteNovels = [];
  12. ScrollController scrollController = ScrollController();
  13. double navAlpha = 0;
  14. @override
  15. void initState() {
  16. super.initState();
  17. fetchData();
  18. scrollController.addListener(() {
  19. var offset = scrollController.offset;
  20. if (offset < 0) {
  21. if (navAlpha != 0) {
  22. setState(() {
  23. navAlpha = 0;
  24. });
  25. }
  26. } else if (offset < 50) {
  27. setState(() {
  28. navAlpha = 1 - (50 - offset) / 50;
  29. });
  30. } else if (navAlpha != 1) {
  31. setState(() {
  32. navAlpha = 1;
  33. });
  34. }
  35. });
  36. }
  37. Future<void> fetchData() async {
  38. try {
  39. List<Novel> favoriteNovels = [];
  40. List<dynamic> favoriteResponse = await Request.get(action: 'bookshelf');
  41. favoriteResponse.forEach((data) {
  42. favoriteNovels.add(Novel.fromJson(data));
  43. });
  44. setState(() {
  45. this.favoriteNovels = favoriteNovels;
  46. });
  47. } catch (e) {
  48. Toast.show(e.toString());
  49. }
  50. }
  51. Widget buildActions(Color iconColor) {
  52. return Row(children: <Widget>[
  53. Container(
  54. height: kToolbarHeight,
  55. width: 44,
  56. child:
  57. Image.asset('assets/img/actionbar_checkin.png', color: iconColor),
  58. ),
  59. Container(
  60. height: kToolbarHeight,
  61. width: 44,
  62. child: Image.asset('assets/img/actionbar_search.png', color: iconColor),
  63. ),
  64. SizedBox(width: 15)
  65. ]);
  66. }
  67. Widget buildNavigationBar() {
  68. return Stack(
  69. children: <Widget>[
  70. Positioned(
  71. right: 0,
  72. child: Container(
  73. margin: EdgeInsets.fromLTRB(5, Screen.topSafeHeight, 0, 0),
  74. child: buildActions(SQColor.white),
  75. ),
  76. ),
  77. Opacity(
  78. opacity: navAlpha,
  79. child: Container(
  80. padding: EdgeInsets.fromLTRB(5, Screen.topSafeHeight, 0, 0),
  81. height: Screen.navigationBarHeight,
  82. color: SQColor.white,
  83. child: Row(
  84. children: <Widget>[
  85. SizedBox(width: 103),
  86. Expanded(
  87. child: Text(
  88. '书架',
  89. style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
  90. textAlign: TextAlign.center,
  91. ),
  92. ),
  93. buildActions(SQColor.darkGray),
  94. ],
  95. ),
  96. ),
  97. )
  98. ],
  99. );
  100. }
  101. Widget buildFavoriteView() {
  102. if (favoriteNovels.length <= 1) {
  103. return Container();
  104. }
  105. List<Widget> children = [];
  106. var novels = favoriteNovels.sublist(1);
  107. novels.forEach((novel) {
  108. children.add(BookshelfItemView(novel));
  109. });
  110. var width = (Screen.width - 15 * 2 - 24 * 2) / 3;
  111. children.add(GestureDetector(
  112. onTap: () {
  113. eventBus.emit(EventToggleTabBarIndex, 1);
  114. },
  115. child: Container(
  116. color: SQColor.paper,
  117. width: width,
  118. height: width / 0.75,
  119. child: Image.asset('assets/img/bookshelf_add.png'),
  120. ),
  121. ));
  122. return Container(
  123. padding: EdgeInsets.fromLTRB(15, 20, 15, 15),
  124. child: Wrap(
  125. spacing: 23,
  126. children: children,
  127. ),
  128. );
  129. }
  130. @override
  131. Widget build(BuildContext context) {
  132. return Scaffold(
  133. backgroundColor: SQColor.white,
  134. body: AnnotatedRegion(
  135. value: navAlpha > 0.5 ? SystemUiOverlayStyle.dark : SystemUiOverlayStyle.light,
  136. child: Stack(children: [
  137. RefreshIndicator(
  138. onRefresh: fetchData,
  139. child: ListView(
  140. padding: EdgeInsets.only(top: 0),
  141. controller: scrollController,
  142. children: <Widget>[
  143. favoriteNovels.length > 0 ? BookshelfHeader(favoriteNovels[0]) : Container(),
  144. buildFavoriteView(),
  145. ],
  146. ),
  147. ),
  148. buildNavigationBar(),
  149. ]),
  150. ),
  151. );
  152. }
  153. }