Created
June 7, 2020 00:51
-
-
Save kangabru/dbe256f2d6e6e8d357a523ad7f2ce21b to your computer and use it in GitHub Desktop.
A couple of handy Flutter mixins I use for animations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
callback(); | |
}); | |
} | |
} | |
/// 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()); | |
@override | |
void dispose() { | |
_cancelTimers(); | |
super.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Instead of first load, I would use https://api.flutter.dev/flutter/widgets/State/didChangeDependencies.html. This would not require a rebuild to access the context.
Safe travel.