toggle_buttons.1_test.dart 4.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 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 67 68 69 70 71 72 73 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
// Copyright 2014 The Flutter 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_api_samples/material/toggle_buttons/toggle_buttons.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('ToggleButtons allows multiple or no selection', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(useMaterial3: true);
    Finder findButton(String text) {
      return find.descendant(
        of: find.byType(ToggleButtons),
        matching: find.widgetWithText(TextButton, text),
      );
    }

    await tester.pumpWidget(const example.ToggleButtonsApp());

    TextButton toggleMButton = tester.widget<TextButton>(findButton('M'));
    TextButton toggleXLButton = tester.widget<TextButton>(findButton('XL'));

    // Initially, only M is selected.
    expect(
      toggleMButton.style!.backgroundColor!.resolve(enabled),
      theme.colorScheme.primary.withOpacity(0.12),
    );
    expect(
      toggleXLButton.style!.backgroundColor!.resolve(enabled),
      theme.colorScheme.surface.withOpacity(0.0),
    );

    // Tap on XL.
    await tester.tap(findButton('XL'));
    await tester.pumpAndSettle();

    // Now both M and XL are selected.
    toggleMButton = tester.widget<TextButton>(findButton('M'));
    toggleXLButton = tester.widget<TextButton>(findButton('XL'));

    expect(
      toggleMButton.style!.backgroundColor!.resolve(enabled),
      theme.colorScheme.primary.withOpacity(0.12),
    );
    expect(
      toggleXLButton.style!.backgroundColor!.resolve(enabled),
      theme.colorScheme.primary.withOpacity(0.12),
    );

    // Tap M to deselect it.
    await tester.tap(findButton('M'));
    await tester.pumpAndSettle();

    // Tap XL to deselect it.
    await tester.tap(findButton('XL'));
    await tester.pumpAndSettle();

    // Now neither M nor XL are selected.
    toggleMButton = tester.widget<TextButton>(findButton('M'));
    toggleXLButton = tester.widget<TextButton>(findButton('XL'));

    expect(
      toggleMButton.style!.backgroundColor!.resolve(enabled),
      theme.colorScheme.surface.withOpacity(0.0),
    );
    expect(
      toggleXLButton.style!.backgroundColor!.resolve(enabled),
      theme.colorScheme.surface.withOpacity(0.0),
    );
  });

  testWidgets('SegmentedButton allows multiple or no selection', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(useMaterial3: true);
    Finder findButton(String text) {
      return find.descendant(
        of: find.byType(SegmentedButton<example.ShirtSize>),
        matching: find.widgetWithText(TextButton, text),
      );
    }

    await tester.pumpWidget(const example.ToggleButtonsApp());

    Material segmentMButton = tester.widget<Material>(find.descendant(
      of: findButton('M'),
      matching: find.byType(Material),
    ));
    Material segmentXLButton = tester.widget<Material>(find.descendant(
      of: findButton('XL'),
      matching: find.byType(Material),
    ));

    // Initially, only M is selected.
    expect(segmentMButton.color, theme.colorScheme.secondaryContainer);
    expect(segmentXLButton.color, Colors.transparent);

    // Tap on XL.
    await tester.tap(findButton('XL'));
    await tester.pumpAndSettle();

    // // Now both M and XL are selected.
    segmentMButton = tester.widget<Material>(find.descendant(
      of: findButton('M'),
      matching: find.byType(Material),
    ));
    segmentXLButton = tester.widget<Material>(find.descendant(
      of: findButton('XL'),
      matching: find.byType(Material),
    ));

    expect(segmentMButton.color, theme.colorScheme.secondaryContainer);
    expect(segmentXLButton.color, theme.colorScheme.secondaryContainer);

    // Tap M to deselect it.
    await tester.tap(findButton('M'));
    await tester.pumpAndSettle();

    // Tap XL to deselect it.
    await tester.tap(findButton('XL'));
    await tester.pumpAndSettle();

    // Now neither M nor XL are selected.
    segmentMButton = tester.widget<Material>(find.descendant(
      of: findButton('M'),
      matching: find.byType(Material),
    ));
    segmentXLButton = tester.widget<Material>(find.descendant(
      of: findButton('XL'),
      matching: find.byType(Material),
    ));

    expect(segmentMButton.color, Colors.transparent);
    expect(segmentXLButton.color, Colors.transparent);
  });
}

Set<MaterialState> enabled = <MaterialState>{ };