Skip to content

Make const work with LayoutBuilder Widget #145537

@SittiphanSittisak

Description

@SittiphanSittisak

Use case

I tried to use const with responsive widgets like using the LayoutBuilder widget or using if statement with MediaQuery.of(context).size.width but these const widgets still rebuild. I check it by using print in the Widget build(BuildContext context)
I think it is good to make const work for the responsive widget. Many widgets are used in many screen sizes.
For an example:

  @override
  Widget build(BuildContext context) {
    const testWidget = Test();
    return LayoutBuilder(builder: (context, constraints) {
      return constraints.maxWidth > 500 ? const Column(children: [testWidget]) : const Row(children: [testWidget]);
    });
class Test extends StatelessWidget {
  const Test({super.key});

  @override
  Widget build(BuildContext context) {
    print('rebuild');
    return const SizedBox();
  }
}

The console will print rebuild every time you change screen size between 500 units.

Proposal

Make the const widget work with LayoutBuilder or other responsive widgets.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions