Skip to content

Instantly share code, notes, and snippets.

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

Christian Findlay MelbourneDeveloper

🏠
Working from home
View GitHub Profile
@MelbourneDeveloper
MelbourneDeveloper / main.dart
Created June 25, 2023 07:13
Navigation Extension
import 'package:flutter/material.dart';
extension NavigationExtension on BuildContext {
Future<void> navigateTo(String routeName, {Object? arguments}) {
return Navigator.pushNamed(this, routeName, arguments: arguments);
}
}
void main() {
runApp(MaterialApp(
@MelbourneDeveloper
MelbourneDeveloper / main.dart
Last active February 15, 2025 03:59
Material Design Seed to Color Scheme Viewer
import 'package:flutter/material.dart';
int? hexToInteger(String hex) => int.tryParse(hex, radix: 16);
Color contrastColor(Color color) {
final Brightness brightness = ThemeData.estimateBrightnessForColor(color);
return brightness == Brightness.dark ? Colors.white : Colors.black;
}
extension StringColorExtensions on String {
@MelbourneDeveloper
MelbourneDeveloper / main.dart
Last active May 30, 2023 06:40
Vehicle Example
class Vehicle {
//Defaults to car
String get type => "car";
void drive() {
print("You're driving a $type.");
}
}
class Tractor implements Vehicle {
@override
@MelbourneDeveloper
MelbourneDeveloper / main.dart
Created May 15, 2023 02:13
JSON Placeholder Future Records Sample
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
final random = Random();
void main() => runApp(const MyApp());
@MelbourneDeveloper
MelbourneDeveloper / mani.dart
Created May 13, 2023 08:31
Bloc Style Functions
//Programming is too easy. What if we wrote functions like this?
//Instead of just calling the function, we create types to bundle
//up the arguments and then pass the type to the function that accepts
//a generic type.
//
//This is the essence of the Bloc pattern.
class AdditionArguments {
final num a;
final num b;
@MelbourneDeveloper
MelbourneDeveloper / main.dart
Created May 13, 2023 08:30
Bloc Style With Sealed Event Types
//Programming is too easy. What if we wrote functions like this?
//Instead of just calling the function, we create types to bundle
//up the arguments and then pass the type to the function that accepts
//a generic type.
//
//This is the essence of the Bloc pattern.
sealed class Event {}
class AdditionArguments extends Event {
@MelbourneDeveloper
MelbourneDeveloper / main.dart
Created May 11, 2023 22:58
Flutter Example
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: FutureBuilder(
future: Future<String>.delayed(
const Duration(seconds: 3), () => 'Hello World!'),
@MelbourneDeveloper
MelbourneDeveloper / main.dart
Created May 11, 2023 22:30
Multiple Values and when
(int a, int b) returnMulti() => (1, 2);
void main() {
var numbers = returnMulti();
var dayNumber = switch (numbers) {
(int a, int b) when a == 1 && b == 2 => 'One and Two',
(_, _) => 'Default'
};
print(dayNumber);
}
import 'dart:math';
sealed class Shape {}
class Square extends Shape {
Square(this.length, this.width);
final double length;
final double width;
}
@MelbourneDeveloper
MelbourneDeveloper / main.dart
Created May 11, 2023 21:44
Pattern Matching Example
abstract class Animal {
String get name;
}
class Dog extends Animal {
@override
String get name => 'Spot';
}
class Cat extends Animal {