Skip to content

Instantly share code, notes, and snippets.

import 'package:flutter/material.dart';
// based on https://medium.com/flutter/learning-flutters-new-navigation-and-routing-system-7c9068155ade
// but update for null safety by Howard "Horizons" Chong, @chonghorizons on github
void main() {
runApp(BooksApp());
}
class Book {
@chonghorizons
chonghorizons / jsonEncodeGotcha.dart
Last active December 8, 2021 10:16
jsonEncode, extensions gotcha
// See https://stackoverflow.com/questions/70272959/jsonencode-doesnt-work-with-enum-extensions-is-there-a-workaround
import 'dart:convert';
enum Day { monday, tuesday }
extension ParseToJsonString on Day {
String toJson() {
return this.toString().split('.').last;
}
// This code is distributed under the MIT License.
// Copyright (c) 2018 Felix Angelov.
// Ported to dartpad.dev 2021 by Howard "horizons" Chong.
// You can find the original at https://github.com/felangel/bloc.
// Specifically, see https://github.com/felangel/bloc/tree/master/examples/flutter_counter
//
// The tutorial/guide at https://bloclibrary.dev/#/fluttercountertutorial describes this code.
// Linkback to DartPad: https://dartpad.dev/90da115645caf06ffe10dc8ab170f347
//
// ===== TROUBLESHOOTING INSTRUCTIONS =====
@chonghorizons
chonghorizons / flexiblesWorkWithEmptyContainers.dart
Created November 28, 2021 04:45
Flexibles Work with Empty Containers
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Empty Containers aren't rendered",
debugShowCheckedModeBanner: false,
@chonghorizons
chonghorizons / FlexibleWithEmptyContainers.dart
Last active November 27, 2021 01:10
Empty Containers don't render for Flexible, Flutter
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Empty Containers aren't rendered",
debugShowCheckedModeBanner: false,
// Copyright 2017, 2020 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() {
runApp(
const FriendlyChatApp(),
);
@chonghorizons
chonghorizons / FractionalBoxPlay.dart
Last active November 18, 2021 20:42
Fractional Box in a Column, not working the way one would expect
// Copyright (c) 2021 Howard Chong
// See discussion of FractionalSizedBox in https://stackoverflow.com/questions/57242651/using-fractionallysizedbox-in-a-row/70026340#70026340
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
@chonghorizons
chonghorizons / star_clipping_Dartpad.dart
Created November 6, 2021 21:42
Flutter, Star Path for clipping, N-pointed star clipper for Flutter
//Based on the blog article by "The Plumber" https://www.kindacode.com/article/flutter-drawing-an-n-pointed-star-with-customclipper/
// main.dart
import 'package:flutter/material.dart';
import 'dart:math' as math;
// This custom clipper help us achieve n-pointed star shape
class StarClipper extends CustomClipper<Path> {
/// The number of points of the star
@chonghorizons
chonghorizons / final_const_dartpad_Nov2021.dart
Created November 6, 2021 19:39
Const and Final, Dartpad, samples of what can and cannot be edited, for a String and a Map
// Note: Although a final object cannot be modified, its fields can be changed. In comparison, a const object and its fields cannot be changed: they’re immutable.
void main() {
const String name='Howard';
final stats= {'age': 42, 'income': 0};
const unchangeableStats={'hair': "black", "race": "chinese"};
var normalMap= {};
print("My age is ${stats['age']}");
stats['age']=stats['age']!.toInt()+1;
@chonghorizons
chonghorizons / playwithclass_Dartpad.dart
Last active November 6, 2021 19:08
DART: Playing with class, initialization list.
import 'dart:math' as math;
const double originX=5;
const double originY=5;
class Point {
double x;
double y;