Listening For Device Orientation Changes In Flutter. You can listen to screen size changes, but MediaQuery.of(…) should work as well and should cause rebuilds of your widget when orientation changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import 'dart:ui'; import 'package:flutter/material.dart'; class WidthHeight extends StatefulWidget { WidthHeight({ Key key }) : super(key: key); @override WidthHeightState createState() => new WidthHeightState(); } class WidthHeightState extends State with WidgetsBindingObserver { @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } double width = 0.0; double height = 0.0; @override void didChangeMetrics() { setState(() { width = window.physicalSize.width; height = window.physicalSize.height; }); } @override Widget build(BuildContext context) { return new Text('Width: $width, Height $height'); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.