Skip to content

Instantly share code, notes, and snippets.

@osaxma
osaxma / transitive_reduction.dart
Last active February 7, 2024 08:39
Transitive Reduction Implementation in Dart (Harry Hsu)
/// Return the [Transitive Reduction] for a directed acyclic graph (DAG).
///
/// This function assumes the graph is acyclic and it doesn't check for that.
///
/// [nodes] should be a list of all nodes in the graph.
///
/// [hasPath] should return `true` when the first argument has a path to the second argument.
/// > Note: [hasPath] should return `true` when there is a **path**, not only an edge.
///
/// This function is a port of [jgrapht][] implementation of Harry Hsu's paper:
@osaxma
osaxma / resolve_ast_from_string.dart
Created January 15, 2024 12:46
generate a resolved AST fromString
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/file_system/overlay_file_system.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
final string = '''
class App {
final String id;
final String name;
@osaxma
osaxma / main.dart
Created September 21, 2022 14:20
Replace Western Arabic Numerals with Western ones
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class TextFieldExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return TextField(
inputFormatters: <TextInputFormatter>[
/// Replaces: any of [٠١٢٣٤٥٦٧٨٩] with its respective [0123456789]
...replaceArabicNumeralsFromEastToWestFormatters,
// for SO question: https://stackoverflow.com/q/71134564/10976714
// ignore_for_file: avoid_function_literals_in_foreach_calls, avoid_print
import 'package:flutter/material.dart';
void main() {
runApp(const ContainerWidthExampleApp());
}
class ContainerWidthExampleApp extends StatelessWidget {
import 'package:flutter/material.dart';
void main() {
runApp(const NestedTopicsExample());
}
class NestedTopicsExample extends StatelessWidget {
const NestedTopicsExample({Key? key}) : super(key: key);
@osaxma
osaxma / multiple_text_fields.dart
Last active January 13, 2022 15:47
An Example that shows how multiple TextFields are used together
// ignore_for_file: avoid_function_literals_in_foreach_calls, avoid_print
import 'package:flutter/material.dart';
void main() => runApp(MultipleTextFieldsExampleApp());
class MultipleTextFieldsExampleApp extends StatelessWidget {
const MultipleTextFieldsExampleApp({Key? key}) : super(key: key);
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(const WindowApp());
}
class WindowApp extends StatelessWidget {
const WindowApp({Key? key}) : super(key: key);
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
@osaxma
osaxma / stream_provider_example.dart
Last active January 9, 2022 14:04
an example for stream provider
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
@osaxma
osaxma / quadraticCurvePoint.dart
Last active April 6, 2021 13:48
Find the coordinates of a point along a quadratic Bezier curve in dart.
// credit: https://stackoverflow.com/a/9195706/10976714
// The formulas to calculate the coordinates of a point at any given position (from 0 to 1) on the Bezier curve are:
// x(t) = (1-t)^2 * x1 + 2 * (1-t) * t * x2 + t^2 * x3
// y(t) = (1-t)^2 * y1 + 2 * (1-t) * t * y2 + t^2 * y3
// where (x1, y1) is the starting point, (x2, y2) is the control point and (x3, y3) is the end point.
// get a point along the Bezier curve line
// If you pass the start, end & control points, along with 0.5 for the halfway position, you get the offset to that point
Offset getQuadraticCurvePoint(Offset start, Offset control, Offset end, double position) {