Created
May 22, 2021 12:01
-
-
Save shivanshtalwar0/67f329dc80757fcea0b000b0a6a841f3 to your computer and use it in GitHub Desktop.
bootstrap inspired flutter widget to handle responsive breakpoints to aid in development of scalable user interfaces
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/widgets.dart'; | |
class Responsive { | |
bool get isXl => width >= 1200; | |
bool get isLg => width >= 992 && width < 1200; | |
bool get isMd => width >= 768 && width < 992; | |
bool get isSm => width >= 576 && width < 768; | |
bool get isXs => width < 576; | |
double get width => MediaQuery.of(_context).size.width; | |
late BuildContext _context; | |
static Responsive of(BuildContext context) { | |
Responsive temp = Responsive(); | |
temp._context = context; | |
return temp; | |
} | |
} | |
class ResponsiveBuilder extends StatelessWidget { | |
final Widget? xl; | |
final Widget? lg; | |
final Widget? md; | |
final Widget? sm; | |
final Widget? xs; | |
final Key? key; | |
final Widget? mobile; | |
final Widget? tablet; | |
final Widget? desktop; | |
final bool? deviceMode; | |
ResponsiveBuilder( | |
{this.key, | |
this.xs, | |
this.xl, | |
this.lg, | |
this.md, | |
this.sm, | |
this.deviceMode = false, | |
this.mobile, | |
this.tablet, | |
this.desktop}) | |
: super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
Widget child; | |
if (deviceMode!) { | |
if (Responsive.of(context).isXs || Responsive.of(context).isSm) { | |
child = this.mobile!; | |
} else if (Responsive.of(context).isMd || Responsive.of(context).isLg) { | |
child = this.tablet!; | |
} else { | |
child = this.desktop!; | |
} | |
} else { | |
if (Responsive.of(context).isXl && this.xl != null) { | |
child = this.xl!; | |
} else if (Responsive.of(context).isLg && this.lg != null) { | |
child = this.lg!; | |
} else if (Responsive.of(context).isMd && this.md != null) { | |
child = this.md!; | |
} else if (Responsive.of(context).isSm && this.sm != null) { | |
child = this.sm!; | |
} else if (Responsive.of(context).isXs && this.xs != null) { | |
child = this.xs!; | |
} else { | |
child = LimitedBox( | |
maxWidth: 0.0, | |
maxHeight: 0.0, | |
child: ConstrainedBox(constraints: const BoxConstraints.expand()), | |
); | |
} | |
} | |
return child; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment