Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created May 4, 2025 18:24
Show Gist options
  • Save fredgrott/422d92498e56d944b92fa3f9060ac66d to your computer and use it in GitHub Desktop.
Save fredgrott/422d92498e56d944b92fa3f9060ac66d to your computer and use it in GitHub Desktop.
breakpoints
// Copyright 2025 Fredrick Allan Grott. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// ignore_for_file: avoid_classes_with_only_static_members, avoid_bool_literals_in_conditional_expressions
import 'package:flutter/material.dart';
import 'package:userinterface/core/utils/window_size_enum.dart';
/// Helper class for defining breakpoints in a responsive UI design.
class Breakpoints {
Breakpoints();
/// Determines the current layout based on the width of the screen.
///
/// Returns a [LayoutEnum] value that corresponds to the current layout.
///
/// Throws an [Exception] if none of the breakpoints match the current width.
static WindowSizeEnum getLayout(BuildContext context) {
if (isCompact(context)) {
return WindowSizeEnum.compact;
} else if (isMedium(context)) {
return WindowSizeEnum.medium;
} else if (isExpanded(context)) {
return WindowSizeEnum.expanded;
} else if (isLarge(context)) {
return WindowSizeEnum.large;
} else if (isExtraLarge(context)) {
return WindowSizeEnum.extraLarge;
} else {
throw Exception('Bad condition!');
}
}
static EdgeInsetsGeometry getLayoutSpacing(BuildContext context) {
if (isCompact(context)) {
return EdgeInsets.symmetric(
horizontal: WindowSizeEnum.compact.margin,
vertical: WindowSizeEnum.compact.verticalPadding,
);
} else if (isMedium(context)) {
return EdgeInsets.symmetric(
horizontal: WindowSizeEnum.medium.margin,
vertical: WindowSizeEnum.medium.verticalPadding,
);
} else if (isExpanded(context)) {
return EdgeInsets.symmetric(
horizontal: WindowSizeEnum.expanded.margin,
vertical: WindowSizeEnum.expanded.verticalPadding,
);
} else if (isLarge(context)) {
return EdgeInsets.symmetric(
horizontal: WindowSizeEnum.large.margin,
vertical: WindowSizeEnum.large.verticalPadding,
);
} else if (isExtraLarge(context)) {
return EdgeInsets.symmetric(
horizontal: WindowSizeEnum.extraLarge.margin,
vertical: WindowSizeEnum.extraLarge.verticalPadding,
);
} else {
throw UnimplementedError('Bad breakpoint');
}
}
/// Determines if the screen is in the compact layout.
///
/// Returns `true` if the screen width is less than 600 pixels, `false` otherwise.
static bool isCompact(BuildContext context) {
return context.size!.width < WindowSizeEnum.compact.endWidth ? true : false;
}
/// Determines if the screen is in the medium layout.
///
/// Returns `true` if the screen width is between 600 and 840 pixels, `false` otherwise.
static bool isMedium(BuildContext context) {
return context.size!.width >= WindowSizeEnum.medium.beginWidth &&
context.size!.width < WindowSizeEnum.expanded.beginWidth
? true
: false;
}
/// Determines if the screen is in the extended layout.
///
/// Returns `true` if the screen width is greater than 840 pixels, `false` otherwise.
static bool isExpanded(BuildContext context) {
return context.size!.width >= WindowSizeEnum.expanded.beginWidth &&
context.size!.width < WindowSizeEnum.large.beginWidth
? true
: false;
}
static bool isLarge(BuildContext context) {
return context.size!.width >= WindowSizeEnum.large.beginWidth &&
context.size!.width < WindowSizeEnum.extraLarge.beginWidth
? true
: false;
}
static bool isExtraLarge(BuildContext context) {
return context.size!.width >= WindowSizeEnum.extraLarge.beginWidth ? true : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment