history.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../constants.dart';
  4. import '../utils/api.dart';
  5. import '../utils/http_utils.dart';
  6. import '../utils/size_fit.dart';
  7. import 'component/top_container.dart';
  8. class History extends StatefulWidget {
  9. const History({Key? key}) : super(key: key);
  10. @override
  11. State<History> createState() => _HistoryState();
  12. }
  13. class _HistoryState extends State<History> {
  14. List histories = [];
  15. @override
  16. void initState() {
  17. // TODO: implement initState
  18. getDatas();
  19. super.initState();
  20. }
  21. Future getDatas() async {
  22. var data = await HttpUtils.get(Api.history);
  23. setState(() {
  24. histories = data['data'];
  25. });
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. SizeFit.initialize(context);
  30. return Material(
  31. color: kBgColor,
  32. child: TopContainer(
  33. child: Stack(children: [
  34. SingleChildScrollView(
  35. child: Column(
  36. children: [
  37. ...List<Widget>.generate(histories.length, (index) {
  38. return HistoryItem(data: histories[index]);
  39. })
  40. ],
  41. ),
  42. )
  43. ])));
  44. }
  45. }
  46. class HistoryItem extends StatefulWidget {
  47. var data;
  48. HistoryItem({Key? key, required this.data}) : super(key: key);
  49. @override
  50. State<HistoryItem> createState() => _HistoryItemState();
  51. }
  52. class _HistoryItemState extends State<HistoryItem>
  53. with SingleTickerProviderStateMixin {
  54. double scale = 1.0;
  55. late AnimationController controller;
  56. late Animation animation;
  57. @override
  58. void initState() {
  59. controller = AnimationController(
  60. vsync: this, duration: const Duration(milliseconds: 100));
  61. animation = Tween(begin: 0.0, end: 1.0)
  62. .animate(CurvedAnimation(parent: controller, curve: Curves.easeOut));
  63. controller.addListener(() {
  64. setState(() {});
  65. });
  66. super.initState();
  67. }
  68. @override
  69. void dispose() {
  70. controller.dispose();
  71. super.dispose();
  72. }
  73. formatTime(DateTime dateTime) {
  74. DateTime now = DateTime.now();
  75. int minutes = (now.millisecondsSinceEpoch ~/ 1000 -
  76. dateTime.millisecondsSinceEpoch ~/ 1000) ~/
  77. 60;
  78. if (minutes < 1) {
  79. return '刚刚';
  80. } else if (minutes <= 59) {
  81. return minutes.toString() + '分钟前';
  82. } else if (minutes <= 60 * 24 - 1) {
  83. return (minutes ~/ 60).toString() + '小时前';
  84. } else if (minutes <= 60 * 24 * 3 - 1) {
  85. return (minutes ~/ (60 * 24)).toString() + '天前';
  86. }
  87. return dateTime.year.toString() +
  88. '-' +
  89. dateTime.month.toString().padLeft(2, "0") +
  90. '-' +
  91. dateTime.day.toString().padLeft(2, "0");
  92. }
  93. @override
  94. Widget build(BuildContext context) {
  95. return GestureDetector(
  96. onTap: () {
  97. Get.toNamed('/',parameters: {'u':widget.data['url']});
  98. },
  99. onTapDown: (TapDownDetails details) {
  100. controller.forward();
  101. },
  102. onTapUp: (TapUpDetails details) {
  103. controller.reverse();
  104. },
  105. onTapCancel: () {
  106. controller.reverse();
  107. },
  108. child: Transform.scale(
  109. scale: 1 - 0.05 * animation.value,
  110. child: Container(
  111. width: 343.px,
  112. margin: EdgeInsets.only(top: 8.px),
  113. padding: EdgeInsets.fromLTRB(20.px, 18.px, 12.px, 18.px),
  114. decoration: BoxDecoration(
  115. color: const Color(0xFF262626),
  116. borderRadius: BorderRadius.circular(24.px)),
  117. child: Row(
  118. children: [
  119. ClipRRect(
  120. borderRadius: BorderRadius.circular(24.px),
  121. child: Image.network(
  122. widget.data['avatar'],
  123. width: 48.px,
  124. height: 48.px,
  125. fit: BoxFit.cover,
  126. ),
  127. ),
  128. SizedBox(width: 12.px),
  129. Expanded(
  130. child: Column(
  131. crossAxisAlignment: CrossAxisAlignment.start,
  132. children: [
  133. Text(
  134. widget.data['nickname'],
  135. style: TextStyle(
  136. color: Colors.white,
  137. fontSize: 16.px,
  138. fontWeight: FontWeight.bold),
  139. ),
  140. SizedBox(
  141. height: 4.px,
  142. ),
  143. Text(
  144. formatTime(DateTime.fromMillisecondsSinceEpoch(widget.data['time'])),
  145. style: TextStyle(
  146. color: const Color(0xFF74747A), fontSize: 12.px),
  147. ),
  148. ],
  149. )),
  150. Image.asset(
  151. 'assets/images/arrow_right.png',
  152. width: 20.px,
  153. height: 20.px,
  154. )
  155. ],
  156. ),
  157. ),
  158. ));
  159. }
  160. }