bottom_app_bar_test.dart 14.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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/rendering.dart';
7
import 'package:flutter_test/flutter_test.dart';
8 9 10 11

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
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/44572
83

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

    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(
111 112
      MaterialApp(
        home: Builder(
113
          builder: (BuildContext context) {
114
            return Theme(
115
              data: Theme.of(context).copyWith(bottomAppBarColor: const Color(0xffffff00)),
116
              child: const Scaffold(
117
                floatingActionButton: FloatingActionButton(
118 119
                  onPressed: null,
                ),
120
                bottomNavigationBar: BottomAppBar(
121
                  color: Color(0xff0000ff),
122 123 124
                ),
              ),
            );
125
          },
126 127 128 129 130 131 132 133 134 135
        ),
      ),
    );

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

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

136 137 138 139 140 141 142 143 144
  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,
          ),
        ),
145
      ),
146 147 148 149 150 151 152 153
    );

    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));
  });

154 155
  // 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
156
  // _BottomAppBarClipper would try an illegal downcast.
157
  testWidgets('toggle shape to null', (WidgetTester tester) async {
158
    await tester.pumpWidget(
159 160
      const MaterialApp(
        home: Scaffold(
161 162
          bottomNavigationBar: BottomAppBar(
            shape: RectangularNotch(),
163 164 165 166 167 168
          ),
        ),
      ),
    );

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

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

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

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

    final Path actualPath = shapeListenerState.cache.value;

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

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

251
    final Path expectedPath = Path()
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      ..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),
269
      ),
270 271 272 273 274
    );
  });

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

303
    final Path expectedPath = Path()
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
      ..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),
321
      ),
322 323
    );
  });
324 325 326

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

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

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

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

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

    physicalShape = tester.widget(find.byType(PhysicalShape));
    expect(physicalShape.clipBehavior, Clip.antiAliasWithSaveLayer);
  });
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

  testWidgets('BottomAppBar with shape when Scaffold.bottomNavigationBar == null', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/80878
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
          floatingActionButton: FloatingActionButton(
            backgroundColor: Colors.green,
            child: const Icon(Icons.home),
            onPressed: () {},
          ),
          body: Stack(
            children: <Widget>[
              Container(
                color: Colors.amber,
              ),
              Container(
                alignment: Alignment.bottomCenter,
                child: BottomAppBar(
                  color: Colors.green,
                  shape: const CircularNotchedRectangle(),
                  child: Container(height: 50),
                ),
              ),
            ],
          ),
        ),
      ),
    );

    expect(tester.getRect(find.byType(FloatingActionButton)), const Rect.fromLTRB(372, 528, 428, 584));
    expect(tester.getSize(find.byType(BottomAppBar)), const Size(800, 50));
  });
417 418 419 420
}

// 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
421
// at paint time looks for a descendant PhysicalShape and caches the
422 423 424 425
// clip path it is using.
class ClipCachePainter extends CustomPainter {
  ClipCachePainter(this.context);

426
  late Path value;
427 428 429 430
  BuildContext context;

  @override
  void paint(Canvas canvas, Size size) {
431 432
    final RenderPhysicalShape physicalShape = findPhysicalShapeChild(context)!;
    value = physicalShape.clipper!.getClip(size);
433 434
  }

435 436
  RenderPhysicalShape? findPhysicalShapeChild(BuildContext context) {
    RenderPhysicalShape? result;
437
    context.visitChildElements((Element e) {
438
      final RenderObject renderObject = e.findRenderObject()!;
439 440
      if (renderObject.runtimeType == RenderPhysicalShape) {
        assert(result == null);
441
        result = renderObject as RenderPhysicalShape;
442 443 444 445 446 447 448 449 450 451 452 453 454 455
      } else {
        result = findPhysicalShapeChild(e);
      }
    });
    return result;
  }

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

class ShapeListener extends StatefulWidget {
456
  const ShapeListener(this.child, { Key? key }) : super(key: key);
457 458 459 460

  final Widget child;

  @override
461
  State createState() => ShapeListenerState();
462 463 464 465 466 467

}

class ShapeListenerState extends State<ShapeListener> {
  @override
  Widget build(BuildContext context) {
468
    return CustomPaint(
469
      painter: cache,
470
      child: widget.child,
471 472 473
    );
  }

474
  late ClipCachePainter cache;
475 476 477 478

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
479
    cache = ClipCachePainter(context);
480 481 482
  }

}
483

484
class RectangularNotch extends NotchedShape {
485 486 487
  const RectangularNotch();

  @override
488
  Path getOuterPath(Rect host, Rect? guest) {
489 490
    if (guest == null)
      return Path()..addRect(host);
491
    return Path()
492 493 494 495 496 497 498 499 500 501 502
      ..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();
  }
}