Home > Net >  How to control LayoutBuilder re-building widgets - Flutter
How to control LayoutBuilder re-building widgets - Flutter

Time:11-15

I use LayoutBuilder to control the view when the orientation changes, but it rebuilds the view with every change like when a keyboard is up. How can I control when to re-build the widget only when orientation changes?

class ExampleScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, constraints) {
      double screenHeight = constraints.maxHeight;
      double screenWidth = constraints.maxWidth;

      ///this function checks the orientation of the device, and it's used
      ///for the rebuild of the LayoutBuilder to fit both portrait and 
      ///landscape orientations
      Get.find<ViewController>().setOrientation(context);

      return Scaffold(
        backgroundColor: Colors.green,
        body: SafeArea(
          child: Container(),
        ),
      );
    });
  }
}

CodePudding user response:

Make sure ExampleScreen widget is not the child of some parent widget which is causing it to rebuild.

CodePudding user response:

You can try using OrientationBuilder

Widget build(BuildContext context) {
  return Scaffold(
    backgroundColor: Colors.green,
    body: OrientationBuilder(
      builder: (context, orientation) {
        ///  
      },
    ),
  );
}
  • Related