0
votes

To get the size/position of a widget on screen, I can use GlobalKey to get its BuildContext and then find the RenderBox.

But for ErrorWidget(Red Screen) when build() error happened I want to calculate the error area's size, and then decide whether to destroy the page or replace with other widget e.g. Container().

I have already used ErrorWidget.builder create custom ErrorWidget, but need to be more precise, different sizes of ErrorWidget are treated differently. How to get ErrorWidget size for rebuild?

2

2 Answers

0
votes

ErrorWidget do not evade the widgets rules. A widget cannot depend on the size of anything else.

You can, however, use LayoutBuilder to calculate the available size.

0
votes

LayoutBuilder Widget can help us know how much space is available for the child widget, before finally build it. It's builder function has parameters BuildContext context, BoxConstraints constraints.

BoxConstraints constraints provides us with the opportunity to execute custom logic.

ErrorWidget.builder = (FlutterErrorDetails details) {
  return LayoutBuilder(
    builder: (BuildContext context, BoxConstraints constraints) {
      Size screenSize = MediaQuery.of(context).size;
      double screenRatio = (constraints.maxWidth * constraints.maxHeight) /
          (screenSize.width * screenSize.height);

      if (screenRatio < ACCEPTABLE_SCREEN_RATIO) {
        return Container();
      } 

      return ErrorWidget(details.exception);
    },
  );
};