Skip to content

Instantly share code, notes, and snippets.

View Indy9000's full-sized avatar
🏠
Working from home

Indy M. Indy9000

🏠
Working from home
View GitHub Profile
@Indy9000
Indy9000 / convert-int-to-roman-numerals-and-back.py
Last active September 10, 2020 17:23
Convert an int to roman numeral and back
def toRoman(i:int)->str:
sym = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
num = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
result = ""
while i > 0:
for j in range(len(num)):
m = i // num[j]
result += sym[j] * m
i = i % num[j]
return result
@Indy9000
Indy9000 / text-similarity.dart
Created February 28, 2021 17:46
Text Similarity in 75 Lines of Dart
import 'dart:core';
import 'dart:math';
class Vector {
List<num> vec;
Vector(List<num> el) {
vec = el;
}
operator [](index) => vec[index];
@Indy9000
Indy9000 / flutter-auto-scroll-listview-to-end.dart
Created May 16, 2021 20:11
Flutter auto scroll a listview to the bottom
import 'dart:async';
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
@Indy9000
Indy9000 / dataitem.dart
Created May 31, 2021 15:24
animated-donut-chart-01
class DataItem {
final double value;
final String label;
final Color color;
DataItem(this.value, this.label, this.color);
}
@Indy9000
Indy9000 / main.dart
Created May 31, 2021 15:29
animated-donut-chart-02
void main() {
runApp(MyApp());
}
const pal = [0xFFF2387C, 0xFF05C7F2, 0xFF04D9C4, 0xFFF2B705, 0xFFF26241];
class MyApp extends StatelessWidget {
final List<DataItem> dataset = [
DataItem(0.2, "Comedy", Color(pal[0])),
DataItem(0.25, "Action", Color(pal[1])),
@Indy9000
Indy9000 / donut-chart-widget.dart
Created May 31, 2021 15:40
animated-donut-chart-03
class DonutChartWidget extends StatefulWidget {
final List<DataItem> dataset;
DonutChartWidget(this.dataset);
@override
_DonutChartWidgetState createState() => _DonutChartWidgetState();
}
class _DonutChartWidgetState extends State<DonutChartWidget> {
@override
Widget build(BuildContext context) {
@Indy9000
Indy9000 / donut-chart-painter.dart
Created May 31, 2021 16:20
animated-donut-chart-03
class DonutChartPainter extends CustomPainter {
final List<DataItem> dataset;
DonutChartPainter(this.dataset);
@override
void paint(Canvas canvas, Size size) {
final c = Offset(size.width / 2.0, size.height / 2.0);
final radius = size.width * 0.9;
final rect = Rect.fromCenter(center: c, width: radius, height: radius);
final fullAngle = 360.0;
@Indy9000
Indy9000 / donut-chart-painter.dart
Created May 31, 2021 16:41
animated-donut-chart-04
void drawSector(Canvas canvas, DataItem di, Rect rect, double startAngle,
double sweepAngle) {
final paint = Paint()
..style = PaintingStyle.fill
..color = di.color;
canvas.drawArc(rect, startAngle, sweepAngle, true, paint);
}
@Indy9000
Indy9000 / donut-chart-painter.dart
Created May 31, 2021 16:55
animated-donut-chart-05
TextPainter measureText(
String s, TextStyle style, double maxWidth, TextAlign align) {
final span = TextSpan(text: s, style: style);
final tp = TextPainter(
text: span, textAlign: align, textDirection: TextDirection.ltr);
// ellipsis: "...");
tp.layout(minWidth: 0, maxWidth: maxWidth);
return tp;
}
@Indy9000
Indy9000 / donut-chart-painter.dart
Created May 31, 2021 17:21
animated-donut-chart-06
void drawLabels(Canvas canvas, Offset c, double radius, double startAngle,
double sweepAngle, String label) {
final r = radius * 0.4;
final dx = r * cos(startAngle + sweepAngle / 2.0);
final dy = r * sin(startAngle + sweepAngle / 2.0);
final position = c + Offset(dx, dy);
drawTextCentered(canvas, position, label, labelStyle, 100.0, (Size sz) {
final rect = Rect.fromCenter(
center: position, width: sz.width + 5, height: sz.height + 5);
final rrect = RRect.fromRectAndRadius(rect, Radius.circular(5));