i wan to change my layout from landscape to portrait and vice versa inside my app (depending on the rotation of the device by the user) in flutter
i tried using OrientationBuilder and MediaQuery but all of them read only for first time (even when user change the orientation mode) it doesn’t detect the changes.
even i tried the WidgetsBindingObserver by adding:
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
and use didChangeMetrics() but same issue:
@override
void didChangeMetrics() {
final orientation =
WidgetsBinding.instance.window.physicalSize.aspectRatio > 1
? Orientation.landscape
: Orientation.portrait;
_currentOrientation = MediaQuery.of(context).orientation;
print('Before Orientation Change: $_currentOrientation');
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
setState(() {
_currentOrientation = MediaQuery.of(context).orientation;
});
print('After Orientation Change: $_currentOrientation');
});
}
i even tries to add setPrefrences inside the didChangeMetrics but didn’t work either!