Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adityadroid/77b556c819e7513e8732413ee5362aac to your computer and use it in GitHub Desktop.
Save adityadroid/77b556c819e7513e8732413ee5362aac to your computer and use it in GitHub Desktop.
List of my favorite extension methods in Flutter.
import 'package:flutter/material.dart';
extension BuildContextExtensions on BuildContext {
ThemeData get theme => Theme.of(this);
TextTheme get textTheme => theme.textTheme;
ColorScheme get colorScheme => theme.colorScheme;
DefaultTextStyle get defaultTextStyle => DefaultTextStyle.of(this);
MediaQueryData get mediaQuery => MediaQuery.of(this);
NavigatorState get navigator => Navigator.of(this);
FocusScopeNode get focusScope => FocusScope.of(this);
ScaffoldState get scaffold => Scaffold.of(this);
ScaffoldMessengerState get scaffoldMessenger => ScaffoldMessenger.of(this);
}
extension MaterialStateHelpers on Iterable<MaterialState> {
bool get isHovered => contains(MaterialState.hovered);
bool get isFocused => contains(MaterialState.focused);
bool get isPressed => contains(MaterialState.pressed);
bool get isDragged => contains(MaterialState.dragged);
bool get isSelected => contains(MaterialState.selected);
bool get isScrolledUnder => contains(MaterialState.scrolledUnder);
bool get isDisabled => contains(MaterialState.disabled);
bool get isError => contains(MaterialState.error);
}
extension IterableExtensions on Iterable {
bool containsAny(Iterable<Object?> other) => other.any((e) => contains(e));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment