YSWalletListItemView.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_slidable/flutter_slidable.dart';
  6. import 'package:flutter_wallet/tools/YSAlertView.dart';
  7. import 'package:flutter_wallet/tools/YSNetWork.dart';
  8. import 'package:flutter_wallet/tools/YSSqflite.dart';
  9. import 'package:flutter_wallet/wallet/YSWalletListItemDetail.dart';
  10. import 'package:provider/provider.dart';
  11. import '../../login/view/YSHelpView.dart';
  12. import '../../tools/YSColors.dart';
  13. import '../../tools/YSTools.dart';
  14. class YSWalletListItemView extends StatefulWidget {
  15. final Map item;
  16. const YSWalletListItemView({Key? key, required this.item}) : super(key: key);
  17. @override
  18. YSWalletListItemViewState createState() => YSWalletListItemViewState();
  19. }
  20. class YSWalletListItemViewState extends State<YSWalletListItemView> {
  21. double _amount = 0;
  22. Map _token = {};
  23. int _number = 0;
  24. @override
  25. void initState() {
  26. _amount = YSData().balanceMap['${widget.item['address']}${YSData().wallet['id']}token']??0;
  27. networkDelay((){
  28. _getToken();
  29. });
  30. super.initState();
  31. }
  32. _getToken() async{
  33. Map type = getWalletType(widget.item);
  34. List list = [];
  35. //ETH===========TRON-HD
  36. // LogUtil.d('_getToken=======${YSData().allTokenArray}');
  37. if(type.isNotEmpty&&widget.item['address']!='THb4CqiFdwNHsWsQCs4JhzwjMWys4aqCbF'){
  38. widget.item['name'] = type['token_name'];
  39. widget.item['address'] = YSData().wallet['public'];
  40. _token = widget.item;
  41. List list = YSData().priceArray.where((element) => element['symbol']==_token['name']).toList();
  42. if(list.isNotEmpty){
  43. _token = widget.item;
  44. _token['price_usd'] = double.parse(list.first['price']);
  45. }
  46. // String address = '';
  47. // if(type['token_name']=='BNB'){
  48. // address = '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c';
  49. // }else if(type['token_name']=='ETH'){
  50. // address = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
  51. // }
  52. // list = YSData().allTokenArray.where((element) => element['address']==address).toList();
  53. // if(list.isNotEmpty){
  54. // _token = widget.item;
  55. // _token['price_usd'] = list.first['price_usd'];
  56. // }else{
  57. // _token = widget.item;
  58. // List list = YSData().priceArray.where((element) => element['symbol']==_token['name']).toList();
  59. // if(list.isNotEmpty){
  60. // _token['price_usd'] = double.parse(list.first['price']);
  61. // }
  62. // }
  63. }else{
  64. list = YSData().allTokenArray.where((element) => element['address']==widget.item['address']).toList();
  65. if(list.isNotEmpty){
  66. _token = list.first;
  67. }else{
  68. _token = widget.item;
  69. }
  70. }
  71. _token['ysType'] = YSData().typeId;
  72. setState(() {});
  73. }
  74. @override
  75. void didUpdateWidget(covariant YSWalletListItemView oldWidget) {
  76. _number++;
  77. if(_number%50==0){
  78. _getToken();
  79. }
  80. super.didUpdateWidget(oldWidget);
  81. }
  82. @override
  83. Widget build(BuildContext context) {
  84. // LogUtil.d('${_token['name']}===========${_token['address']}');
  85. return Slidable(
  86. key: const ValueKey(0),
  87. endActionPane: ActionPane(
  88. extentRatio: 0.3,
  89. motion: GestureDetector(
  90. onTap: (){
  91. ysShowCenterAlertView(context, YSTipsAlertView(valueSetter: (value) async{
  92. if(value){
  93. YSSqflite2 sqflite2 = YSSqflite2().init();
  94. sqflite2.rawDelete('${widget.item['hid']}');
  95. List array = await sqflite2.rawQuery();
  96. if(!mounted)return;
  97. YSSqflite2.notifier(YSData().context, array);
  98. }
  99. },tipsStr: '是否删除次资产?',));
  100. },
  101. child: Container(
  102. color: Colors.red,
  103. alignment: Alignment.center,
  104. child: Text('删除',style: YSColors.content2Style(context),),
  105. ),
  106. ),
  107. children: const [],
  108. ),
  109. child: Consumer(
  110. builder: (context,YSUnitNotifier unit,child) {
  111. return GestureDetector(
  112. onTap: (){
  113. YSData().contAddress = _token['address'];
  114. YSData().contName = _token['name'];
  115. Navigator.of(context).push(
  116. CupertinoPageRoute(builder: (context){
  117. return YSWalletListItemDetail(item: widget.item,);
  118. })
  119. );
  120. },
  121. behavior: HitTestBehavior.opaque,
  122. child: _token.isEmpty?Container():Container(
  123. padding: EdgeInsets.only(left: hsp(15),right: hsp(15),top: hsp(10),bottom: hsp(10)),
  124. alignment: Alignment.centerLeft,
  125. child: Row(
  126. children: [
  127. ClipRRect(
  128. borderRadius: const BorderRadius.all(Radius.circular(50)),
  129. key: Key('$_number'),
  130. child: YSImage.network(_token['icon_url'],height: hsp(40),width: hsp(40),),
  131. ),
  132. Expanded(
  133. child: Container(
  134. padding: EdgeInsets.only(top: hsp(5),bottom: hsp(5),left: hsp(5),right: hsp(5)),
  135. child: Column(
  136. crossAxisAlignment: CrossAxisAlignment.start,
  137. children: [
  138. Text(_token['name']??'',style: YSColors.contentStyle(context),maxLines: 1,),
  139. YSBalanceView(public: _token['address'],style: YSColors.subStyle(context),type: 1,walletAddress: YSData().wallet['public'],valueSetter: (value) {
  140. _amount = double.parse(value)*(_token['price_usd']??1);
  141. setState(() {});
  142. YSData().balanceMap['${widget.item['address']}${YSData().wallet['id']}token'] = _amount;
  143. if(_amount>0){
  144. Provider.of<YSBalanceTotalNotifier>(context, listen: false) .setValue({_token['symbol']:_amount,'ysType':_token['ysType']});
  145. }else{
  146. Provider.of<YSBalanceTotalNotifier>(context, listen: false) .setValue({_token['symbol']:_amount,'ysType':_token['ysType']});
  147. }
  148. },key: Key('$_number'),)
  149. // if(_info.isNotEmpty)Padding(
  150. // padding: EdgeInsets.only(top: hsp(5)),
  151. // child: RichText(text: TextSpan(
  152. // style: YSColors.subStyle(context),
  153. // children: [
  154. // TextSpan(
  155. // text: '\$${_info['lastPrice']} ',
  156. // ),
  157. // TextSpan(
  158. // text: ' ${(double.parse(_info['lastPrice']??'0')-double.parse(_info['openPrice']??'0')).toStringAsFixed(8)}',
  159. // style: const TextStyle(color: Color(0xFFFF0000))
  160. // )
  161. // ]
  162. // ),maxLines: 1,)
  163. // )
  164. ],
  165. ),
  166. )
  167. ),
  168. Container(
  169. width: hsp(150),
  170. padding: EdgeInsets.only(top: hsp(5),bottom: hsp(5),left: hsp(5),right: hsp(5)),
  171. child: Column(
  172. crossAxisAlignment: CrossAxisAlignment.end,
  173. children: [
  174. Text('${YSUserRecord().unit=='RMB'?'¥':'\$'} ${(double.parse(toFlex(_amount,range: 2))*unitNumber()).toStringAsFixed(4)}'.fixAutoLines(),style: YSColors.contentStyle(context),),//${_info['count']}
  175. // Padding(
  176. // padding: EdgeInsets.only(top: hsp(5)),
  177. // child: Text('\$ ${widget.item['gas']} ',style: YSColors.subStyle(context),)
  178. // )
  179. ],
  180. ),
  181. ),
  182. ],
  183. )
  184. ),
  185. );
  186. }
  187. ),
  188. );
  189. }
  190. }
  191. class YSWalletListChooseItemView extends StatefulWidget {
  192. final ValueSetter? valueSetter;
  193. final Map item;
  194. const YSWalletListChooseItemView({Key? key, required this.item, this.valueSetter}) : super(key: key);
  195. @override
  196. YSWalletListChooseItemViewState createState() => YSWalletListChooseItemViewState();
  197. }
  198. class YSWalletListChooseItemViewState extends State<YSWalletListChooseItemView> {
  199. @override
  200. Widget build(BuildContext context) {
  201. bool isChoose = YSData().tokenArray.any((element) => element['hid']=='${widget.item['hid']}')&&widget.valueSetter==null;
  202. return GestureDetector(
  203. onTap: (){
  204. },
  205. behavior: HitTestBehavior.opaque,
  206. child: Container(
  207. padding: EdgeInsets.only(left: hsp(15),right: hsp(15),top: hsp(10),bottom: hsp(10)),
  208. alignment: Alignment.centerLeft,
  209. child: Row(
  210. children: [
  211. ClipRRect(
  212. borderRadius: const BorderRadius.all(Radius.circular(50)),
  213. child: YSImage.network(widget.item['icon_url'],height: hsp(40),width: hsp(40),),
  214. ),
  215. Expanded(
  216. child: Container(
  217. padding: EdgeInsets.only(top: hsp(5),bottom: hsp(5),left: hsp(5),right: hsp(5)),
  218. child: Column(
  219. crossAxisAlignment: CrossAxisAlignment.start,
  220. children: [
  221. Text(widget.item['name']??'',style: YSColors.contentStyle(context),maxLines: 1,),
  222. Padding(
  223. padding: EdgeInsets.only(top: hsp(5)),
  224. child: Text(ysTextCut(widget.item['address']),style: YSColors.subStyle(context),)
  225. )
  226. ],
  227. ),
  228. )
  229. ),
  230. GestureDetector(
  231. onTap: () async{
  232. if(widget.valueSetter!=null){
  233. widget.valueSetter!(widget.item);
  234. }else{
  235. YSSqflite2 sq2 = YSSqflite2().init();
  236. if(isChoose){
  237. sq2.rawDelete('${widget.item['hid']}');
  238. }else{
  239. sq2.rawInsert(
  240. hid: '${widget.item['hid']}',
  241. name: '${widget.item['name']}',
  242. gas: '${widget.item['gas']}',
  243. decimal: '${widget.item['decimal']}',
  244. rice_usd: '${widget.item['rice_usd']}',
  245. percent_change_24h: '${widget.item['percent_change_24h']}',
  246. address: '${widget.item['address']}',
  247. total_supply: '${widget.item['total_supply']}',
  248. icon_url: '${widget.item['icon_url']}',
  249. symbol: '${widget.item['symbol']}'
  250. );
  251. }
  252. List tokenArray = await sq2.rawQuery();
  253. if(!mounted)return;
  254. YSSqflite2.notifier(context, tokenArray);
  255. setState(() {});
  256. }
  257. },
  258. child: Icon(isChoose?Icons.remove_circle_outline:Icons.add_circle_outline,size: hsp(20),color: isChoose?Colors.grey:Colors.blue,),
  259. )
  260. ],
  261. )
  262. ),
  263. );
  264. }
  265. }