• flutter仿支付宝余额宝年化收益折线图


    在这里插入图片描述

    绘制:
    1.在pubspec.yaml中引入:fl_chart: 0.55.2
    2.绘制:

    import 'package:jade/utils/JadeColors.dart';
    import 'package:util/easy_loading_util.dart';
    import 'package:fl_chart/fl_chart.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_screenutil/flutter_screenutil.dart';
    
    class MyLineChart extends StatefulWidget {
      const MyLineChart({Key key}) : super(key: key);
    
      
      State<MyLineChart> createState() => _MyLineChartState();
    }
    
    class _MyLineChartState extends State<MyLineChart> with TickerProviderStateMixin{
    
     List<String> _tabs = ['近7日','近1月','近3月'];
      TabController _tabController;
    
      List<Color> gradientColors = [
        Colors.yellow,
        Colors.yellow,
      ];
    
      //进入后默认显示指示竖线和轨迹球的点
      FlSpot _defaultShowFlSpot = FlSpot(8, 4);
      //进入页面时默认显示的点上的竖线显隐开关
      bool _showSpotLine = true;
    
    
      List<String> get weekDays => const ['10.21', '10.22', '10.23', '10.24', '10.25', '10.26', '10.27'];
    
     
      void initState() {
        // TODO: implement initState
        super.initState();
        _tabController = TabController(
            length: _tabs.length,vsync: this);
      }
    
      
      void dispose() {
        // TODO: implement dispose
        _tabController.dispose();
        super.dispose();
      }
    
      
      Widget build(BuildContext context) {
        return Container(
          child: Column(
            children: <Widget>[
             _tabBarView(),
              _lineChartWidget()
            ],
          ),
        );
      }
    
    //选项卡
      _tabBarView(){
        return Container(
          width: double.infinity,
          height: 120.w,
          margin: EdgeInsets.only(bottom: 20.w),
          decoration: BoxDecoration(
            color: JadeColors.lightGrey,
            borderRadius: BorderRadius.circular(5)
          ),
          child: Column(
            children: [
             Container(
               height: 70.w,
               child: TabBar(
                 isScrollable: false,
                 labelPadding: EdgeInsets.symmetric(horizontal: 0),
                 indicator: BoxDecoration(
                     color: Colors.white,
                     borderRadius: BorderRadius.circular(5),
                     border: Border.all(width: 2,color: JadeColors.lightGrey)
                 ),
                 labelColor: Color(0xff333333),
                 labelStyle: TextStyle(
                   fontSize: 30.sp,
                   fontWeight: FontWeight.w600,
                 ),
                 unselectedLabelColor: JadeColors.grey,
                 unselectedLabelStyle: TextStyle(
                     fontSize: 30.sp,
                     fontWeight: FontWeight.w300
                 ),
                 indicatorSize: TabBarIndicatorSize.tab,
                 controller: _tabController,
                 tabs: _tabs
                     .map((value) => Container(
                    width: double.infinity,
                    height: double.infinity,
                     padding: EdgeInsets.symmetric(horizontal: 20.w),
                     alignment: Alignment.center,
                     decoration: BoxDecoration(
                       borderRadius: BorderRadius.only(
                           topLeft: value == _tabs.first? Radius.circular(5): Radius.circular(0),
                           bottomLeft: value == _tabs.first? Radius.circular(5): Radius.circular(0),
                           topRight: value == _tabs.last? Radius.circular(5): Radius.circular(0),
                           bottomRight: value == _tabs.last? Radius.circular(5): Radius.circular(0),
                       ),
                       border: Border.all(width: 1.w,color: JadeColors.grey_4)
                     ),
                     child: Text(value))
                 ).toList(),
                 onTap: (index) {
                   esLoadingToast('点击${_tabs[index]}');
                 },
               )
             ),
              Expanded(child: Container(
                margin: EdgeInsets.only(left: 40.w,right: 40.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('2023-10-30',style: TextStyle(color: JadeColors.grey_2,fontSize: 22.sp,fontWeight: FontWeight.bold),),
                    Text.rich(TextSpan(
                        children: [
                          TextSpan(
                              text: '核销数:',
                              style: TextStyle(color: JadeColors.grey_3,fontSize: 22.sp)
                          ),
                          TextSpan(
                              text: '5',
                              style: TextStyle(color: JadeColors.orange,fontSize: 22.sp)
                          )
                        ]
                    ))
                  ],
                ),
              ))
            ],
          ),
        );
      }
    
    //折线图表
      _lineChartWidget(){
        return AspectRatio(
          aspectRatio: 1.70,
          child: Padding(
              padding: EdgeInsets.only(
                right: 40.w,
              ),
              child: LineChart(mainData())
    
          ),
        );
      }
    
      LineChartData mainData() {
        return LineChartData(
          //网格
          gridData: FlGridData(
            show: true,
            drawVerticalLine: false,
            horizontalInterval: 1,
            verticalInterval: 1,
            getDrawingHorizontalLine: (value) {
              return  FlLine(
                color: JadeColors.lightGrey,
                strokeWidth: 1,
              );
            },
            getDrawingVerticalLine: (value) {
              return  FlLine(
                color: Colors.red,
                strokeWidth: 1,
              );
            },
          ),
          titlesData: FlTitlesData(
            show: true,
            rightTitles:  AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
            topTitles:  AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
            bottomTitles: AxisTitles(
              sideTitles: SideTitles(
                showTitles: true,
                reservedSize: 30,
                interval: 2,
                getTitlesWidget: bottomTitleWidgets,
              ),
            ),
            leftTitles: AxisTitles(
              sideTitles: SideTitles(
                showTitles: true,
                interval: 1,
                getTitlesWidget: leftTitleWidgets,
                reservedSize: 30,
              ),
            ),
          ),
          borderData: FlBorderData(
            show: true,
            //  border: Border.symmetric(horizontal: BorderSide(color: JadeColors.lightGrey, width: 1)),
            border: Border.fromBorderSide( BorderSide(color: JadeColors.lightGrey, width: 1)),
          ),
          minX: 0,
          maxX: 12,
          minY: 0,
          maxY: 6,
          lineBarsData: [
            LineChartBarData(
              spots: const [
                FlSpot(0, 0),
                FlSpot(2.6, 4),
                FlSpot(4.9, 5),
                FlSpot(6.8, 3.1),
                FlSpot(8, 4),
                FlSpot(9.5, 3),
                FlSpot(12, 6),
              ],
              isCurved: false,
              gradient: LinearGradient(
                colors: gradientColors,
              ),
              barWidth: 2,
              dotData:  FlDotData(
                show: _showSpotLine,
                getDotPainter: (spot, percent, barData, index) {
                  //当点是默认要显示的点时
                  if(spot == _defaultShowFlSpot){
                    return FlDotCirclePainter(
                      strokeWidth: 1,
                      strokeColor: const Color(0xffF27800),
                      radius: 2,
                      color: Colors.white,
                    );
                  }
                  return FlDotCirclePainter(
                    strokeWidth: 0,
                    strokeColor: Colors.yellow,
                    radius: 1,
                    color: Colors.yellow,
                  );
                }
    
              ),
              belowBarData: BarAreaData(
                show: true,
                gradient: LinearGradient(
                    colors: [Colors.yellow,Colors.white]
                        .map((color) => color.withOpacity(0.3))
                        .toList(),
                    begin: Alignment.topCenter, end: Alignment.bottomCenter
                ),
                  spotsLine: BarAreaSpotsLine(
                    show: _showSpotLine,
                    flLineStyle: FlLine(
                      color: const Color(0xffF27800),
                      strokeWidth: 1,
                    ),
                    checkToShowSpotLine: (spot) {
                      if (spot == _defaultShowFlSpot) {
                        return true;
                      }
                      return false;
                    },
                  ),
              ),
              aboveBarData: BarAreaData(
                show: _showSpotLine,
                gradient: LinearGradient(
                    colors: [Colors.white,Colors.white]
                        .map((color) => color.withOpacity(0.3))
                        .toList(),
                    begin: Alignment.topCenter, end: Alignment.bottomCenter
                ),
                spotsLine: BarAreaSpotsLine(
                  show: true,
                  flLineStyle: FlLine(
                    color: const Color(0xffF27800),
                    strokeWidth: 1,
                  ),
                  checkToShowSpotLine: (spot) {
                    if (spot == _defaultShowFlSpot) {
                      return true;
                    }
                    return false;
                  },
                ),
              ),
            ),
          ],
          lineTouchData: LineTouchData(
            enabled: true,
              getTouchLineEnd: (data, index) => double.infinity,
              getTouchedSpotIndicator: (LineChartBarData barData, List<int> spotIndexes) {
                return spotIndexes.map((spotIndex) {
                  return TouchedSpotIndicatorData(
                    //接触点竖线
                     FlLine(color: const Color(0xffF27800), strokeWidth: 1),
                    FlDotData(
                      //设置接触点 轨迹球画笔
                      getDotPainter: (spot, percent, barData, index) =>
                          FlDotCirclePainter(
                            strokeWidth: 1,
                            strokeColor: const Color(0xffF27800),
                            radius: 2,
                            color: Colors.white,
                          ),
                    ),
                  );
                }).toList();
              },
            touchTooltipData: LineTouchTooltipData(
                tooltipBgColor:Colors.black26
            ),
            touchCallback: (FlTouchEvent event,LineTouchResponse response){
              if(event!= null){
                bool isInteractions = event.isInterestedForInteractions; //手指是否和图表交互(手指和图标交互时隐藏默认显示的点的指示线和轨迹球,手指离开时则隐藏)
                setState(() {
                  _showSpotLine = !isInteractions;
                });
              }
    
              if(response != null){
                TouchLineBarSpot touchLineBarSpot = response?.lineBarSpots?.first;
                int spotIndex = touchLineBarSpot?.spotIndex;
                LineChartBarData lineChartBarData = touchLineBarSpot?.bar;
                if(lineChartBarData != null && spotIndex != null){
                  FlSpot flSpot = lineChartBarData.spots[spotIndex];
    
                  // print('spotIndex= ${touchLineBarSpot?.spotIndex}'); //接触的是折线的第几个圆点
                  // print('barIndex= ${touchLineBarSpot?.barIndex}'); //接触的是第几条折线
                  // print('flSpot.x= ${flSpot?.x}');
                  print('flSpot.y= ${flSpot?.y}');
                  // print('spotIndex= ${touchLineBarSpot?.props}');
                }
              }
            }
          )
        );
      }
    
      Widget bottomTitleWidgets(double value, TitleMeta meta) {
        TextStyle style = TextStyle(
          fontWeight: FontWeight.w600,
          fontSize: 22.sp,
        );
        Widget text;
        switch (value.toInt()) {
          case 0:
            text = Text('10.25', style: style);
            break;
          case 2:
            text = Text(' 10.26 ', style: style);
            break;
          case 4:
            text = Text(' 10.27 ', style: style);
            break;
          case 6:
            text = Text(' 10.28 ', style: style);
            break;
          case 8:
            text = Text(' 10.29 ', style: style);
            break;
          case 10:
            text = Text(' 10.30 ', style: style);
            break;
          case 12:
            text = Text(' 10.31 ', style: style);
            break;
          default:
            text = Text('', style: style);
            break;
        }
    
        return SideTitleWidget(
          axisSide: meta.axisSide,
          child: text,
        );
      }
    
      Widget leftTitleWidgets(double value, TitleMeta meta) {
        TextStyle style = TextStyle(
          fontWeight: FontWeight.w600,
          fontSize: 22.sp,
        );
        String text;
        switch (value.toInt()) {
          case 0:
            text = '0';
            break;
          case 1:
            text = '2';
            break;
          case 2:
            text = '4';
            break;
          case 3:
            text = '6';
            break;
          case 4:
            text = '8';
            break;
          case 5:
            text = '10';
            break;
          default:
            return Container();
        }
    
        return Text(text, style: style, textAlign: TextAlign.center);
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414

    切换的选项卡没有绘制。对应的数据和坐标点之间的换算没做,要根据后台给的数据操作,现在这个只是单纯的绘制。

  • 相关阅读:
    UWB安全数据通讯STS-加密、身份认证
    ArduPilot开源飞控之AP_Baro_MSP
    复杂度(下卷)
    c++调用相机进行保存
    【JavaEE】博客系统【前后端分离版本】
    [ vulhub漏洞复现篇 ] Thinkphp SQL注入 && 敏感信息泄露
    解析设计模式与设计原则:构建可维护性和可扩展性代码的重要性
    手写本地缓存实战2—— 打造正规军,构建通用本地缓存框架
    AWing:1227.分巧克力 (蓝桥杯)
    PMP提分练习
  • 原文地址:https://blog.csdn.net/androidhyf/article/details/134441309