Created
February 27, 2025 08:24
-
-
Save Muhammad-Haris-2/9aab5ff11d27ab16cf0da128ec36477e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'package:flutter/material.dart'; | |
| class AdaptiveLayout extends StatelessWidget { | |
| static const double smallScreenMaxWidth = 999; | |
| static const double mediumScreenMinWidth = 1000; | |
| static const double mediumScreenMaxWidth = 1200; | |
| static const double largeScreenMinWidth = 1201; | |
| final Widget largeScreen; | |
| final Widget? mediumScreen; | |
| final Widget? smallScreen; | |
| const AdaptiveLayout({ | |
| Key? key, | |
| required this.largeScreen, | |
| this.mediumScreen, | |
| this.smallScreen, | |
| }) : super(key: key); | |
| static bool isSmallScreen(BuildContext context) => | |
| MediaQuery.of(context).size.width <= smallScreenMaxWidth; | |
| static bool isMediumScreen(BuildContext context) => | |
| MediaQuery.of(context).size.width >= mediumScreenMinWidth && | |
| MediaQuery.of(context).size.width <= mediumScreenMaxWidth; | |
| static bool isLargeScreen(BuildContext context) => | |
| MediaQuery.of(context).size.width >= largeScreenMinWidth; | |
| @override | |
| Widget build(BuildContext context) { | |
| return LayoutBuilder( | |
| builder: (_, constraints) { | |
| if (constraints.maxWidth >= largeScreenMinWidth) { | |
| return largeScreen; | |
| } else if (constraints.maxWidth >= mediumScreenMinWidth) { | |
| return mediumScreen ?? largeScreen; | |
| } else { | |
| return smallScreen ?? mediumScreen ?? largeScreen; | |
| } | |
| }, | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment