【问题标题】:How to apply linear gradient in flutter charts?如何在颤动图中应用线性渐变?
【发布时间】:2019-10-19 15:04:03
【问题描述】:

如何在颤振图中应用线性渐变?

我尝试过使用颜色类,但我只能应用纯色,无法找到在图表栏中使用渐变的方法。

chart.Series<Person,String>(
    id:"Person3",
    colorFn:(_,__)=>chart.MaterialPalette.red.shadeDefault,
    domainFn: (Person dataPoint, _)=>dataPoint.name,
    measureFn: (Person dataPoint, _)=>dataPoint.no,
    data:data2,
  )

给我一​​些应用线性渐变的方法

【问题讨论】:

    标签: flutter colors gradient


    【解决方案1】:

    使用Container的属性BoxDecoration可以实现它,像这样:

      @override
      Widget build(BuildContext context) {
        return Container(
          width: double.infinity,
          height: 370,
          decoration: BoxDecoration(
              boxShadow: [
                BoxShadow(
                    color: Colors(0XFFA573FF),
                    blurRadius: 10,
                    offset: Offset(2, 3)),
              ],
              borderRadius: BorderRadius.all(Radius.circular(18)),
              gradient: LinearGradient(colors: [
                color: Colors(0XFFA573FF),
                color: Colors(0XFF645AFF),
              ], stops: [
                0.35,
                1
              ], begin: Alignment.topLeft, end: Alignment.bottomRight)),
              child: 
                  SizedBox(
                      height: 280,
                      width: double.infinity,
                      child: StackedAreaCustomColorLineChart(
                          StackedAreaCustomColorLineChart._createSampleData())
                          // replace child with your chart here
                          ),
        );
      }
    

    【讨论】:

    • 我要求提供图表。这个适用于普通小部件,不适用于图表库。
    【解决方案2】:

    AreaSeries 图表示例

    @override
          Widget build(BuildContext context) {
        final List<Color> color = <Color>[];
            color.add(Colors.blue[50]);
            color.add(Colors.blue[100]);
            color.add(Colors.blue);
    
        final List<double> stops = <double>[];
        stops.add(0.0);
        stops.add(0.5);
        stops.add(1.0);
    
        final LinearGradient gradientColors =
            LinearGradient(colors: color, stops: stops);
     return   Container(
                          width: double.infinity,
                          height: 250,
                          child: SfCartesianChart(
                            primaryXAxis: CategoryAxis(),
                            series: <AreaSeries<SalesData, String>>[
                              AreaSeries<SalesData, String>(
                                dataSource: <SalesData>[
                                  SalesData('Jan', 85),
                                  SalesData('Feb', 58),
                                  SalesData('Mar', 64),
                                  SalesData('Apr', 62),
                                  SalesData('May', 78),
                                  SalesData('Jun', 92),
                                  SalesData('Jul', 90),
    
                                ],
                                xValueMapper: (SalesData sales, _) => sales.year,
                                yValueMapper: (SalesData sales, _) => sales.sales,
                                gradient: gradientColors,
                              ),
                            ],
                          ),
                        );
    
    }
    

    【讨论】:

      【解决方案3】:

      显然还没有这样的功能。我设法让它使用以下代码:

      Widget _buildChart() {
        return Container(
          height: 300,
          child: ShaderMask(
            child: charts.BarChart(
              _createRandomData(),
              animate: true,
              primaryMeasureAxis: new charts.NumericAxisSpec(
                  renderSpec: new charts.NoneRenderSpec(), showAxisLine: false),
              domainAxis: new charts.OrdinalAxisSpec(
                  showAxisLine: false, renderSpec: new charts.NoneRenderSpec()),
              layoutConfig: new charts.LayoutConfig(
                  leftMarginSpec: new charts.MarginSpec.fixedPixel(0),
                  topMarginSpec: new charts.MarginSpec.fixedPixel(0),
                  rightMarginSpec: new charts.MarginSpec.fixedPixel(0),
                  bottomMarginSpec: new charts.MarginSpec.fixedPixel(0)),
              defaultRenderer: new charts.BarRendererConfig(
                  cornerStrategy: const charts.ConstCornerStrategy(30)),
            ),
            shaderCallback: (Rect bounds) {
              return LinearGradient(
                begin: Alignment.bottomCenter,
                end: Alignment.topCenter,
                colors: [Color(0xFF51DE93), Color(0xFFFFB540), Color(0xFFFA4169)],
                stops: [
                  0.0,
                  0.5,
                  1.0,
                ],
              ).createShader(bounds);
            },
            blendMode: BlendMode.srcATop,
          )
        );
      }
      

      结果:

      【讨论】:

      • 感谢您的回答。但我有一个查询如何减少高度哦图,因为当我使用你的代码时,大小并没有减少它占用页面的整个高度
      猜你喜欢
      • 2021-05-07
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 2013-01-12
      • 2020-09-27
      相关资源
      最近更新 更多