button_bar_test.dart 3.87 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9 10
  testWidgets('ButtonBar default control smoketest', (WidgetTester tester) async {
    await tester.pumpWidget(
11
      const Directionality(
12
        textDirection: TextDirection.ltr,
13
        child: ButtonBar(),
14 15
      ),
    );
16
  });
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

  testWidgets('ButtonBar has a min height of 52 when using ButtonBarLayoutBehavior.constrained', (WidgetTester tester) async {
    await tester.pumpWidget(
      SingleChildScrollView(
        child: ListBody(
          children: <Widget>[
            ButtonTheme.bar(
              layoutBehavior: ButtonBarLayoutBehavior.constrained,
              child: const Directionality(
                textDirection: TextDirection.ltr,
                child: ButtonBar(
                  children: <Widget>[
                    SizedBox(width: 10.0, height: 10.0),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );

    final Finder buttonBar = find.byType(ButtonBar);
    expect(tester.getBottomRight(buttonBar).dy - tester.getTopRight(buttonBar).dy, 52.0);
  });

  testWidgets('ButtonBar has padding applied when using ButtonBarLayoutBehavior.padded', (WidgetTester tester) async {
    await tester.pumpWidget(
      SingleChildScrollView(
        child: ListBody(
          children: <Widget>[
            ButtonTheme.bar(
              layoutBehavior: ButtonBarLayoutBehavior.padded,
              child: const Directionality(
                textDirection: TextDirection.ltr,
                child: ButtonBar(
                  children: <Widget>[
                    SizedBox(width: 10.0, height: 10.0),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );

    final Finder buttonBar = find.byType(ButtonBar);
    expect(tester.getBottomRight(buttonBar).dy - tester.getTopRight(buttonBar).dy, 26.0);
  });
67 68 69 70 71 72

  testWidgets('ButtonBar FlatButton inherits Theme accentColor', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/22789

    await tester.pumpWidget(
      MaterialApp(
73
        theme: ThemeData(accentColor: const Color(0x00000001)),
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        home: Builder(
          builder: (BuildContext context) {
            return Center(
              child: ButtonTheme.bar(
                child: ButtonBar(
                  children: <Widget>[
                    FlatButton(
                      child: const Text('button'),
                      onPressed: () {
                        showDialog<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return AlertDialog( // puts its actions in a ButtonBar
                              actions: <Widget>[
                                FlatButton(
                                  onPressed: () { },
                                  child: const Text('enabled'),
                                ),
                              ],
                            );
                          },
                        );
                      },
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );

107
    expect(tester.widget<RawMaterialButton>(find.byType(RawMaterialButton)).textStyle.color, const Color(0x00000001));
108 109 110 111 112 113 114 115 116

    // Show the dialog
    await tester.tap(find.text('button'));
    await tester.pumpAndSettle();

    final Finder dialogButton = find.ancestor(
      of: find.text('enabled'),
      matching: find.byType(RawMaterialButton),
    );
117
    expect(tester.widget<RawMaterialButton>(dialogButton).textStyle.color, const Color(0x00000001));
118
  });
119
}