set_state_2_test.dart 1.48 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8

void main() {
9
  testWidgets('setState() overbuild test', (WidgetTester tester) async {
10
    final List<String> log = <String>[];
11
    final Builder inner = Builder(
12 13
      builder: (BuildContext context) {
        log.add('inner');
Ian Hickson's avatar
Ian Hickson committed
14
        return const Text('inner', textDirection: TextDirection.ltr);
15
      },
16 17
    );
    int value = 0;
18
    await tester.pumpWidget(Builder(
19 20
      builder: (BuildContext context) {
        log.add('outer');
21
        return StatefulBuilder(
22 23
          builder: (BuildContext context, StateSetter setState) {
            log.add('stateful');
24
            return GestureDetector(
25 26 27 28 29
              onTap: () {
                setState(() {
                  value += 1;
                });
              },
30
              child: Builder(
31 32 33
                builder: (BuildContext context) {
                  log.add('middle $value');
                  return inner;
34
                },
35
              ),
36
            );
37
          },
38
        );
39
      },
40 41
    ));
    log.add('---');
pq's avatar
pq committed
42
    await tester.tap(find.text('inner'));
43
    await tester.pump();
44 45 46 47 48 49 50 51 52 53 54
    log.add('---');
    expect(log, equals(<String>[
      'outer',
      'stateful',
      'middle 0',
      'inner',
      '---',
      'stateful',
      'middle 1',
      '---',
    ]));
55 56
  });
}