contributions_chart 1.0.0%2B4

SDKflutter
Platformandroidioswindowslinuxmacosweb

A Flutter widget that displays a GitHub contributions graph. Provides a visualization similar to the contribution graph on GitHub profiles.

Contributions Chart

A Flutter widget that displays a GitHub-style contributions graph in your Flutter application. contributions_chart_radius

Features

  • Visual representation similar to GitHub profile contribution graphs
  • Display contributions for an entire year or recent contributions
  • Highly customizable colors and styling
  • Support for tap/click interactions on contribution cells
  • Calendar labels (month names on top, day names on left)
  • Automatic scaling based on widget dimensions
  • Responsive design that works on all screen sizes
  • Light and dark theme support through color customization

Installation

Add this package to your pubspec.yaml file:

dependencies:
  contributions_chart: ^1.0.0+3

Then run:

flutter pub get

Usage

Basic Usage

import 'package:contributions_chart/contributions_chart.dart';

// ...

GitHubContributionsWidget(
  githubUrl: 'https://github.com/username',
  width: 300,
  height: 100,
),

Advanced Usage with Customization

GitHubContributionsWidget(
  githubUrl: 'username', // GitHub username or full profile URL
  year: 2023, // Specific year to display (default: current year)
  showRecent: false, // Show recent contributions instead
  width: 350, // Width (at least one of width or height is required)
  height: 120, // Height
  backgroundColor: Colors.black, // Background color
  contributionColors: [ // Colors for contribution levels (0-4)
    Colors.grey.shade900, // 0 contributions
    Colors.blue.shade900, // 1 contribution
    Colors.blue.shade700, // 2 contributions
    Colors.blue.shade500, // 3 contributions
    Colors.blue.shade300, // 4+ contributions
  ],
  // Alternatively, use a single color with different opacities:
  singleContributionColor: Colors.blue,
  singleColorOpacities: [0.1, 0.3, 0.5, 0.7, 0.9],
  
  cellSpacing: 2.0, // Spacing between cells
  squareBorderRadius: 2.0, // Border radius for contribution squares
  showCalendar: true, // Show month and day labels
  
  // Calendar label styling
  monthLabelStyle: TextStyle(color: Colors.white, fontSize: 10),
  dayLabelStyle: TextStyle(color: Colors.white, fontSize: 10),
  customMonthLabels: ['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D'],
  customDayLabels: ['M', 'W', 'F'],
  
  // Cell border
  contributionBorder: Border.all(color: Colors.white10, width: 0.5),
  emptyColor: Colors.grey.shade800, // Color for cells with no contributions
  
  // Custom loading indicator
  loadingWidget: Center(child: CircularProgressIndicator()),
  
  // Tap/click handler for cells
  onCellTap: (DateTime date, int contributionCount) {
    print('On $date: $contributionCount contributions');
  },
);

Widget Properties

PropertyTypeDescription
githubUrlStringGitHub username or full profile URL (required)
yearint?Year to display contributions for (default: current year)
showRecentboolShow recent contributions instead of a specific year
widthdouble?Width of the widget (one of width or height required)
heightdouble?Height of the widget (one of width or height required)
backgroundColorColorBackground color of the widget
contributionColorsList<Color>?Array of colors for different contribution levels (0-4)
singleContributionColorColor?Single color option (if set, will override contributionColors)
singleColorOpacitiesList<double>?Opacity values for single color mode (0.0 to 1.0)
cellSpacingdoubleSpacing between contribution cells
squareBorderRadiusdoubleBorder radius for contribution squares
showCalendarboolShow calendar labels (month names on top, day names on left)
monthLabelStyleTextStyle?Text style for month names
dayLabelStyleTextStyle?Text style for day names
customMonthLabelsList<String>?Custom month names (12 names required if provided)
customDayLabelsList<String>?Custom day names (3 names required for Mon/Wed/Fri)
contributionBorderBorder?Border for each contribution cell
emptyColorColor?Color for cells with no contributions
loadingWidgetWidget?Custom loading widget
tooltipTextFormatString?Custom tooltip text format
onCellTapFunction(DateTime, int)?Callback when a contribution cell is tapped
urlPrefixString?URL prefix (optional, typically used to bypass CORS issues)
headersMap<String, String>?Custom HTTP headers for requests (useful for CORS issues)

How It Works

The GitHubContributionsWidget fetches contribution data from GitHub for the specified user and year. It works by:

  1. Parsing GitHub's contribution calendar HTML
  2. Extracting contribution data from the DOM
  3. Organizing data into a matrix representation
  4. Rendering a visual representation using Flutter's CustomPainter

The widget is responsive and will adapt to whatever size you provide, maintaining the proper aspect ratio of the GitHub contribution graph.

Architecture

The package follows a clean architecture with several components:

  • GitHubContributionsWidget: Main widget class with the public API
  • ContributionData: Data model for contribution matrix
  • GitHubService: Service for fetching and parsing GitHub contribution data
  • ContributionsPainter: CustomPainter for rendering the contribution graph
  • LayoutUtils: Helper functions for layout calculations

Examples

Default Dark Theme (GitHub Style)

GitHubContributionsWidget(
  githubUrl: 'username',
  width: 400,
  height: 150,
),

Custom Colors

GitHubContributionsWidget(
  githubUrl: 'username',
  width: 400,
  height: 150,
  backgroundColor: Colors.white,
  contributionColors: [
    Colors.grey.shade200,
    Colors.green.shade100,
    Colors.green.shade300,
    Colors.green.shade500,
    Colors.green.shade700,
  ],
),

Single Color with Opacity Levels

GitHubContributionsWidget(
  githubUrl: 'username',
  width: 400,
  height: 150,
  singleContributionColor: Colors.purple,
  singleColorOpacities: [0.1, 0.25, 0.5, 0.75, 1.0],
),

With Calendar Labels

GitHubContributionsWidget(
  githubUrl: 'username',
  width: 400,
  height: 150,
  showCalendar: true,
),

Handling CORS Issues

For web applications or when accessing GitHub from restricted domains, you can use custom headers to resolve CORS issues:

GitHubContributionsWidget(
  githubUrl: 'username',
  width: 400,
  height: 150,
  headers: {
    'User-Agent': 'MyApp/1.0',
    'Accept': 'text/html,application/xhtml+xml',
    'Access-Control-Allow-Origin': '*',
  },
  // Optional: Use with proxy services
  urlPrefix: 'https://api.codetabs.com/v1/proxy?quest=',
),

Contributing

Contributions are welcome! Please feel free to submit a Pull Request or open issues for bugs, feature requests, or enhancements.

License

This package is distributed under the MIT License.