Using Mixins in Google Dart / Google Flutter

Using Mixins in Google Dart / Google Flutter

Introduction

As mentioned at the start of this book, a Mixin is a class that contains methods for use by other classes without it having to be the parent class of those other classes.

So, a Mixin is a class you can use code from without having to inherit from. It enables developers to piecemeal classes together without having to get involved with inheritance, abstract classes etc.

Basic Example

https://github.com/markclow/flutter_book_examples/blob/master/mixins/lib/main.dart

Serialization & Deserialization 

Mixins are often used to merge generated code into your code.  Code generators are often used to generate code to serialize and deserialize data.  You run the code generator when you do the build and it creates abstract classes containing code to serialize and deserialize your data.  Your code then uses the ‘with’ + the abstract class name to include that code in your class as a mixin.

https://github.com/markclow/flutter_book_examples/blob/a84269f9a9f77c29142439d149a2adb2a8f32d6d/deserialize_with_generated_code/lib/main.dart

Animations

Mixins are often used with Explicit animation code, where some animation has been coded into a StatefulWidget. 

SingleTickerProviderStateMixin contains ticker animation code which you combine into your class to add ticker functionality to animate every tick.

https://github.com/markclow/flutter_book_examples/blob/94c708c5bb44b2fa451bb3d8c6516aa019d13c1f/animated_progress_circle/lib/main.dart