persistent_bottom_sheet_test.dart 18.1 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/material.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter_test/flutter_test.dart';
8 9

void main() {
10
  // Pumps and ensures that the BottomSheet animates non-linearly.
11
  Future<void> checkNonLinearAnimation(WidgetTester tester) async {
12 13 14 15 16 17 18 19 20 21
    final Offset firstPosition = tester.getCenter(find.text('One'));
    await tester.pump(const Duration(milliseconds: 30));
    final Offset secondPosition = tester.getCenter(find.text('One'));
    await tester.pump(const Duration(milliseconds: 30));
    final Offset thirdPosition = tester.getCenter(find.text('One'));

    final double dyDelta1 = secondPosition.dy - firstPosition.dy;
    final double dyDelta2 = thirdPosition.dy - secondPosition.dy;

    // If the animation were linear, these two values would be the same.
22
    expect(dyDelta1, isNot(moreOrLessEquals(dyDelta2, epsilon: 0.1)));
23 24
  }

25
  testWidgets('Verify that a BottomSheet can be rebuilt with ScaffoldFeatureController.setState()', (WidgetTester tester) async {
26
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
27
    int buildCount = 0;
28

29 30
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
31
        key: scaffoldKey,
32 33
        body: const Center(child: Text('body')),
      ),
34
    ));
35

36
    final PersistentBottomSheetController<void> bottomSheet = scaffoldKey.currentState!.showBottomSheet<void>((_) {
37
      return Builder(
38 39
        builder: (BuildContext context) {
          buildCount += 1;
40
          return Container(height: 200.0);
41
        },
42 43
      );
    });
44

45
    await tester.pump();
46
    expect(buildCount, equals(1));
47
    bottomSheet.setState!(() { });
48
    await tester.pump();
49
    expect(buildCount, equals(2));
50 51
  });

52 53 54 55 56 57 58 59 60 61
  testWidgets('Verify that a persistent BottomSheet cannot be dismissed', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: const Center(child: Text('body')),
        bottomSheet: DraggableScrollableSheet(
          expand: false,
          builder: (_, ScrollController controller) {
            return ListView(
              controller: controller,
              shrinkWrap: true,
62 63 64 65
              children: const <Widget>[
                SizedBox(height: 100.0, child: Text('One')),
                SizedBox(height: 100.0, child: Text('Two')),
                SizedBox(height: 100.0, child: Text('Three')),
66 67 68 69
              ],
            );
          },
        ),
70
      ),
71 72 73 74 75 76 77 78 79 80 81 82
    ));

    await tester.pumpAndSettle();

    expect(find.text('Two'), findsOneWidget);

    await tester.drag(find.text('Two'), const Offset(0.0, 400.0));
    await tester.pumpAndSettle();

    expect(find.text('Two'), findsOneWidget);
  });

83
  testWidgets('Verify that a scrollable BottomSheet can be dismissed', (WidgetTester tester) async {
84
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
85

86 87
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
88
        key: scaffoldKey,
89 90
        body: const Center(child: Text('body')),
      ),
91 92
    ));

93
    scaffoldKey.currentState!.showBottomSheet<void>((BuildContext context) {
94
      return ListView(
95
        shrinkWrap: true,
96
        primary: false,
97 98 99 100
        children: const <Widget>[
          SizedBox(height: 100.0, child: Text('One')),
          SizedBox(height: 100.0, child: Text('Two')),
          SizedBox(height: 100.0, child: Text('Three')),
101 102 103 104
        ],
      );
    });

105
    await tester.pumpAndSettle();
106 107 108

    expect(find.text('Two'), findsOneWidget);

109 110
    await tester.drag(find.text('Two'), const Offset(0.0, 400.0));
    await tester.pumpAndSettle();
111 112 113 114

    expect(find.text('Two'), findsNothing);
  });

115 116 117 118 119 120 121 122 123 124
  testWidgets('Verify that a BottomSheet animates non-linearly', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        key: scaffoldKey,
        body: const Center(child: Text('body')),
      ),
    ));

125
    scaffoldKey.currentState!.showBottomSheet<void>((BuildContext context) {
126 127 128
      return ListView(
        shrinkWrap: true,
        primary: false,
129 130 131 132
        children: const <Widget>[
          SizedBox(height: 100.0, child: Text('One')),
          SizedBox(height: 100.0, child: Text('Two')),
          SizedBox(height: 100.0, child: Text('Three')),
133 134 135 136
        ],
      );
    });
    await tester.pump();
137
    await checkNonLinearAnimation(tester);
138 139 140 141 142 143

    await tester.pumpAndSettle();

    expect(find.text('Two'), findsOneWidget);

    await tester.drag(find.text('Two'), const Offset(0.0, 200.0));
144
    await checkNonLinearAnimation(tester);
145 146 147 148 149
    await tester.pumpAndSettle();

    expect(find.text('Two'), findsNothing);
  });

150 151 152 153 154 155
  testWidgets('Verify that a scrollControlled BottomSheet can be dismissed', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        key: scaffoldKey,
156
        body: const Center(child: Text('body')),
157
      ),
158 159
    ));

160
    scaffoldKey.currentState!.showBottomSheet<void>(
161 162 163 164 165 166 167
      (BuildContext context) {
        return DraggableScrollableSheet(
          expand: false,
          builder: (_, ScrollController controller) {
            return ListView(
              shrinkWrap: true,
              controller: controller,
168 169 170 171
              children: const <Widget>[
                SizedBox(height: 100.0, child: Text('One')),
                SizedBox(height: 100.0, child: Text('Two')),
                SizedBox(height: 100.0, child: Text('Three')),
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
              ],
            );
          },
        );
      },
    );

    await tester.pumpAndSettle();

    expect(find.text('Two'), findsOneWidget);

    await tester.drag(find.text('Two'), const Offset(0.0, 400.0));
    await tester.pumpAndSettle();

    expect(find.text('Two'), findsNothing);
  });

  testWidgets('Verify that a persistent BottomSheet can fling up and hide the fab', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(),
          body: const Center(child: Text('body')),
          bottomSheet: DraggableScrollableSheet(
            expand: false,
            builder: (_, ScrollController controller) {
              return ListView.builder(
                itemExtent: 50.0,
                itemCount: 50,
                itemBuilder: (_, int index) => Text('Item $index'),
                controller: controller,
              );
            },
          ),
          floatingActionButton: const FloatingActionButton(
            onPressed: null,
            child: Text('fab'),
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();

    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 22'), findsNothing);
    expect(find.byType(FloatingActionButton), findsOneWidget);
    expect(find.byType(FloatingActionButton).hitTestable(), findsOneWidget);
    expect(find.byType(BackButton).hitTestable(), findsNothing);

    await tester.drag(find.text('Item 2'), const Offset(0, -20.0));
    await tester.pumpAndSettle();

    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 22'), findsNothing);
    expect(find.byType(FloatingActionButton), findsOneWidget);
    expect(find.byType(FloatingActionButton).hitTestable(), findsOneWidget);

    await tester.fling(find.text('Item 2'), const Offset(0.0, -600.0), 2000.0);
    await tester.pumpAndSettle();

    expect(find.text('Item 2'), findsNothing);
    expect(find.text('Item 22'), findsOneWidget);
    expect(find.byType(FloatingActionButton), findsOneWidget);
    expect(find.byType(FloatingActionButton).hitTestable(), findsNothing);
  });

  testWidgets('Verify that a back button resets a persistent BottomSheet', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(),
          body: const Center(child: Text('body')),
          bottomSheet: DraggableScrollableSheet(
            expand: false,
            builder: (_, ScrollController controller) {
              return ListView.builder(
                itemExtent: 50.0,
                itemCount: 50,
                itemBuilder: (_, int index) => Text('Item $index'),
                controller: controller,
              );
            },
          ),
          floatingActionButton: const FloatingActionButton(
            onPressed: null,
            child: Text('fab'),
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();

    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 22'), findsNothing);
    expect(find.byType(BackButton).hitTestable(), findsNothing);

    await tester.drag(find.text('Item 2'), const Offset(0, -20.0));
    await tester.pumpAndSettle();

    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 22'), findsNothing);
    // We've started to drag up, we should have a back button now for a11y
    expect(find.byType(BackButton).hitTestable(), findsOneWidget);

    await tester.tap(find.byType(BackButton));
    await tester.pumpAndSettle();

    expect(find.byType(BackButton).hitTestable(), findsNothing);
    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 22'), findsNothing);

    await tester.fling(find.text('Item 2'), const Offset(0.0, -600.0), 2000.0);
    await tester.pumpAndSettle();

    expect(find.text('Item 2'), findsNothing);
    expect(find.text('Item 22'), findsOneWidget);
    expect(find.byType(BackButton).hitTestable(), findsOneWidget);

    await tester.tap(find.byType(BackButton));
    await tester.pumpAndSettle();

    expect(find.byType(BackButton).hitTestable(), findsNothing);
    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 22'), findsNothing);
  });

  testWidgets('Verify that a scrollable BottomSheet hides the fab when scrolled up', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        key: scaffoldKey,
        body: const Center(child: Text('body')),
        floatingActionButton: const FloatingActionButton(
          onPressed: null,
          child: Text('fab'),
        ),
311
      ),
312 313
    ));

314
    scaffoldKey.currentState!.showBottomSheet<void>(
315 316 317 318 319 320 321
      (BuildContext context) {
        return DraggableScrollableSheet(
          expand: false,
          builder: (_, ScrollController controller) {
            return ListView(
              controller: controller,
              shrinkWrap: true,
322 323 324 325 326 327 328 329 330 331 332 333
              children: const <Widget>[
                SizedBox(height: 100.0, child: Text('One')),
                SizedBox(height: 100.0, child: Text('Two')),
                SizedBox(height: 100.0, child: Text('Three')),
                SizedBox(height: 100.0, child: Text('Three')),
                SizedBox(height: 100.0, child: Text('Three')),
                SizedBox(height: 100.0, child: Text('Three')),
                SizedBox(height: 100.0, child: Text('Three')),
                SizedBox(height: 100.0, child: Text('Three')),
                SizedBox(height: 100.0, child: Text('Three')),
                SizedBox(height: 100.0, child: Text('Three')),
                SizedBox(height: 100.0, child: Text('Three')),
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
              ],
            );
          },
        );
      },
    );

    await tester.pumpAndSettle();

    expect(find.text('Two'), findsOneWidget);
    expect(find.byType(FloatingActionButton).hitTestable(), findsOneWidget);

    await tester.drag(find.text('Two'), const Offset(0.0, -600.0));
    await tester.pumpAndSettle();

    expect(find.text('Two'), findsOneWidget);
    expect(find.byType(FloatingActionButton), findsOneWidget);
    expect(find.byType(FloatingActionButton).hitTestable(), findsNothing);
  });

354
  testWidgets('showBottomSheet()', (WidgetTester tester) async {
355 356 357 358
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Placeholder(key: key),
359
      ),
360 361 362
    ));

    int buildCount = 0;
363
    showBottomSheet<void>(
364
      context: key.currentContext!,
365
      builder: (BuildContext context) {
366
        return Builder(
367 368
          builder: (BuildContext context) {
            buildCount += 1;
369
            return Container(height: 200.0);
370
          },
371 372 373 374 375 376 377
        );
      },
    );
    await tester.pump();
    expect(buildCount, equals(1));
  });

378
  testWidgets('Scaffold removes top MediaQuery padding', (WidgetTester tester) async {
379 380
    late BuildContext scaffoldContext;
    late BuildContext bottomSheetContext;
381

382 383
    await tester.pumpWidget(MaterialApp(
      home: MediaQuery(
384
        data: const MediaQueryData(
385
          padding: EdgeInsets.all(50.0),
386
        ),
387
        child: Scaffold(
388
          resizeToAvoidBottomInset: false,
389
          body: Builder(
390 391
            builder: (BuildContext context) {
              scaffoldContext = context;
392
              return Container();
393
            },
394 395
          ),
        ),
396
      ),
397 398 399 400
    ));

    await tester.pump();

401
    showBottomSheet<void>(
402 403 404
      context: scaffoldContext,
      builder: (BuildContext context) {
        bottomSheetContext = context;
405
        return Container();
406 407 408 409 410 411
      },
    );

    await tester.pump();

    expect(
412
      MediaQuery.of(bottomSheetContext).padding,
413 414 415 416 417 418 419
      const EdgeInsets.only(
        bottom: 50.0,
        left: 50.0,
        right: 50.0,
      ),
    );
  });
420 421

  testWidgets('Scaffold.bottomSheet', (WidgetTester tester) async {
422
    final Key bottomSheetKey = UniqueKey();
423 424

    await tester.pumpWidget(
425 426
      MaterialApp(
        home: Scaffold(
427
          body: const Placeholder(),
428
          bottomSheet: Container(
429 430 431
            key: bottomSheetKey,
            alignment: Alignment.center,
            height: 200.0,
432
            child: Builder(
433
              builder: (BuildContext context) {
434
                return ElevatedButton(
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
                  child: const Text('showModalBottomSheet'),
                  onPressed: () {
                    showModalBottomSheet<void>(
                      context: context,
                      builder: (BuildContext context) => const Text('modal bottom sheet'),
                    );
                  },
                );
              },
            ),
          ),
        ),
      ),
    );

    expect(find.text('showModalBottomSheet'), findsOneWidget);
    expect(tester.getSize(find.byKey(bottomSheetKey)), const Size(800.0, 200.0));
    expect(tester.getTopLeft(find.byKey(bottomSheetKey)), const Offset(0.0, 400.0));

    // Show the modal bottomSheet
    await tester.tap(find.text('showModalBottomSheet'));
    await tester.pumpAndSettle();
    expect(find.text('modal bottom sheet'), findsOneWidget);

459 460
    // Dismiss the modal bottomSheet by tapping above the sheet
    await tester.tapAt(const Offset(20.0, 20.0));
461 462 463 464 465 466
    await tester.pumpAndSettle();
    expect(find.text('modal bottom sheet'), findsNothing);
    expect(find.text('showModalBottomSheet'), findsOneWidget);

    // Remove the persistent bottomSheet
    await tester.pumpWidget(
467 468
      const MaterialApp(
        home: Scaffold(
469
          body: Placeholder(),
470 471 472 473 474 475 476
        ),
      ),
    );
    await tester.pumpAndSettle();
    expect(find.text('showModalBottomSheet'), findsNothing);
    expect(find.byKey(bottomSheetKey), findsNothing);
  });
477

478
  // Regression test for https://github.com/flutter/flutter/issues/71435
479 480 481 482 483 484 485 486 487 488 489 490
  testWidgets(
    'Scaffold.bottomSheet should be updated without creating a new RO'
    ' when the new widget has the same key and type.',
    (WidgetTester tester) async {
      Widget buildFrame(String text) {
        return MaterialApp(
          home: Scaffold(
            body: const Placeholder(),
            bottomSheet: Text(text),
          ),
        );
      }
491

492 493
      await tester.pumpWidget(buildFrame('I love Flutter!'));
      final RenderParagraph renderBeforeUpdate = tester.renderObject(find.text('I love Flutter!'));
494

495 496 497
      await tester.pumpWidget(buildFrame('Flutter is the best!'));
      await tester.pumpAndSettle();
      final RenderParagraph renderAfterUpdate = tester.renderObject(find.text('Flutter is the best!'));
498

499 500 501
      expect(renderBeforeUpdate, renderAfterUpdate);
    },
  );
502

503 504 505 506
  testWidgets('Verify that visual properties are passed through', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
    const Color color = Colors.pink;
    const double elevation = 9.0;
507
    const ShapeBorder shape = BeveledRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12)));
508
    const Clip clipBehavior = Clip.antiAlias;
509 510 511 512 513 514 515 516

    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        key: scaffoldKey,
        body: const Center(child: Text('body')),
      ),
    ));

517
    scaffoldKey.currentState!.showBottomSheet<void>((BuildContext context) {
518 519 520
      return ListView(
        shrinkWrap: true,
        primary: false,
521 522 523 524
        children: const <Widget>[
          SizedBox(height: 100.0, child: Text('One')),
          SizedBox(height: 100.0, child: Text('Two')),
          SizedBox(height: 100.0, child: Text('Three')),
525 526
        ],
      );
527
    }, backgroundColor: color, elevation: elevation, shape: shape, clipBehavior: clipBehavior);
528 529 530 531 532 533 534

    await tester.pumpAndSettle();

    final BottomSheet bottomSheet = tester.widget(find.byType(BottomSheet));
    expect(bottomSheet.backgroundColor, color);
    expect(bottomSheet.elevation, elevation);
    expect(bottomSheet.shape, shape);
535
    expect(bottomSheet.clipBehavior, clipBehavior);
536 537
  });

538 539 540 541 542
  testWidgets('PersistentBottomSheetController.close dismisses the bottom sheet', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        key: scaffoldKey,
543
        body: const Center(child: Text('body')),
544
      ),
545 546
    ));

547
    final PersistentBottomSheetController<void> bottomSheet = scaffoldKey.currentState!.showBottomSheet<void>((_) {
548 549 550
      return Builder(
        builder: (BuildContext context) {
          return Container(height: 200.0);
551
        },
552 553 554 555 556 557 558 559 560 561
      );
    });

    await tester.pump();
    expect(find.byType(BottomSheet), findsOneWidget);

    bottomSheet.close();
    await tester.pump();
    expect(find.byType(BottomSheet), findsNothing);
  });
562
}