YSPingView.dart 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'package:dart_ping/dart_ping.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import '../wallet/YSWalletRocChoose.dart';
  5. import 'YSTools.dart';
  6. class YSPingView extends StatefulWidget {
  7. final bool isEnable;
  8. final String? url;
  9. const YSPingView({Key? key, this.url, this.isEnable = true}) : super(key: key);
  10. @override
  11. YSPingViewState createState() => YSPingViewState();
  12. }
  13. class YSPingViewState extends State<YSPingView> {
  14. int _time = 0;
  15. @override
  16. void initState() {
  17. _getPingData();
  18. super.initState();
  19. }
  20. _getPingData() async{
  21. String url = widget.url??YSData().rpc;
  22. LogUtil.d(url);
  23. PingData data = await Ping(url, count: 1).stream.first;
  24. Duration? d = data.response?.time!;
  25. if(d!=null){
  26. _time = d.inMicroseconds;
  27. }
  28. setState(() {});
  29. LogUtil.d(data);
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. Color color = _time<100?Colors.greenAccent:_time>100&&_time<200?Colors.orangeAccent:Colors.redAccent;
  34. return GestureDetector(
  35. onTap: (){
  36. if(widget.isEnable==false)return;
  37. Navigator.of(context).push(
  38. CupertinoPageRoute(builder: (context){
  39. return const YSWalletRocChoose();
  40. })
  41. );
  42. },
  43. behavior: HitTestBehavior.opaque,
  44. child: Row(
  45. children: [
  46. Container(
  47. margin: EdgeInsets.only(left: hsp(5)),
  48. height: hsp(20),
  49. width: hsp(20),
  50. alignment: Alignment.center,
  51. child: Container(
  52. height: hsp(5),
  53. width: hsp(5),
  54. decoration: BoxDecoration(
  55. color: color,
  56. borderRadius: const BorderRadius.all(Radius.circular(50))
  57. ),
  58. ),
  59. ),
  60. Text('$_time ms',style: TextStyle(fontSize: zsp(8),color: color),)
  61. ],
  62. ),
  63. );
  64. }
  65. }