Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A couple of handy Flutter mixins I use for animations.
import 'dart:async';
import 'package:flutter/material.dart';
/// A mixin to perform context dependent function when the widget loads.
mixin FirstLoad<T extends StatefulWidget> on State<T> {
bool _hasLoaded = false;
bool get isFirstLoad => !_hasLoaded;
/// Calls the given callback on the first load only.
/// This should NOT be called in [initState] as the UI will not have rendered it.
/// This should only be used when an initialised [context] is required.
onFirstLoad(Function callback) {
if (isFirstLoad)
setState(() {
_hasLoaded = true;
/// A mixin which provides common timer functionality and util methods.
mixin StateDelay<T extends StatefulWidget> on State<T> {
List<Timer> _timers = [];
/// Calls the given callback after the given amount of time in milliseconds
delay(int delay, void Function() callback) =>
_timers.add(Timer(Duration(milliseconds: delay), callback));
/// Calls the callback within [setState] after the given amount of time in milliseconds
setStateDelay(int delay, void Function() callback) =>
this.delay(delay, () => setState(callback));
setTimer(Timer timer) => _timers.add(timer);
_cancelTimers() => _timers.forEach((t) => t.cancel());
void dispose() {

This comment has been minimized.

Copy link

@bounty1342 bounty1342 commented Oct 23, 2020


Instead of first load, I would use This would not require a rebuild to access the context.

Safe travel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.