import 'dart:async';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/tools/YSAlertView.dart';
import 'package:flutter_wallet/tools/YSColors.dart';
import 'package:flutter_wallet/tools/YSSqflite.dart';
import 'package:flutter_wallet/tools/YSTools.dart';
import 'package:flutter_wallet/wallet/YSMessage.dart';
import 'package:flutter_wallet/wallet/YSWalletCollection.dart';
import 'package:flutter_wallet/wallet/YSWalletManager.dart';
import 'package:flutter_wallet/wallet/YSWalletTokenChoose.dart';
import 'package:flutter_wallet/wallet/YSWalletTransfer.dart';
import 'package:flutter_wallet/wallet/view/YSWalletListItemView.dart';
import 'package:provider/provider.dart';
import 'package:skeletons/skeletons.dart';
import '../base/YSTabBar.dart';
import '../generated/l10n.dart';
import '../login/YSWalletSetting.dart';
import '../login/view/YSHelpView.dart';
import '../tools/YSBip.dart';
import '../tools/YSNetWork.dart';
import 'YSWalletDetail.dart';

class YSWallet extends StatefulWidget {
  const YSWallet({Key? key}) : super(key: key);

  @override
  YSWalletState createState() => YSWalletState();
}

class YSWalletState extends State<YSWallet> with SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin{
  final List _titles = [
    {'title':'资产','type':0},
    {'title':'藏品','type':1},
  ];
  bool _isShow = true;
  late TabController _tabController;
  // int _refeshIndex = 0;
  Map _wallet = {};
  Timer? _timer;
  @override
  void initState() {
    _tabController = TabController(
      vsync: this,
      length: _titles.length,
    );
    _getWalletData();
    _startTime();
    checkAddress();
    super.initState();
  }


  _startTime() {
    // _timer = Timer.periodic(const Duration(seconds: 1), (timer) async{
    //   _refreshData();
    // });
  }
  _getPriceData() {
    YSNetWork.ysRequestHttp(context, type: RequestType.get, api: 'tokens', parameter: {'chain_id':YSData().typeId,'page':1,'count':100}, successSetter: (dict) async{
      YSData().allTokenArray = dict['data']['list']??[];
      addToken(context);
      getMindToken(context);
    });
  }

  _getSymbolData() {
    List array = YSData().typeArray.where((element) => element['id']==YSData().typeId).toList();
    if(array.isNotEmpty){
      Map map = array.first;
      YSNetWork.ysRequestHttp(context, type: RequestType.get, api: 'prices', parameter: {'symbol':map['token_name'],'page':1,'count':5}, successSetter: (dict) async{
        YSData().priceArray = dict['data']['list']??[];
      });
    }
  }

  _refreshData() async{
    List array = YSData().typeArray.where((element) => element['id']==YSData().typeId).toList();
    if(array.isNotEmpty){
      Map map = array.first;
      YSNetWork.ysRequestHttp(context, type: RequestType.get, api: 'prices', parameter: {'symbol':map['token_name'],'page':1,'count':5}, successSetter: (dict) async{
        YSData().priceArray = dict['data']['list']??[];
        YSNetWork.ysRequestHttp(context, type: RequestType.get, api: 'tokens', parameter: {'chain_id':YSData().typeId,'page':1,'count':100}, successSetter: (dict) async{
          YSData().allTokenArray = dict['data']['list']??[];
          _getTokenArray();
        });
      });
    }
  }


  _getWalletData() async{
    YSSqflite sqflite = YSSqflite().init();
    List walletArray = await sqflite.rawQuery();
    if(!mounted)return;
    YSSqflite.notifier(context, walletArray);
    if(walletArray.isNotEmpty){
      YSData().wallet = walletArray.first;
      _getPriceData();
    }
  }

  @override
  void dispose() {
    if(_timer!=null){
      _timer?.cancel();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    List _menuArray = [
      {'title':S.current.ZHUANZHANG,'type':1},
      {'title':S.current.SHOUKUAN,'type':2},
      {'title':S.current.SHANDUI,'type':3}
    ];
    YSData().context = context;
    return Scaffold(
      body: SingleChildScrollView(
        child: Consumer2(
          builder: (context,YSWalletNotifier value,YSTokenNotifier value2,child) {
            _getSymbolData();
            if(value.walletArray.isNotEmpty){
              _wallet = value.walletArray.firstWhere((element) => element['chooseType']==1,orElse: ()=>{});
              if(_wallet.isEmpty)_wallet = value.walletArray.first;
            }else{
              _wallet.clear();
            }
            YSData().wallet = _wallet;
            YSData().tokenArray = value2.value;
            // _getTokenArray();
            return Skeleton(
              isLoading: YSData().isLoad==false,
              skeleton: Stack(
                children: [
                  Column(
                    children: [
                      SkeletonAvatar(
                        style: SkeletonAvatarStyle(
                          width: double.infinity,
                          height: hsp(250)
                        ),
                      ),
                      SizedBox(
                        height: hsp(50),
                        child: Row(
                          children: [
                            Expanded(child: Center(
                              child: SkeletonLine(
                                style: SkeletonLineStyle(
                                  height: 15,
                                  width: 50,
                                  borderRadius: BorderRadius.circular(8),
                                  alignment: AlignmentDirectional.center
                                ),
                              ),
                            )),
                            Expanded(child: Center(
                              child: SkeletonLine(
                                style: SkeletonLineStyle(
                                  height: 15,
                                  width: 50,
                                  borderRadius: BorderRadius.circular(8),
                                  alignment: AlignmentDirectional.center
                                ),
                              ),
                            )),
                          ],
                        ),
                      ),
                      Container(
                        width: ysWidth(context),
                        padding: EdgeInsets.only(left: hsp(15)),
                        child: ListView.separated(
                          itemBuilder: (context,index){
                            return Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                const SkeletonAvatar(
                                  style: SkeletonAvatarStyle(
                                      shape: BoxShape.circle, width: 50, height: 50),
                                ),
                                const SizedBox(width: 8),
                                Expanded(
                                  child: SkeletonParagraph(
                                    style: SkeletonParagraphStyle(
                                        lines: 2,
                                        spacing: 10,
                                        lineStyle: SkeletonLineStyle(
                                          randomLength: true,
                                          height: 10,
                                          borderRadius: BorderRadius.circular(8),
                                          minLength: MediaQuery.of(context).size.width / 6,
                                          maxLength: MediaQuery.of(context).size.width / 3,
                                        )
                                    ),
                                  ),
                                ),
                                Expanded(
                                  child: SkeletonParagraph(
                                    style: SkeletonParagraphStyle(
                                        lines: 1,
                                        lineStyle: SkeletonLineStyle(
                                          alignment: AlignmentDirectional.centerEnd,
                                          randomLength: true,
                                          height: 10,
                                          borderRadius: BorderRadius.circular(8),
                                          minLength: MediaQuery.of(context).size.width / 6,
                                          maxLength: MediaQuery.of(context).size.width / 3,
                                        )
                                    ),
                                  ),
                                )

                              ],
                            );
                          },
                          separatorBuilder: (context,index){
                            return Container(height: hsp(20),);
                          },
                          itemCount: 3,
                          shrinkWrap: true,
                          padding: const EdgeInsets.all(0),
                          physics: const NeverScrollableScrollPhysics(),
                        ),
                      ),
                    ],
                  )
                ],
              ),
              child: LayoutBuilder(
                builder: (context,conSize) {
                  return Stack(
                    children: [
                      const YSHeadView(),
                      Padding(
                        padding: EdgeInsets.only(top: ysTOP(context)+hsp(10)),
                        child: Column(
                          children: [
                            Container(
                              height: hsp(50),
                              padding: EdgeInsets.only(left: hsp(10),right: hsp(10)),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  GestureDetector(
                                    onTap: () async{
                                      // ysShowCenterAlertView(context, YSLoadView2(funAction: () async{
                                      //   return await YSBipManager.setMnemonic('habit benefit gallery egg movie moon crumble topple side found aim cement');
                                      // },valueSetter: (isDone) async{
                                      //   if(isDone&&mounted){
                                      //     if(YSData().isManager){
                                      //       List array = await YSSqflite().rawQuery();
                                      //       if(!mounted)return;
                                      //       YSSqflite.notifier(context, array);
                                      //       Navigator.popUntil(context, ModalRoute.withName("YSWalletManager"));
                                      //     }else{
                                      //       Navigator.of(context).pushAndRemoveUntil(CupertinoPageRoute(builder: (context){
                                      //         return const YSTabBar();
                                      //       }), (route) => false);
                                      //     }
                                      //   }
                                      // },));
                                      // return;
                                      // YSFileWriteRead().readContent();
                                      // YSFileWriteRead().writeContent('content');
                                      // return;
                                      Navigator.of(context).push(
                                        CupertinoPageRoute(builder: (context){
                                          return const YSMessage();
                                        })
                                      );
                                      // Navigator.of(context).push(
                                      //     CupertinoPageRoute(builder: (context){
                                      //       return const YSWalletSetting();
                                      //     })
                                      // );
                                    },
                                    child: Stack(
                                      children: [
                                        Image.asset(YSColors.imageStyle(context, '编组 4'),height: hsp(30),width: hsp(20),),
                                        // Container(
                                        //   height: hsp(5),
                                        //   width: hsp(5),
                                        //   decoration: const BoxDecoration(
                                        //     color: Colors.red,
                                        //     borderRadius: BorderRadius.all(Radius.circular(50))
                                        //   ),
                                        //   margin: EdgeInsets.only(left: hsp(15),top: hsp(5)),
                                        // )
                                      ],
                                    )
                                  ),
                                  const YSChangeNetView(),
                                  const YSScanView(size: 20,)
                                ],
                              ),
                            ),
                            if(_wallet.isNotEmpty)Padding(
                              padding: EdgeInsets.only(top: hsp(20),bottom: hsp(10),left: hsp(15),right: hsp(15)),
                              child: ClipRRect(
                                borderRadius: const BorderRadius.all(Radius.circular(10)),
                                child: GestureDetector(
                                  onTap: (){
                                    if(_wallet.isEmpty)return;
                                    Navigator.of(context).push(
                                        CupertinoPageRoute(builder: (context){
                                          return YSWalletDetail(wallet: _wallet,);
                                        },)
                                    );
                                  },
                                  behavior: HitTestBehavior.opaque,
                                  child: SizedBox(
                                    height: hsp(160),
                                    width: ysWidth(context),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        GestureDetector(
                                          onTap: (){
                                            Navigator.of(context).push(
                                                CupertinoPageRoute(builder: (context){
                                                  return const YSWalletManager();
                                                },settings: const RouteSettings(name: 'YSWalletManager'),)
                                            ).then((value) {
                                              // _refeshIndex++;
                                              // _getTokenArray();
                                              // getMindToken(context);
                                            });
                                          },
                                          behavior: HitTestBehavior.opaque,
                                          child: SizedBox(
                                            height: hsp(20),
                                            child: RichText(text: TextSpan(
                                                style: YSColors.subStyle(context),
                                                children: [
                                                  TextSpan(text: '${_wallet['name']??''}  '),
                                                  WidgetSpan(child: Image.asset(YSColors.imageStyle(context, '路径'),height: hsp(5),width: hsp(5),),alignment: PlaceholderAlignment.middle),
                                                  WidgetSpan(child: YSTagView(wallet: YSData().wallet,key: Key(YSData().wallet['public']),))
                                                ]
                                            )),
                                          ),
                                        ),
                                        SizedBox(
                                            height: hsp(40),
                                            child: Row(
                                              mainAxisSize: MainAxisSize.min,
                                              children: [
                                                // Image.asset(YSColors.imageStyle(context, '编组 11'),height: hsp(25),width: hsp(25),),
                                                Text(YSUserRecord().unit=='RMB'?'¥':'\$',style: TextStyle(fontSize: zsp(25),color: Colors.white),),
                                                if(_wallet.isNotEmpty)Padding(
                                                  padding: EdgeInsets.only(left: hsp(5),right: hsp(10)),
                                                  child: Container(
                                                    constraints: BoxConstraints(
                                                      maxWidth: ysWidth(context)/2
                                                    ),
                                                    child: _isShow==false?Container(
                                                      padding: EdgeInsets.only(top: hsp(10)),
                                                      child: Text(
                                                        '*****',
                                                        style: TextStyle(fontSize: zsp(25),color: (YSColors.content2Style(context)).color),
                                                      ),
                                                    ):YSBalanceTotalData(style: TextStyle(fontSize: zsp(25),color: (YSColors.content2Style(context)).color),
                                                      key: Key('${YSData().typeId}${_wallet['id']}${YSData().chooseIndex}'),),
                                                  )
                                                ),
                                                GestureDetector(
                                                  onTap: (){
                                                    _isShow = !_isShow;
                                                    setState(() {});
                                                  },
                                                  behavior: HitTestBehavior.opaque,
                                                  child: Container(
                                                    height: hsp(20),
                                                    alignment: Alignment.topCenter,
                                                    child: Image.asset(YSColors.imageStyle(context, _isShow?'眼睛备份':'yanjing_yincang'),height: hsp(15),width: hsp(15),color: (YSColors.content2Style(context)).color,),
                                                  )
                                                ),
                                              ],
                                            )
                                        ),
                                        SizedBox(
                                          height: hsp(100),
                                          width: ysWidth(context),
                                          child: false?Row(//'${_wallet['private']}'.isEmpty
                                            children: [
                                              GestureDetector(
                                                onTap: (){
                                                  Navigator.of(context).push(
                                                      CupertinoPageRoute(builder: (context){
                                                        return YSWalletCollection(wallet: _wallet,);
                                                      })
                                                  );
                                                },
                                                child: Container(
                                                  // width: (conSize.maxWidth-hsp(30))/_menuArray.length,
                                                  width: hsp(100),
                                                  alignment: Alignment.center,
                                                  child: Column(
                                                    mainAxisSize: MainAxisSize.min,
                                                    children: [
                                                      Image.asset(YSColors.imageStyle(context, '收款_1'),height: hsp(50),width: hsp(50),),
                                                      Container(
                                                          constraints: BoxConstraints(maxWidth: (conSize.maxWidth/_menuArray.length)-hsp(30)),
                                                          child: Text(' ${S.current.SHOUKUAN}',style: YSColors.content3Style(context),maxLines: 1,)
                                                      )
                                                    ],
                                                  ),
                                                ),
                                              ),
                                            ],
                                          ):ListView.builder(
                                            itemBuilder: (context,index){
                                              Map item = _menuArray[index];
                                              return GestureDetector(
                                                onTap: (){
                                                  if(index==2){
                                                    CustomerNotification().dispatch(context);
                                                    return;
                                                  }
                                                  // LogUtil.d(_wallet);
                                                  // LogUtil.d(YSData().tokenArray);
                                                  // return;
                                                  if(index==0){
                                                    YSData().contAddress = _wallet['public'];
                                                    YSData().contName = _wallet['name'];
                                                  }
                                                  Navigator.of(context).push(
                                                      CupertinoPageRoute(builder: (context){
                                                        return index==0?const YSWalletTransfer():index==1?YSWalletCollection(wallet: _wallet,):const YSWalletTransfer();
                                                      })
                                                  );
                                                },
                                                behavior: HitTestBehavior.opaque,
                                                child: Container(
                                                  // width: (conSize.maxWidth-hsp(30))/_menuArray.length,
                                                  width: hsp(100),
                                                  alignment: Alignment.center,
                                                  child: Column(
                                                    mainAxisSize: MainAxisSize.min,
                                                    children: [
                                                      Image.asset(YSColors.imageStyle(context, item['type']==1?'转账 (1)':item['type']==2?'收款_1':'交易_闪兑'),height: hsp(50),width: hsp(50),),
                                                      Container(
                                                          constraints: BoxConstraints(maxWidth: (conSize.maxWidth/_menuArray.length)-hsp(30)),
                                                          child: Text(' ${item['title']}',style: YSColors.content3Style(context),maxLines: 1,)
                                                      )
                                                    ],
                                                  ),
                                                ),
                                              );
                                            },
                                            itemCount: _menuArray.length,
                                            padding: const EdgeInsets.all(0),
                                            physics: const NeverScrollableScrollPhysics(),
                                            scrollDirection: Axis.horizontal,
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            if(_wallet.isNotEmpty)DefaultTabController(
                              length: _titles.length,
                              child: ClipRRect(
                                borderRadius: const BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10)),
                                child: Container(
                                  height: ysHeight(context)-ysTOP(context)-ystabBarHeight-hsp(250),
                                  width: ysWidth(context),
                                  color: YSColors.containColor(context),
                                  child: Column(
                                    children: [
                                      Container(
                                        margin: EdgeInsets.only(left: hsp(15),right: hsp(15)),
                                        height: hsp(50),
                                        child: Row(
                                          children: [
                                            SizedBox(
                                              width: ysWidth(context)-hsp(90),
                                              child: Stack(
                                                children: [
                                                  Container(
                                                    height: hsp(46),
                                                    decoration: BoxDecoration(
                                                      border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1))),
                                                    ),
                                                  ),
                                                  TabBar(
                                                    controller: _tabController,
                                                    indicatorColor: YSColors.selectedColor(context),
                                                    indicatorWeight: hsp(1),
                                                    tabs: _titles.map((f) {//text: '· ${f['title']}'
                                                      return Tab(
                                                        child: Text(f['title']=='资产'?S.current.ZICHAN:f['title'],style: YSColors.contentStyle(context),),
                                                      );
                                                    }).toList(),
                                                  ),
                                                ],
                                              ),
                                            ),
                                            Container(
                                              width: hsp(60),
                                              height: hsp(46),
                                              decoration: BoxDecoration(
                                                border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1))),
                                              ),
                                              alignment: Alignment.center,
                                              child: Row(
                                                mainAxisSize: MainAxisSize.min,
                                                children: [
                                                  Padding(
                                                    padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
                                                    child: Image.asset(YSColors.imageStyle(context, '编组 10'),height: hsp(15),width: hsp(15),),
                                                  ),
                                                  GestureDetector(
                                                    onTap: (){
                                                      Navigator.of(context).push(
                                                          CupertinoPageRoute(builder: (context){
                                                            return const YSWalletTokenChoose();
                                                          })
                                                      );
                                                    },
                                                    child: Image.asset(YSColors.imageStyle(context, '添加'),height: hsp(15),width: hsp(15),),
                                                  )

                                                ],
                                              ),
                                            )
                                          ],
                                        ),
                                      ),
                                      Container(
                                        color: YSColors.containColor(context),
                                        height: ysHeight(context)-ysTOP(context)-ystabBarHeight-hsp(300),
                                        child: TabBarView(
                                          controller: _tabController,
                                          children: _titles.map((f) {
                                            return f['type']==0?RefreshIndicator(
                                              onRefresh: _getTokenArray,
                                              child: ListView.builder(
                                                itemBuilder: (context, index) {
                                                  Map item = YSData().tokenArray [index];
                                                  return YSData().allTokenArray.isEmpty?Container():YSWalletListItemView(
                                                    item: item,
                                                    key: Key('${YSData().typeId}${YSData().wallet['id']}${item['name']}${YSData().dataRefresh}'),
                                                  );
                                                },
                                                itemCount: YSData().tokenArray.length,
                                                padding: const EdgeInsets.all(0),
                                              ),
                                            ):Container();
                                          }).toList(),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                      if(_wallet.isEmpty)Container(
                        margin: EdgeInsets.only(top: ysTOP(context)+hsp(60)),
                        height: ysHeight(context)-ysTOP(context)-hsp(130),
                        width: ysWidth(context),
                        child: GestureDetector(
                          onTap: (){},
                          behavior: HitTestBehavior.opaque,
                          child: ClipRRect(
                            child: BackdropFilter(
                              filter: ImageFilter.blur(sigmaX: 5.0,sigmaY: 5.0),
                              child: Padding(
                                padding: EdgeInsets.only(top: ysHeight(context)-ysTOP(context)-hsp(430)),
                                child: const YSChooseWalletType(isHome: true,),
                              ),
                            ),
                          ),
                        ),
                      )
                    ],
                  );
                }
              ),
            );
          }
        ),
      ),
    );
  }

  Future<void> _getTokenArray() async{
    // YSSqflite2 sqflite2 = YSSqflite2().init();
    // List array = await sqflite2.rawQuery();
    // if(!mounted)return;
    YSData().dataRefresh++;
    // YSSqflite2.notifier(context, array);
    addToken(context);

  }

  @override
  bool get wantKeepAlive => false;
}


class YSWalletOld extends StatefulWidget {
  const YSWalletOld({Key? key}) : super(key: key);

  @override
  YSWalletOldState createState() => YSWalletOldState();
}

class YSWalletOldState extends State<YSWalletOld> with SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin{
  final List _titles = [
    {'title':'资产','type':0},
    {'title':'DeFi','type':1},
    {'title':'NFT','type':2},
  ];
  bool _isShow = true;
  late TabController _tabController;
  int _refeshIndex = 0;
  Map _wallet = {};
  Timer? _timer;
  @override
  void initState() {
    _tabController = TabController(
      vsync: this,
      length: _titles.length,
    );
    _getWalletData();
    _startTime();
    super.initState();
  }
  _startTime() {
    _timer = Timer.periodic(const Duration(seconds: 3), (timer){
      _getTokenArray();
    });
  }
  _getPriceData() {
    YSNetWork.ysRequestHttp(context, type: RequestType.get, api: 'tokens', parameter: {'chain_id':YSData().typeId,'page':1,'count':100}, successSetter: (dict) async{
      YSData().allTokenArray = dict['data']['list']??[];
      addToken(context);
      getMindToken(context);
    });

  }


  _getWalletData() async{
    YSSqflite sqflite = YSSqflite().init();
    List walletArray = await sqflite.rawQuery();
    if(!mounted)return;
    YSSqflite.notifier(context, walletArray);
    if(walletArray.isNotEmpty){
      YSData().wallet = walletArray.first;
      _getPriceData();
    }
  }

  @override
  void dispose() {
    if(_timer!=null){
      _timer?.cancel();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    List _menuArray = [
      {'title':S.current.ZHUANZHANG,'type':1},
      {'title':S.current.SHOUKUAN,'type':2},
      // {'title':S.current.SHANDUI,'type':3}
    ];
    return Scaffold(
      body: SingleChildScrollView(
        child: Consumer2(
            builder: (context,YSWalletNotifier value,YSTokenNotifier value2,child) {
              if(value.walletArray.isNotEmpty){
                _wallet = value.walletArray.firstWhere((element) => element['chooseType']==1,orElse: ()=>{});
                if(_wallet.isEmpty)_wallet = value.walletArray.first;
              }else{
                _wallet.clear();
              }
              YSData().wallet = _wallet;
              YSData().tokenArray = value2.value;
              // _refeshIndex++;
              return YSData().isLoad==false?Container():LayoutBuilder(
                  builder: (context,conSize) {
                    return Stack(
                      children: [
                        const YSHeadView(),
                        Padding(
                          padding: EdgeInsets.only(top: ysTOP(context)+hsp(10),left: hsp(15),right: hsp(15)),
                          child: Column(
                            children: [
                              SizedBox(
                                height: hsp(50),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    GestureDetector(
                                      onTap: () async{
                                        // YSSqflite2 sqflite2 = YSSqflite2().init();
                                        // int count = await sqflite2.delete();
                                        // LogUtil.d(count);
                                      },
                                      child: Image.asset(YSColors.imageStyle(context, '编组 4'),height: hsp(30),width: hsp(20),),
                                    ),
                                    const YSChangeNetView(),
                                    const YSScanView(size: 20,)
                                  ],
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: hsp(20),bottom: hsp(10)),
                                child: ClipRRect(
                                  borderRadius: const BorderRadius.all(Radius.circular(10)),
                                  child: Container(
                                    height: hsp(160),
                                    color: YSColors.containColor(context),
                                    child: Column(
                                      children: [
                                        GestureDetector(
                                          onTap: (){
                                            if(_wallet.isEmpty)return;
                                            Navigator.of(context).push(
                                                CupertinoPageRoute(builder: (context){
                                                  return YSWalletDetail(wallet: _wallet,);
                                                },)
                                            );
                                          },
                                          behavior: HitTestBehavior.opaque,
                                          child: Container(
                                            width: ysWidth(context)-hsp(30),
                                            height: hsp(110),
                                            padding: EdgeInsets.all(hsp(15)),
                                            decoration: BoxDecoration(
                                              // image: DecorationImage(image: AssetImage(YSColors.imageStyle(context, '编组备份')),fit: BoxFit.fill),
                                                borderRadius: const BorderRadius.all(Radius.circular(10)),
                                                color: YSColors.iconColor(context)
                                            ),
                                            child: Stack(
                                              children: [
                                                Positioned(
                                                    top: 0,
                                                    right: 0,
                                                    child: GestureDetector(
                                                      onTap: (){
                                                        _isShow = !_isShow;
                                                        setState(() {});
                                                      },
                                                      child: Image.asset(YSColors.imageStyle(context, _isShow?'眼睛备份':'yanjing_yincang'),height: hsp(20),width: hsp(20),color: YSColors.containColor(context),),
                                                    )
                                                ),
                                                Positioned(
                                                    top: hsp(15),
                                                    right: hsp(30),
                                                    left: hsp(30),
                                                    child: SizedBox(
                                                      height: hsp(80),
                                                      child: Column(
                                                        children: [
                                                          SizedBox(
                                                              height: hsp(40),
                                                              child: Row(
                                                                mainAxisSize: MainAxisSize.min,
                                                                children: [
                                                                  // Image.asset(YSColors.imageStyle(context, '编组 11'),height: hsp(25),width: hsp(25),),
                                                                  Text('\$',style: TextStyle(fontSize: zsp(30),color: Colors.white),),
                                                                  if(_wallet.isNotEmpty)Padding(
                                                                      padding: EdgeInsets.only(left: hsp(5)),
                                                                      child: Container(
                                                                        constraints: BoxConstraints(
                                                                            maxWidth: ysWidth(context)/2
                                                                        ),
                                                                        child: _isShow==false?Container(
                                                                          padding: EdgeInsets.only(top: hsp(10)),
                                                                          child: Text(
                                                                            '*****',
                                                                            style: TextStyle(fontSize: zsp(25),color: YSColors.containColor(context)),
                                                                          ),
                                                                        ):YSBalanceTotalData(style: YSColors.title2Style(context),
                                                                          key: Key('${YSData().typeId}${_wallet['id']}7'),),
                                                                      )
                                                                  )
                                                                ],
                                                              )
                                                          ),
                                                          GestureDetector(
                                                            onTap: (){
                                                              Navigator.of(context).push(
                                                                  CupertinoPageRoute(builder: (context){
                                                                    return const YSWalletManager();
                                                                  },settings: const RouteSettings(name: 'YSWalletManager'),)
                                                              ).then((value) {
                                                                _refeshIndex++;
                                                                _getTokenArray();
                                                                getMindToken(context);
                                                              });
                                                            },
                                                            behavior: HitTestBehavior.opaque,
                                                            child: SizedBox(
                                                              height: hsp(40),
                                                              child: RichText(text: TextSpan(
                                                                  style: YSColors.sub2Style(context),
                                                                  children: [
                                                                    TextSpan(text: '${_wallet['name']??''}  '),
                                                                    WidgetSpan(child: Image.asset(YSColors.imageStyle(context, '路径'),height: hsp(5),width: hsp(5),),alignment: PlaceholderAlignment.middle),
                                                                  ]
                                                              )),
                                                            ),
                                                          )
                                                        ],
                                                      ),
                                                    )
                                                )
                                              ],
                                            ),
                                          ),
                                        ),
                                        SizedBox(
                                          height: hsp(50),
                                          child: ListView.builder(
                                            itemBuilder: (context,index){
                                              Map item = _menuArray[index];
                                              return GestureDetector(
                                                onTap: (){
                                                  if(index==2)return;
                                                  if(index==0){
                                                    YSData().contAddress = _wallet['public'];
                                                    YSData().contName = _wallet['name'];
                                                  }
                                                  Navigator.of(context).push(
                                                      CupertinoPageRoute(builder: (context){
                                                        return index==0?const YSWalletTransfer():index==1?YSWalletCollection(wallet: _wallet,):const YSWalletTransfer();
                                                      })
                                                  );
                                                },
                                                behavior: HitTestBehavior.opaque,
                                                child: Container(
                                                  width: (conSize.maxWidth-hsp(30))/_menuArray.length,
                                                  alignment: Alignment.center,
                                                  child: Row(
                                                    mainAxisSize: MainAxisSize.min,
                                                    children: [
                                                      Image.asset(YSColors.imageStyle(context, item['type']==1?'转账 (1)':item['type']==2?'收款_1':'交易_闪兑'),height: hsp(30),width: hsp(30),),
                                                      Container(
                                                          constraints: BoxConstraints(maxWidth: (conSize.maxWidth/_menuArray.length)-hsp(30)),
                                                          child: Text(' ${item['title']}',style: YSColors.contentStyle(context),maxLines: 1,)
                                                      )
                                                    ],
                                                  ),
                                                ),
                                              );
                                            },
                                            itemCount: _menuArray.length,
                                            padding: const EdgeInsets.all(0),
                                            physics: const NeverScrollableScrollPhysics(),
                                            scrollDirection: Axis.horizontal,
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                              if(_wallet.isNotEmpty)DefaultTabController(
                                length: _titles.length,
                                child: Container(
                                  height: ysHeight(context)-ysTOP(context)-ystabBarHeight-hsp(270),
                                  width: ysWidth(context),
                                  color: YSColors.containColor(context),
                                  child: Column(
                                    children: [
                                      Container(
                                        margin: EdgeInsets.only(left: hsp(15),right: hsp(15)),
                                        height: hsp(50),
                                        child: Row(
                                          children: [
                                            SizedBox(
                                              width: ysWidth(context)-hsp(120),
                                              child: Stack(
                                                children: [
                                                  Container(
                                                    height: hsp(46),
                                                    decoration: BoxDecoration(
                                                      border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1))),
                                                    ),
                                                  ),
                                                  TabBar(
                                                    controller: _tabController,
                                                    indicatorColor: YSColors.selectedColor(context),
                                                    isScrollable: true,
                                                    indicatorWeight: hsp(1),
                                                    tabs: _titles.map((f) {//text: '· ${f['title']}'
                                                      return Tab(
                                                        child: Text(f['title']=='资产'?S.current.ZICHAN:f['title']=='藏品'?S.current.CANGPIN:f['title'],
                                                          style: YSColors.contentStyle(context),),
                                                      );
                                                    }).toList(),
                                                  ),
                                                ],
                                              ),
                                            ),
                                            Container(
                                              width: hsp(60),
                                              height: hsp(46),
                                              decoration: BoxDecoration(
                                                border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1))),
                                              ),
                                              alignment: Alignment.center,
                                              child: Row(
                                                mainAxisSize: MainAxisSize.min,
                                                children: [
                                                  Padding(
                                                    padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
                                                    child: Image.asset(YSColors.imageStyle(context, '编组 10'),height: hsp(15),width: hsp(15),),
                                                  ),
                                                  GestureDetector(
                                                    onTap: (){
                                                      Navigator.of(context).push(
                                                          CupertinoPageRoute(builder: (context){
                                                            return const YSWalletTokenChoose();
                                                          })
                                                      );
                                                    },
                                                    child: Image.asset(YSColors.imageStyle(context, '添加'),height: hsp(15),width: hsp(15),),
                                                  )

                                                ],
                                              ),
                                            )
                                          ],
                                        ),
                                      ),
                                      Container(
                                        color: YSColors.containColor(context),
                                        height: ysHeight(context)-ysTOP(context)-ystabBarHeight-hsp(330),
                                        child: TabBarView(
                                          controller: _tabController,
                                          children: _titles.map((f) {
                                            return f['type']==0?RefreshIndicator(
                                              onRefresh: _getTokenArray,
                                              child: ListView.builder(
                                                itemBuilder: (context, index) {
                                                  Map item = YSData().tokenArray [index];
                                                  return YSData().allTokenArray.isEmpty?Container():YSWalletListItemView(
                                                    item: item,
                                                    key: Key('${YSData().typeId}${YSData().wallet['id']}${item['name']}$_refeshIndex'),
                                                  );
                                                },
                                                itemCount: YSData().tokenArray.length,
                                                padding: const EdgeInsets.all(0),
                                              ),
                                            ):Container();
                                          }).toList(),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                        if(_wallet.isEmpty)Container(
                          margin: EdgeInsets.only(top: ysTOP(context)+hsp(60)),
                          height: ysHeight(context)-ysTOP(context)-hsp(130),
                          width: ysWidth(context),
                          child: GestureDetector(
                            onTap: (){},
                            behavior: HitTestBehavior.opaque,
                            child: ClipRRect(
                              child: BackdropFilter(
                                filter: ImageFilter.blur(sigmaX: 5.0,sigmaY: 5.0),
                                child: Padding(
                                  padding: EdgeInsets.only(top: ysHeight(context)-ysTOP(context)-hsp(430)),
                                  child: const YSChooseWalletType(isHome: true,),
                                ),
                              ),
                            ),
                          ),
                        )
                      ],
                    );
                  }
              );
            }
        ),
      ),
    );
  }

  Future<void> _getTokenArray() async{
    YSSqflite2 sqflite2 = YSSqflite2().init();
    List array = await sqflite2.rawQuery();
    if(!mounted)return;
    _refeshIndex++;
    YSSqflite2.notifier(context, array);

  }

  @override
  bool get wantKeepAlive => false;
}


