Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BarChartRodData asserting You cannot provide both color and gradient at the same time #1121

Closed
absar opened this issue Aug 16, 2022 · 3 comments
Labels
enhancement New feature or request Fundamental

Comments

@absar
Copy link

absar commented Aug 16, 2022

BarChartRodData is asserting You cannot provide both color and gradient at the same time when we give the end users option to switch between color mode and gradient mode. Even though we are either providing color or gradient but it's asserting probably due to lerp between the colors and gradients, the assert in BarChartRodData is not serving much purpose hence it should be removed, since PaintExtension setColorOrGradient is already handling color and gradient even if both are provided.

To Reproduce

  • Run the code
  • Press the toggle button twice and see the assert in console
Code
import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';

void main() => runApp(const FlChartBarGradientColorAssertIssue());

class FlChartBarGradientColorAssertIssue extends StatelessWidget {
const FlChartBarGradientColorAssertIssue({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) => const MaterialApp(home: HomePage());
}

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

@override
State<StatefulWidget> createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
bool doGradientFill = false;

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    body: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text('Press me twice to assert'),
            Switch(
              value: doGradientFill,
              onChanged: (_) =>
                  setState(() => doGradientFill = !doGradientFill),
            ),
          ],
        ),
        AspectRatio(
          aspectRatio: 1.7,
          child: Card(
            elevation: 0,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4)),
            color: const Color(0xff2c4260),
            child: BuildBarChart(doGradientFill: doGradientFill),
          ),
        ),
      ],
    ),
  );
}
}

class BuildBarChart extends StatelessWidget {
const BuildBarChart({super.key, this.doGradientFill = false});

final bool doGradientFill;

@override
Widget build(BuildContext context) {
  return BarChart(
    BarChartData(
      barTouchData: barTouchData,
      titlesData: titlesData,
      borderData: borderData,
      barGroups: barGroups,
      gridData: FlGridData(show: false),
      alignment: BarChartAlignment.spaceAround,
      maxY: 20,
    ),
  );
}

BarTouchData get barTouchData => BarTouchData(
      enabled: false,
      touchTooltipData: BarTouchTooltipData(
        tooltipBgColor: Colors.transparent,
        tooltipPadding: const EdgeInsets.all(0),
        tooltipMargin: 8,
        getTooltipItem: (group, groupIndex, rod, rodIndex) {
          return BarTooltipItem(
            rod.toY.round().toString(),
            const TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
          );
        },
      ),
    );

Widget getTitles(double value, TitleMeta meta) {
  const style = TextStyle(
      color: Color(0xff7589a2), fontWeight: FontWeight.bold, fontSize: 14);
  String text;
  switch (value.toInt()) {
    case 0:
      text = 'Mn';
      break;
    case 1:
      text = 'Te';
      break;
    case 2:
      text = 'Wd';
      break;
    default:
      text = '';
      break;
  }
  return SideTitleWidget(
      axisSide: meta.axisSide, space: 4.0, child: Text(text, style: style));
}

FlTitlesData get titlesData => FlTitlesData(
      show: true,
      bottomTitles: AxisTitles(
        sideTitles: SideTitles(
            showTitles: true, reservedSize: 30, getTitlesWidget: getTitles),
      ),
      leftTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
      topTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
      rightTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
    );

FlBorderData get borderData => FlBorderData(show: false);

final _barsGradient = const LinearGradient(
  colors: [Colors.lightBlueAccent, Colors.greenAccent],
  begin: Alignment.bottomCenter,
  end: Alignment.topCenter,
);

List<BarChartGroupData> get barGroups => [
      BarChartGroupData(
        x: 0,
        barRods: [
          BarChartRodData(
            toY: 8,
            /// We are either providing color or gradient but it's asserting
            /// probably due to lerp between the colors and gradients
            color: !doGradientFill ? Colors.greenAccent : null,
            gradient: doGradientFill ? _barsGradient : null,
          )
        ],
        showingTooltipIndicators: [0],
      ),
      BarChartGroupData(
        x: 1,
        barRods: [
          BarChartRodData(
            toY: 10,
            color: !doGradientFill ? Colors.greenAccent : null,
            gradient: doGradientFill ? _barsGradient : null,
          )
        ],
        showingTooltipIndicators: [0],
      ),
      BarChartGroupData(
        x: 2,
        barRods: [
          BarChartRodData(
            toY: 14,
            color: !doGradientFill ? Colors.greenAccent : null,
            gradient: doGradientFill ? _barsGradient : null,
          )
        ],
        showingTooltipIndicators: [0],
      ),
    ];
}

Versions

  • Flutter Channel stable 3.0.5
  • fl_chart: ^0.55.1
@imaNNeo
Copy link
Owner

imaNNeo commented Jan 30, 2023

Hi @absar,

First of all thanks for your well-defined issue.
Sorry for my late answer.
We put that assertion to help developers to prevent passing both gradient and color at the same time.

But we can remove the assertion.
FYI I checked BoxDecoration and they did not put any assertion to check. They just set gradient with higher priority than a color.

So we will do the same.

@imaNNeo imaNNeo added enhancement New feature or request Fundamental labels Jan 30, 2023
imaNNeo added a commit that referenced this issue Jan 30, 2023
…property in the BarChartRodData and BackgroundBarChartRodData, #1121.
imaNNeo added a commit that referenced this issue Jan 30, 2023
…roperty in the BarChartRodData and BackgroundBarChartRodData, #1121.
imaNNeo added a commit that referenced this issue Jan 30, 2023
…` property in the BarChartRodData and BackgroundBarChartRodData, #1121.
imaNNeo added a commit that referenced this issue Jan 30, 2023
…` property in the BarChartRodData and BackgroundBarChartRodData, #1121.
@Peetee06
Copy link
Contributor

@imaNNeo this can be closed as done, right?

@imaNNeo
Copy link
Owner

imaNNeo commented Nov 26, 2024

@Peetee06
Yes, thanks for letting me know! I appreciate it.

@imaNNeo imaNNeo closed this as completed Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Fundamental
Projects
None yet
Development

No branches or pull requests

3 participants