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

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

void main() {
  testWidgets('no overlap with floating action button', (WidgetTester tester) async {
    await tester.pumpWidget(
12 13
      const MaterialApp(
        home: Scaffold(
14
          floatingActionButton: FloatingActionButton(
15 16
            onPressed: null,
          ),
17 18 19
          bottomNavigationBar: ShapeListener(
            BottomAppBar(
              child: SizedBox(height: 100.0),
20
            ),
21
          ),
22 23 24 25 26 27
        ),
      ),
    );

    final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener));
    final RenderBox renderBox = tester.renderObject(find.byType(BottomAppBar));
28
    final Path expectedPath = Path()
29 30 31 32 33 34 35 36
      ..addRect(Offset.zero & renderBox.size);

    final Path actualPath = shapeListenerState.cache.value;
    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & renderBox.size).inflate(5.0),
37
      ),
38 39
    );
  });
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  testWidgets('custom shape', (WidgetTester tester) async {
    final Key key = UniqueKey();
    Future<void> pump(FloatingActionButtonLocation location) async {
      await tester.pumpWidget(
        SizedBox(
          width: 200,
          height: 200,
          child: RepaintBoundary(
            key: key,
            child: MaterialApp(
              home: Scaffold(
                floatingActionButton: FloatingActionButton(
                  onPressed: () { },
                ),
                floatingActionButtonLocation: location,
                bottomNavigationBar: BottomAppBar(
                  shape: AutomaticNotchedShape(
                    BeveledRectangleBorder(borderRadius: BorderRadius.circular(50.0)),
59
                    ContinuousRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
60 61 62 63 64 65 66 67 68 69 70 71 72 73
                  ),
                  notchMargin: 10.0,
                  color: Colors.green,
                  child: const SizedBox(height: 100.0),
                ),
              ),
            ),
          ),
        ),
      );
    }
    await pump(FloatingActionButtonLocation.endDocked);
    await expectLater(
      find.byKey(key),
74
      matchesGoldenFile('bottom_app_bar.custom_shape.1.png'),
75 76 77 78 79
    );
    await pump(FloatingActionButtonLocation.centerDocked);
    await tester.pumpAndSettle();
    await expectLater(
      find.byKey(key),
80
      matchesGoldenFile('bottom_app_bar.custom_shape.2.png'),
81
    );
82 83
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/51675,
  // https://github.com/flutter/flutter/issues/44572
84

85 86
  testWidgets('color defaults to Theme.bottomAppBarColor', (WidgetTester tester) async {
    await tester.pumpWidget(
87 88
      MaterialApp(
        home: Builder(
89
          builder: (BuildContext context) {
90
            return Theme(
91
              data: Theme.of(context)!.copyWith(bottomAppBarColor: const Color(0xffffff00)),
92
              child: const Scaffold(
93
                floatingActionButton: FloatingActionButton(
94 95
                  onPressed: null,
                ),
96
                bottomNavigationBar: BottomAppBar(),
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
              ),
            );
          }
        ),
      ),
    );

    final PhysicalShape physicalShape =
      tester.widget(find.byType(PhysicalShape).at(0));

    expect(physicalShape.color, const Color(0xffffff00));
  });

  testWidgets('color overrides theme color', (WidgetTester tester) async {
    await tester.pumpWidget(
112 113
      MaterialApp(
        home: Builder(
114
          builder: (BuildContext context) {
115
            return Theme(
116
              data: Theme.of(context)!.copyWith(bottomAppBarColor: const Color(0xffffff00)),
117
              child: const Scaffold(
118
                floatingActionButton: FloatingActionButton(
119 120
                  onPressed: null,
                ),
121 122
                bottomNavigationBar: BottomAppBar(
                  color: Color(0xff0000ff)
123 124 125 126 127 128 129 130 131 132 133 134 135 136
                ),
              ),
            );
          }
        ),
      ),
    );

    final PhysicalShape physicalShape =
      tester.widget(find.byType(PhysicalShape).at(0));

    expect(physicalShape.color, const Color(0xff0000ff));
  });

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  testWidgets('dark theme applies an elevation overlay color', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData.from(colorScheme: const ColorScheme.dark()),
        home: Scaffold(
          bottomNavigationBar: BottomAppBar(
            color: const ColorScheme.dark().surface,
          ),
        ),
      )
    );

    final PhysicalShape physicalShape = tester.widget(find.byType(PhysicalShape).at(0));

    // For the default dark theme the overlay color for elevation 8 is 0xFF2D2D2D
    expect(physicalShape.color, const Color(0xFF2D2D2D));
  });

155 156
  // This is a regression test for a bug we had where toggling the notch on/off
  // would crash, as the shouldReclip method of ShapeBorderClipper or
157
  // _BottomAppBarClipper would try an illegal downcast.
158
  testWidgets('toggle shape to null', (WidgetTester tester) async {
159
    await tester.pumpWidget(
160 161
      const MaterialApp(
        home: Scaffold(
162 163
          bottomNavigationBar: BottomAppBar(
            shape: RectangularNotch(),
164 165 166 167 168 169
          ),
        ),
      ),
    );

    await tester.pumpWidget(
170 171
      const MaterialApp(
        home: Scaffold(
172
          bottomNavigationBar: BottomAppBar(
173
            shape: null,
174 175 176 177 178 179
          ),
        ),
      ),
    );

    await tester.pumpWidget(
180 181
      const MaterialApp(
        home: Scaffold(
182 183
          bottomNavigationBar: BottomAppBar(
            shape: RectangularNotch(),
184 185 186 187 188
          ),
        ),
      ),
    );
  });
189 190 191

  testWidgets('no notch when notch param is null', (WidgetTester tester) async {
    await tester.pumpWidget(
192 193
      const MaterialApp(
        home: Scaffold(
194
          bottomNavigationBar: ShapeListener(BottomAppBar(
195 196
            shape: null,
          )),
197
          floatingActionButton: FloatingActionButton(
198
            onPressed: null,
199
            child: Icon(Icons.add),
200 201 202 203 204 205 206 207
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        ),
      ),
    );

    final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener));
    final RenderBox renderBox = tester.renderObject(find.byType(BottomAppBar));
208
    final Path expectedPath = Path()
209 210 211 212 213 214 215 216 217
      ..addRect(Offset.zero & renderBox.size);

    final Path actualPath = shapeListenerState.cache.value;

    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & renderBox.size).inflate(5.0),
218
      ),
219 220 221 222 223
    );
  });

  testWidgets('notch no margin', (WidgetTester tester) async {
    await tester.pumpWidget(
224 225
      const MaterialApp(
        home: Scaffold(
226 227 228 229
          bottomNavigationBar: ShapeListener(
            BottomAppBar(
              child: SizedBox(height: 100.0),
              shape: RectangularNotch(),
230
              notchMargin: 0.0,
231
            ),
232
          ),
233
          floatingActionButton: FloatingActionButton(
234
            onPressed: null,
235
            child: Icon(Icons.add),
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        ),
      ),
    );

    final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener));
    final RenderBox babBox = tester.renderObject(find.byType(BottomAppBar));
    final Size babSize = babBox.size;
    final RenderBox fabBox = tester.renderObject(find.byType(FloatingActionButton));
    final Size fabSize = fabBox.size;

    final double fabLeft = (babSize.width / 2.0) - (fabSize.width / 2.0);
    final double fabRight = fabLeft + fabSize.width;
    final double fabBottom = fabSize.height / 2.0;

252
    final Path expectedPath = Path()
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      ..moveTo(0.0, 0.0)
      ..lineTo(fabLeft, 0.0)
      ..lineTo(fabLeft, fabBottom)
      ..lineTo(fabRight, fabBottom)
      ..lineTo(fabRight, 0.0)
      ..lineTo(babSize.width, 0.0)
      ..lineTo(babSize.width, babSize.height)
      ..lineTo(0.0, babSize.height)
      ..close();

    final Path actualPath = shapeListenerState.cache.value;

    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & babSize).inflate(5.0),
270
      ),
271 272 273 274 275
    );
  });

  testWidgets('notch with margin', (WidgetTester tester) async {
    await tester.pumpWidget(
276 277
      const MaterialApp(
        home: Scaffold(
278 279 280 281
          bottomNavigationBar: ShapeListener(
            BottomAppBar(
              child: SizedBox(height: 100.0),
              shape: RectangularNotch(),
282
              notchMargin: 6.0,
283
            ),
284
          ),
285
          floatingActionButton: FloatingActionButton(
286
            onPressed: null,
287
            child: Icon(Icons.add),
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        ),
      ),
    );

    final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener));
    final RenderBox babBox = tester.renderObject(find.byType(BottomAppBar));
    final Size babSize = babBox.size;
    final RenderBox fabBox = tester.renderObject(find.byType(FloatingActionButton));
    final Size fabSize = fabBox.size;

    final double fabLeft = (babSize.width / 2.0) - (fabSize.width / 2.0) - 6.0;
    final double fabRight = fabLeft + fabSize.width + 6.0;
    final double fabBottom = 6.0 + fabSize.height / 2.0;

304
    final Path expectedPath = Path()
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
      ..moveTo(0.0, 0.0)
      ..lineTo(fabLeft, 0.0)
      ..lineTo(fabLeft, fabBottom)
      ..lineTo(fabRight, fabBottom)
      ..lineTo(fabRight, 0.0)
      ..lineTo(babSize.width, 0.0)
      ..lineTo(babSize.width, babSize.height)
      ..lineTo(0.0, babSize.height)
      ..close();

    final Path actualPath = shapeListenerState.cache.value;

    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & babSize).inflate(5.0),
322
      ),
323 324
    );
  });
325 326 327

  testWidgets('observes safe area', (WidgetTester tester) async {
    await tester.pumpWidget(
328 329
      const MaterialApp(
        home: MediaQuery(
330 331
          data: MediaQueryData(
            padding: EdgeInsets.all(50.0),
332
          ),
333 334 335 336
          child: Scaffold(
            bottomNavigationBar: BottomAppBar(
              child: Center(
                child: Text('safe'),
337 338 339 340 341 342 343 344 345 346 347 348
              ),
            ),
          ),
        ),
      ),
    );

    expect(
      tester.getBottomLeft(find.widgetWithText(Center, 'safe')),
      const Offset(50.0, 550.0),
    );
  });
349 350 351

  testWidgets('clipBehavior is propagated', (WidgetTester tester) async {
    await tester.pumpWidget(
352 353
      const MaterialApp(
        home: Scaffold(
354 355 356 357 358 359 360 361 362 363 364 365 366 367
          bottomNavigationBar:
              BottomAppBar(
                child: SizedBox(height: 100.0),
                shape: RectangularNotch(),
                notchMargin: 0.0,
              ),
        ),
      ),
    );

    PhysicalShape physicalShape = tester.widget(find.byType(PhysicalShape));
    expect(physicalShape.clipBehavior, Clip.none);

    await tester.pumpWidget(
368 369
      const MaterialApp(
        home: Scaffold(
370 371 372 373 374 375 376 377 378 379 380 381 382 383
          bottomNavigationBar:
          BottomAppBar(
            child: SizedBox(height: 100.0),
            shape: RectangularNotch(),
            notchMargin: 0.0,
            clipBehavior: Clip.antiAliasWithSaveLayer,
          ),
        ),
      ),
    );

    physicalShape = tester.widget(find.byType(PhysicalShape));
    expect(physicalShape.clipBehavior, Clip.antiAliasWithSaveLayer);
  });
384 385 386 387
}

// The bottom app bar clip path computation is only available at paint time.
// In order to examine the notch path we implement this caching painter which
388
// at paint time looks for a descendant PhysicalShape and caches the
389 390 391 392
// clip path it is using.
class ClipCachePainter extends CustomPainter {
  ClipCachePainter(this.context);

393
  late Path value;
394 395 396 397
  BuildContext context;

  @override
  void paint(Canvas canvas, Size size) {
398 399
    final RenderPhysicalShape physicalShape = findPhysicalShapeChild(context)!;
    value = physicalShape.clipper!.getClip(size);
400 401
  }

402 403
  RenderPhysicalShape? findPhysicalShapeChild(BuildContext context) {
    RenderPhysicalShape? result;
404
    context.visitChildElements((Element e) {
405
      final RenderObject renderObject = e.findRenderObject()!;
406 407
      if (renderObject.runtimeType == RenderPhysicalShape) {
        assert(result == null);
408
        result = renderObject as RenderPhysicalShape;
409 410 411 412 413 414 415 416 417 418 419 420 421 422
      } else {
        result = findPhysicalShapeChild(e);
      }
    });
    return result;
  }

  @override
  bool shouldRepaint(ClipCachePainter oldDelegate) {
    return true;
  }
}

class ShapeListener extends StatefulWidget {
423
  const ShapeListener(this.child, { Key? key }) : super(key: key);
424 425 426 427

  final Widget child;

  @override
428
  State createState() => ShapeListenerState();
429 430 431 432 433 434

}

class ShapeListenerState extends State<ShapeListener> {
  @override
  Widget build(BuildContext context) {
435
    return CustomPaint(
436
      child: widget.child,
437
      painter: cache,
438 439 440
    );
  }

441
  late ClipCachePainter cache;
442 443 444 445

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
446
    cache = ClipCachePainter(context);
447 448 449
  }

}
450

451
class RectangularNotch extends NotchedShape {
452 453 454
  const RectangularNotch();

  @override
455
  Path getOuterPath(Rect host, Rect? guest) {
456 457
    if (guest == null)
      return Path()..addRect(host);
458
    return Path()
459 460 461 462 463 464 465 466 467 468 469
      ..moveTo(host.left, host.top)
      ..lineTo(guest.left, host.top)
      ..lineTo(guest.left, guest.bottom)
      ..lineTo(guest.right, guest.bottom)
      ..lineTo(guest.right, host.top)
      ..lineTo(host.right, host.top)
      ..lineTo(host.right, host.bottom)
      ..lineTo(host.left, host.bottom)
      ..close();
  }
}