Created
June 22, 2020 23:06
-
-
Save dhruvilp/fa0359565d2c6787709797a66a52f657 to your computer and use it in GitHub Desktop.
Flutter Responsive Widget
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
class ResponsiveWidget extends StatelessWidget { | |
final Widget largeScreen; | |
final Widget mediumScreen; | |
final Widget smallScreen; | |
const ResponsiveWidget({ | |
Key key, | |
@required this.largeScreen, | |
this.mediumScreen, | |
this.smallScreen, | |
}) : super(key: key); | |
static bool isSmallScreen(BuildContext context) { | |
return MediaQuery.of(context).size.width < 800; | |
} | |
static bool isMediumScreen(BuildContext context) { | |
return MediaQuery.of(context).size.width > 800 && | |
MediaQuery.of(context).size.width < 1200; | |
} | |
static bool isLargeScreen(BuildContext context) { | |
return MediaQuery.of(context).size.width > 800; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return LayoutBuilder( | |
builder: (context, constraints) { | |
if (constraints.maxWidth > 800) { | |
return largeScreen; | |
} else if (constraints.maxWidth < 1200 && constraints.maxWidth > 800) { | |
return mediumScreen ?? largeScreen; | |
} else { | |
return smallScreen ?? largeScreen; | |
} | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment