git-touch-android-ios-app/lib/widgets/contribution.dart

101 lines
2.8 KiB
Dart
Raw Normal View History

import 'dart:math';
2022-09-17 14:35:45 +02:00
import 'package:flutter/widgets.dart';
2022-09-12 16:59:17 +02:00
import 'package:from_css_color/from_css_color.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:provider/provider.dart';
const contributionEmptyColor = '#ebedf0';
const contributionColors = ['#9be9a8', '#40c463', '#30a14e', '#216e39'];
class HideScrollbar extends StatelessWidget {
2022-09-25 19:59:30 +02:00
const HideScrollbar({super.key, this.child});
2022-09-21 18:28:21 +02:00
final Widget? child;
@override
Widget build(BuildContext context) {
return NotificationListener<ScrollNotification>(
onNotification: (_) => true,
2021-05-16 09:16:35 +02:00
child: child!,
);
}
}
class ContributionDay {
ContributionDay({this.hexColor, this.count})
: assert(hexColor != null || count != null);
2022-09-21 18:28:21 +02:00
String? hexColor;
int? count;
}
class ContributionWidget extends StatelessWidget {
2022-09-17 17:15:01 +02:00
2021-05-16 09:16:35 +02:00
ContributionWidget({required this.weeks}) {
int? maxCount;
2022-09-24 07:41:46 +02:00
for (final week in weeks!) {
for (final day in week) {
if (day.count != null) {
if (maxCount == null) {
2022-09-24 07:41:46 +02:00
for (final week in weeks!) {
for (final day in week) {
2021-05-16 09:16:35 +02:00
maxCount = max(day.count!, maxCount ?? 0);
}
}
}
if (day.count == 0) {
day.hexColor = contributionEmptyColor;
} else {
// TODO: algorithm
2021-05-16 09:16:35 +02:00
final level = (day.count! * 4) ~/ (maxCount! + 1);
day.hexColor = contributionColors[level];
}
}
}
}
}
2022-09-21 18:28:21 +02:00
final Iterable<Iterable<ContributionDay>>? weeks;
static Color _revertColor(Color color) {
return Color.fromRGBO(
0xff - color.red, 0xff - color.green, 0xff - color.blue, 1);
}
@override
Widget build(BuildContext context) {
final theme = context.watch<ThemeModel>();
return Container(
alignment: Alignment.center,
padding: CommonStyle.padding,
child: HideScrollbar(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
reverse: true,
child: Wrap(
spacing: 3,
children: [
2021-05-16 09:16:35 +02:00
for (final week in weeks!)
Wrap(
direction: Axis.vertical,
spacing: 3,
children: [
for (final day in week)
2022-09-17 17:15:01 +02:00
Container(
width: 10,
height: 10,
2022-09-17 17:15:01 +02:00
decoration: BoxDecoration(
color: theme.brightness == Brightness.dark
? _revertColor(fromCssColor(day.hexColor!))
: fromCssColor(day.hexColor!),
borderRadius: BorderRadius.circular(2),
),
)
],
)
],
),
)),
);
}
}