Performance declaring widget in stateful widget's build

40 Views Asked by At

I saw this post on stack overflow

Performance difference between widget function and class

Where

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _counter = 0;

  Widget _buildNonsenseWidget() {
    return Container(
      child: Column(
        children: [
          Text('Hello'),
          Row(
            children: [
              Text('there'),
              Text('world!'),
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Text('Counter: $_counter'),

        // The deeply nesting widget is now refactored into a
        // separate method and we have a cleaner build method. Yay!
        _buildNonsenseWidget(),
      ],
    );
  }
}

Has an impact on performance because it can cause unneeded rebuilds. But I was wondering if I have

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _counter = 0;

  @override
  Widget build(BuildContext context) {
    final dummy = DummyWidget();

    return Row(
      children: [
        Text('Counter: $_counter'),
        dummy,
      ],
    );
  }
}

Would that have the same negative impact on performance (having those unneeded rebuilds)? Or would it be just a performant as

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _counter = 0;

  @override
  Widget build(BuildContext context) {    
    return Row(
      children: [
        Text('Counter: $_counter'),
        DummyWidget(),
      ],
    );
  }
}
0

There are 0 best solutions below