Skip to content

Instantly share code, notes, and snippets.

@iamEtornam
Last active October 2, 2022 11:23
Show Gist options
  • Save iamEtornam/ccdbd9c48798ee8e8c46ee363d6c1024 to your computer and use it in GitHub Desktop.
Save iamEtornam/ccdbd9c48798ee8e8c46ee363d6c1024 to your computer and use it in GitHub Desktop.
How to use MediaQuery in Flutter to determine responsiveness
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