page_route_builder_test.dart 2.52 KB
Newer Older
1 2 3 4
// 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.

5 6 7 8
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])

9 10 11 12
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

class TestPage extends StatelessWidget {
13 14
  const TestPage({Key? key}) : super(key: key);

15 16 17 18 19 20 21
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
22
      home: const HomePage(),
23 24 25 26 27
    );
  }
}

class HomePage extends StatefulWidget {
28 29
  const HomePage({Key? key}) : super(key: key);

30 31 32 33 34 35
  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  void _presentModalPage() {
36
    Navigator.of(context).push(PageRouteBuilder<void>(
37 38 39
      barrierColor: Colors.black54,
      opaque: false,
      pageBuilder: (BuildContext context, _, __) {
40
        return const ModalPage();
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
      },
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: const Center(
        child: Text('Test Home'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _presentModalPage,
        child: const Icon(Icons.add),
      ),
    );
  }
}

class ModalPage extends StatelessWidget {
60 61
  const ModalPage({Key? key}) : super(key: key);

62 63 64 65 66 67 68 69 70 71 72
  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: SafeArea(
        child: Stack(
          children: <Widget>[
            InkWell(
              highlightColor: Colors.transparent,
              splashColor: Colors.transparent,
              onTap: () {
73
                Navigator.of(context).pop();
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
              },
              child: const SizedBox.expand(),
            ),
            Align(
              alignment: Alignment.bottomCenter,
              child: Container(
                height: 150,
                color: Colors.teal,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  testWidgets('Barriers show when using PageRouteBuilder', (WidgetTester tester) async {
93
    await tester.pumpWidget(const TestPage());
94 95 96 97 98 99 100 101
    await tester.tap(find.byType(FloatingActionButton));
    await tester.pumpAndSettle();
    await expectLater(
      find.byType(TestPage),
      matchesGoldenFile('page_route_builder.barrier.png'),
    );
  });
}