GoodsPage.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_markdown/flutter_markdown.dart';
  3. import 'package:flutter_habit/common/I18N.dart';
  4. import 'package:flutter_habit/common/components/PopMenus.dart';
  5. import 'package:flutter_habit/provider/UserProvider.dart';
  6. import 'package:flutter_habit/network/Repository.dart';
  7. import 'package:provider/provider.dart';
  8. class GoodsPage extends StatefulWidget {
  9. final dynamic data;
  10. GoodsPage(this.data);
  11. @override
  12. _GoodsPageState createState() => _GoodsPageState();
  13. }
  14. class _GoodsPageState extends State<GoodsPage> {
  15. late bool isRequesting;
  16. @override
  17. void initState() {
  18. super.initState();
  19. isRequesting = false;
  20. }
  21. @override
  22. Widget build(BuildContext context) {
  23. UserProvider userProvider = Provider.of<UserProvider>(context);
  24. return Scaffold(
  25. appBar: AppBar(
  26. title: Text(I18N.of("商品详情")),
  27. actions: userProvider.token == null
  28. ? []
  29. : <Widget>[
  30. Row(
  31. children: <Widget>[
  32. Text(
  33. "${userProvider.coins} x ",
  34. style: TextStyle(fontSize: 16),
  35. ),
  36. Icon(Icons.monetization_on),
  37. Text(" "),
  38. ],
  39. ),
  40. ],
  41. ),
  42. body: Padding(
  43. padding: EdgeInsets.all(16),
  44. child: Column(
  45. crossAxisAlignment: CrossAxisAlignment.start,
  46. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  47. children: <Widget>[
  48. isRequesting ? LinearProgressIndicator() : Container(),
  49. Padding(
  50. padding: EdgeInsets.only(top: 10, bottom: 10),
  51. child: Container(
  52. padding: EdgeInsets.all(10),
  53. height: 300,
  54. decoration: BoxDecoration(
  55. border: Border.all(
  56. color: Theme.of(context).unselectedWidgetColor,
  57. ),
  58. borderRadius: BorderRadius.circular(10),
  59. ),
  60. child: Stack(
  61. children: <Widget>[
  62. Markdown(
  63. data: widget.data["description"],
  64. ),
  65. Row(
  66. mainAxisAlignment: MainAxisAlignment.end,
  67. children: <Widget>[
  68. IconButton(
  69. icon: Icon(
  70. Icons.zoom_out_map,
  71. color: Theme.of(context).colorScheme.secondary,
  72. ),
  73. onPressed: () =>
  74. Navigator.of(context).push(MaterialPageRoute(
  75. builder: (_) => Scaffold(
  76. appBar: AppBar(
  77. title: Text(
  78. widget.data["name"].toString()),
  79. ),
  80. body: Markdown(
  81. data: widget.data["description"]
  82. .toString(),
  83. ),
  84. ))),
  85. ),
  86. ],
  87. ),
  88. ],
  89. ),
  90. ),
  91. ),
  92. Row(
  93. mainAxisAlignment: MainAxisAlignment.start,
  94. crossAxisAlignment: CrossAxisAlignment.center,
  95. children: <Widget>[
  96. Text(
  97. "${widget.data["price"].toString()} ",
  98. style: TextStyle(fontSize: 34),
  99. ),
  100. Icon(
  101. Icons.monetization_on,
  102. size: 30,
  103. ),
  104. ],
  105. ),
  106. Text(
  107. "[${I18N.of("官方合作")}] ${widget.data["name"].toString()}",
  108. maxLines: 2,
  109. style: TextStyle(fontSize: 20),
  110. ),
  111. Text(
  112. "${I18N.of("库存")} : ${widget.data["count"].toString()}",
  113. ),
  114. Row(
  115. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  116. children: <Widget>[
  117. Container(),
  118. Column(
  119. children: <Widget>[
  120. ElevatedButton(
  121. style: ButtonStyle(
  122. backgroundColor: MaterialStateProperty.all(
  123. Theme.of(context).colorScheme.secondary),
  124. foregroundColor: MaterialStateProperty.all(
  125. Theme.of(context).cardColor)),
  126. child: Text(userProvider.token == null
  127. ? I18N.of("请先登录")
  128. : I18N.of("立即获取优惠口令")),
  129. onPressed: userProvider.token == null || isRequesting
  130. ? null
  131. : () async {
  132. UserProvider userProvider =
  133. Provider.of<UserProvider>(context,
  134. listen: false);
  135. isRequesting = true;
  136. setState(() {});
  137. bool isSuccess = await Repository.getInstance()!
  138. .buyGoods(context, userProvider.token,
  139. userProvider.uid, widget.data["goodsId"]);
  140. if (isSuccess) {
  141. userProvider.coins =
  142. await Repository.getInstance()!
  143. .getCoin(userProvider.uid);
  144. userProvider.refresh();
  145. await PopMenus.attention(
  146. context: context,
  147. content:
  148. Text(I18N.of("感谢您的支持,优惠口令已发送至您的邮箱")));
  149. }
  150. isRequesting = false;
  151. setState(() {});
  152. },
  153. ),
  154. Text(
  155. I18N.of("重复购买不会重复扣费"),
  156. style: Theme.of(context).textTheme.bodySmall,
  157. ),
  158. ],
  159. ),
  160. ],
  161. )
  162. ],
  163. ),
  164. ),
  165. );
  166. }
  167. }