Last active
October 2, 2022 11:23
-
-
Save iamEtornam/ccdbd9c48798ee8e8c46ee363d6c1024 to your computer and use it in GitHub Desktop.
How to use MediaQuery in Flutter to determine responsiveness
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 LayoutBuilderExample extends StatelessWidget { | |
const LayoutBuilderExample({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('LayoutBuilder Example')), | |
body: LayoutBuilder(builder: (context, constraints) { | |
if (constraints.maxWidth > 400) { | |
return Center( | |
child: Row( | |
children: [ | |
Container( | |
height: constraints.maxHeight, | |
width: constraints.maxWidth / 4, | |
color: Colors.red, | |
child: const Center(child: Text('Side navigation')), | |
), | |
Expanded( | |
child: Container( | |
height: constraints.maxHeight, | |
color: Colors.blue, | |
child: const Center(child: Text('Main view')), | |
)) | |
], | |
), | |
); | |
} else { | |
return Center( | |
child: Container( | |
height: constraints.maxHeight, | |
width: constraints.maxWidth, | |
color: Colors.red, | |
child: const Center( | |
child: Text('Mobile view'), | |
), | |
), | |
); | |
} | |
}), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment