import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/adaptive/floating_bar_layout.dart'; import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart'; import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; import 'package:pipelines_app/ui/adaptive/top_bar_notices.dart'; /// Content floats behind the top bar: lists reach the top edge, start below /// the bar at rest and scroll behind it; pinned headers pin under it. void main() { Future setSize(WidgetTester tester, Size size) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); } Rect barRect(WidgetTester tester) => tester.getRect( find.descendant( of: find.byType(SegmentedBar), matching: find.byType(GlassPill), ), ); Widget page(Widget body) { final bar = SegmentedBar(title: 'Page'); return MaterialApp( home: Scaffold(extendBodyBehindAppBar: true, appBar: bar, body: body), ); } Widget rows() => Builder( builder: (context) => ListView( key: const Key('list'), padding: withTopBarClearance(context, const EdgeInsets.all(16)), children: [ for (var index = 0; index < 40; index++) SizedBox(height: 48, child: Text('row $index')), ], ), ); { const host = 'Material'; testWidgets('$host: a list reaches the top edge, starts below the bar at ' 'rest and scrolls behind it', (tester) async { await setSize(tester, const Size(390, 844)); await tester.pumpWidget(page(rows())); expect(tester.getTopLeft(find.byKey(const Key('list'))).dy, 0); final bar = barRect(tester); expect( tester.getTopLeft(find.text('row 0')).dy, greaterThanOrEqualTo(bar.bottom), ); await tester.drag(find.byKey(const Key('list')), const Offset(0, -120)); await tester.pump(); // A row now passes behind the glass title pill. final behind = find.byWidgetPredicate((widget) { if (widget is! Text || !(widget.data ?? '').startsWith('row ')) { return false; } return true; }); final rects = behind.evaluate().map( (element) => tester.getRect(find.byWidget(element.widget)), ); expect(rects.any((rect) => rect.overlaps(bar)), isTrue); }); } testWidgets('a pinned header after SliverTopBarClearance pins below the ' 'bar', (tester) async { await setSize(tester, const Size(390, 844)); await tester.pumpWidget( page( CustomScrollView( key: const Key('list'), slivers: [ const SliverTopBarClearance(), const SliverToBoxAdapter(child: SizedBox(height: 200)), const PinnedHeaderSliver( child: SizedBox(height: 40, child: Text('pinned')), ), SliverList.list( children: [ for (var index = 0; index < 40; index++) SizedBox(height: 48, child: Text('row $index')), ], ), ], ), ), ); final inset = barRect(tester).bottom; await tester.drag(find.byKey(const Key('list')), const Offset(0, -600)); await tester.pump(); // The header pins at the bar's inset, not behind the bar. expect( tester.getTopLeft(find.text('pinned')).dy, greaterThanOrEqualTo(inset), ); expect(tester.getTopLeft(find.text('pinned')).dy, lessThan(inset + 16)); }); testWidgets('BelowTopBar and TopBarNotices keep fixed content below the bar ' 'without remounting it', (tester) async { await setSize(tester, const Size(390, 844)); var notice = false; late StateSetter update; await tester.pumpWidget( page( StatefulBuilder( builder: (context, setState) { update = setState; return TopBarNotices( notices: [if (notice) const Text('Offline')], child: const _Probe(), ); }, ), ), ); final bar = barRect(tester); final probe = tester.state<_ProbeState>(find.byType(_Probe)); expect(probe.topPadding, greaterThanOrEqualTo(bar.bottom)); expect(tester.getTopLeft(find.byType(_Probe)).dy, 0); update(() => notice = true); await tester.pump(); expect(tester.state<_ProbeState>(find.byType(_Probe)), same(probe)); expect( tester.getTopLeft(find.text('Offline')).dy, greaterThanOrEqualTo(bar.bottom), ); expect(probe.topPadding, 0); expect( tester.getTopLeft(find.byType(_Probe)).dy, greaterThanOrEqualTo(tester.getBottomLeft(find.text('Offline')).dy), ); }); testWidgets('a top-edge tab bar sits under the inset and pads the content ' 'by both, without swelling over leftover padding', (tester) async { await setSize(tester, const Size(800, 600)); late double contentTop; await tester.pumpWidget( MaterialApp( home: MediaQuery( data: const MediaQueryData( size: Size(800, 600), padding: EdgeInsets.only(top: 64, bottom: 96), ), child: FloatingBarOverlay( edge: FloatingBarEdge.top, bar: FloatingDestinationBar( edge: FloatingBarEdge.top, currentIndex: 0, onTap: (_) {}, items: const [ FloatingDestinationItem(icon: Icon(Icons.notes), label: 'One'), FloatingDestinationItem( icon: Icon(Icons.timeline), label: 'Two', ), ], ), child: Builder( builder: (context) { contentTop = MediaQuery.paddingOf(context).top; return const SizedBox.expand(); }, ), ), ), ), ); final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey)); expect(pill.top, 64); // The pill is the bar's own height: leftover media padding is not added. expect(pill.height, FloatingDestinationBar.materialSelectedButtonSize); // Wide: sized to its tabs at the leading edge, under the title. expect(pill.left, FloatingDestinationBar.topMargin); expect(pill.width, lessThan(400)); expect(contentTop, pill.bottom + 8); }); } class _Probe extends StatefulWidget { const _Probe(); @override State<_Probe> createState() => _ProbeState(); } class _ProbeState extends State<_Probe> { double topPadding = -1; @override Widget build(BuildContext context) { topPadding = MediaQuery.paddingOf(context).top; return const SizedBox.expand(); } }