Flutter SVG rendering

Fonts are a great option for a lot of cases. I’ve been working on a library to render SVGs on a canvas, available here: https://github.com/dnfield/flutter_svg The API as of right now would look something like new SvgPicture.asset(‘asset_name.svg’) to render asset_name.svg (sized to its parent, e.g. a SizedBox). You can also specify a color and blendMode … Read more

How can I build an enum with Dart? [duplicate]

Dart now has support for enums. The rest of this answer is for Dart <= 1.8. If using > 1.8, use Dart’s formal support for enums (explained in another answer). It’s true, the Dart language does not (yet?) have enums. There is an open issue for it. In the meantime, here is an idiomatic Dart … Read more

When to use part/part of versus import/export in Dart?

update 2018/03 part and part of is used more and more for code generation scenarios recently (instead of deprecated transformers) and unlikely to go away anytime soon. Packages like built_value, json_serializable, and many others depend on it. Discouraged is only the patter where all files of a package are tied together to a single library … Read more

Textfield validation in Flutter

A Minimal Example of what you Want: class MyHomePage extends StatefulWidget { @override MyHomePageState createState() { return new MyHomePageState(); } } class MyHomePageState extends State<MyHomePage> { final _text = TextEditingController(); bool _validate = false; @override void dispose() { _text.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(‘TextField Demo’), ), body: … Read more

How do I share an image on iOS and Android using Flutter?

The below will allow you to send a file (specifically an image in this example) using UIActivityViewController on iOS and as a share intent on Android. FileProvider overview (Android) Update pubspec.yaml to reference your image if local (image.jpg in this example) and to use the path_provider plugin in order to access the file system. https://pub.dartlang.org/packages/path_provider … Read more

How do I add Methods or Values to Enums in Dart?

Starting with Dart 2.6 you can define extensions on classes (Enums included). enum Cat { black, white } extension CatExtension on Cat { String get name { switch (this) { case Cat.black: return ‘Mr Black Cat’; case Cat.white: return ‘Ms White Cat’; default: return null; } } void talk() { print(‘meow’); } } Example: Cat … Read more

What is the Dart null checking idiom or best practice?

As of Dart 1.12 null-aware operators are available for this type of situation: bool isConnected(a, b) { bool outConn = outgoing[a]?.contains(b) ?? false; bool inConn = incoming[a]?.contains(b) ?? false; return outConn || inConn; } The ?. operator short-circuits to null if the left-hand side is null, and the ?? operator returns the left-hand side if … Read more

Dart – Encode and decode base64 string

As of 0.9.2 of the crypto package CryptoUtils is deprecated. Use the Base64 APIs in dart:convert and the hex APIs in the convert package instead. import ‘dart:convert’ show utf8, base64; main() { final str=”https://dartpad.dartlang.org/”; final encoded = base64.encode(UTF8.encode(str)); print(‘base64: $encoded’); final str2 = utf8.decode(base64.decode(encoded)); print(str2); print(str == str2); } Try it in DartPad