import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart'; import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; /// The floating tab bar is sized to its tabs at the bottom right on every /// width, and its selection circle keeps one gap everywhere: the first and /// last circles sit as far from the pill's sides as from its top and bottom, /// and neighbouring circles are that far apart too, on both hosts. void main() { const icons = [ Icons.account_tree_outlined, Icons.play_circle_outline, Icons.chat_bubble_outline, Icons.search, Icons.settings_outlined, ]; Key tabKey(int index) => ValueKey('tab-$index'); Widget bar({ required bool cupertino, required int count, required int selected, ValueChanged? onTap, }) => FloatingDestinationBar( cupertino: cupertino, currentIndex: selected, onTap: onTap ?? (_) {}, items: [ for (var index = 0; index < count; index += 1) FloatingDestinationItem( key: tabKey(index), icon: Icon(icons[index]), label: 'Tab $index', ), ], ); Future pump( WidgetTester tester, { required bool cupertino, required Size size, required Widget child, double textScale = 1, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final body = MediaQuery( data: MediaQueryData( size: size, textScaler: TextScaler.linear(textScale), ), child: child, ); await tester.pumpWidget( cupertino ? CupertinoApp(home: CupertinoPageScaffold(child: body)) : MaterialApp(home: Scaffold(body: body)), ); await tester.pumpAndSettle(); } Rect circle(WidgetTester tester, int index) => tester.getRect( find .descendant( of: find.byKey(tabKey(index)), matching: find.byWidgetPredicate( (widget) => widget is DecoratedBox && widget.decoration is ShapeDecoration && (widget.decoration as ShapeDecoration).shape is CircleBorder, ), ) .first, ); Rect pill(WidgetTester tester) => tester.getRect( find.descendant( of: find.byType(FloatingDestinationBar), matching: find.byType(GlassPill), ), ); final placements = [ (name: 'wide 1200, 4 tabs', size: const Size(1200, 800), count: 4), (name: 'phone 390, 3 tabs', size: const Size(390, 844), count: 3), (name: 'phone 390, 4 tabs', size: const Size(390, 844), count: 4), (name: 'phone 560, 5 tabs', size: const Size(560, 844), count: 5), (name: 'phone 320, 5 tabs', size: const Size(320, 640), count: 5), ]; for (final cupertino in [false, true]) { final host = cupertino ? 'Cupertino' : 'Material'; for (final placement in placements) { testWidgets('$host ${placement.name}: compact at the bottom right; the ' 'outer circles keep the top gap from the sides and neighbours', ( tester, ) async { final count = placement.count; final size = placement.size; final centres = []; for (final selected in [0, count - 1]) { await pump( tester, cupertino: cupertino, size: size, child: Align( alignment: Alignment.bottomCenter, child: bar( cupertino: cupertino, count: count, selected: selected, ), ), ); final box = pill(tester); expect(box.right, size.width - 16); expect(box.bottom, size.height - 16); final mark = circle(tester, selected); expect(mark.width, mark.height); final top = mark.top - box.top; expect(box.bottom - mark.bottom, moreOrLessEquals(top, epsilon: 1)); final side = selected == 0 ? mark.left - box.left : box.right - mark.right; expect( side, moreOrLessEquals(top, epsilon: 1), reason: 'side gap $side vs top gap $top', ); // The pill's round end is concentric with the outer circle. expect(box.height / 2, moreOrLessEquals(top + mark.width / 2)); } // Unselected icons sit where their circle would: neighbours are // evenly spaced, one edge gap apart. for (var index = 0; index < count; index += 1) { centres.add( tester .getCenter( find.descendant( of: find.byKey(tabKey(index)), matching: find.byIcon(icons[index]), ), ) .dx, ); } final last = circle(tester, count - 1); final gap = last.top - pill(tester).top; for (var index = 1; index < count; index += 1) { expect( centres[index] - centres[index - 1] - last.width, moreOrLessEquals(gap, epsilon: 1), ); } expect(last.center.dx, moreOrLessEquals(centres.last, epsilon: 1)); }); } testWidgets('$host phone: taps beside the compact bar reach the content, ' 'taps on it the tabs', (tester) async { final tapped = []; var content = 0; await pump( tester, cupertino: cupertino, size: const Size(390, 844), child: Stack( children: [ Positioned.fill( child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: () => content++, ), ), Align( alignment: Alignment.bottomCenter, child: bar( cupertino: cupertino, count: 3, selected: 1, onTap: tapped.add, ), ), ], ), ); final box = pill(tester); expect(box.width, lessThan(390 / 2)); await tester.tapAt(Offset(box.left - 40, box.center.dy)); await tester.tapAt(box.centerLeft + const Offset(12, 0)); await tester.tapAt(box.centerRight - const Offset(12, 0)); await tester.pump(); expect(content, 1); expect(tapped, [0, 2]); }); testWidgets('$host: 5 tabs at 320 with 2x text do not overflow', ( tester, ) async { await pump( tester, cupertino: cupertino, size: const Size(320, 640), textScale: 2, child: Align( alignment: Alignment.bottomCenter, child: bar(cupertino: cupertino, count: 5, selected: 4), ), ); expect(tester.takeException(), isNull); expect(pill(tester).right, 320 - 16); expect(pill(tester).left, greaterThanOrEqualTo(16)); }); } }