import 'dart:math' as math; 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'; testWidgets('$host: icon and selection circle sizes grow together', ( tester, ) async { await pump( tester, cupertino: cupertino, size: const Size(390, 844), child: Align( alignment: Alignment.bottomCenter, child: bar(cupertino: cupertino, count: 2, selected: 0), ), ); final icon = tester.getRect(find.byIcon(icons.first)); final mark = circle(tester, 0); final iconSize = cupertino ? FloatingDestinationBar.cupertinoIconSize : FloatingDestinationBar.materialIconSize; final highlightSize = cupertino ? FloatingDestinationBar.cupertinoHighlightSize : FloatingDestinationBar.materialHighlightSize; expect(icon.width, iconSize); expect(icon.height, iconSize); expect(mark.width, highlightSize); expect(mark.height, highlightSize); }); testWidgets( '$host: tab entrance animates once and respects reduced motion', (tester) async { tester.platformDispatcher.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures(disableAnimations: false); addTearDown(() { tester.platformDispatcher.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures(disableAnimations: true); }); tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); var selected = 0; await tester.pumpWidget( cupertino ? CupertinoApp( home: CupertinoPageScaffold( child: MediaQuery( data: const MediaQueryData(disableAnimations: false), child: StatefulBuilder( builder: (context, setState) => bar( cupertino: cupertino, count: 3, selected: selected, onTap: (index) => setState(() => selected = index), ), ), ), ), ) : MaterialApp( home: Scaffold( body: MediaQuery( data: const MediaQueryData(disableAnimations: false), child: StatefulBuilder( builder: (context, setState) => bar( cupertino: cupertino, count: 3, selected: selected, onTap: (index) => setState(() => selected = index), ), ), ), ), ), ); final entrance = find.descendant( of: find.byType(FloatingDestinationBar), matching: find.byWidgetPredicate( (widget) => widget is FadeTransition && widget.child is AnimatedBuilder, ), ); double firstOpacity() => tester.widget(entrance.at(0)).opacity.value; double lastOpacity() => tester.widget(entrance.at(2)).opacity.value; expect(firstOpacity(), 0); expect(lastOpacity(), 0); await tester.pump(const Duration(milliseconds: 130)); expect(firstOpacity(), greaterThan(0.9)); expect(lastOpacity(), lessThan(0.2)); await tester.pump(const Duration(milliseconds: 270)); expect(firstOpacity(), 1); expect(lastOpacity(), 1); await tester.tap(find.byKey(tabKey(1))); await tester.pump(); await tester.pumpWidget( MediaQuery( data: const MediaQueryData(disableAnimations: true), child: cupertino ? CupertinoApp( home: CupertinoPageScaffold( child: bar(cupertino: cupertino, count: 3, selected: 0), ), ) : MaterialApp( home: Scaffold( body: bar(cupertino: cupertino, count: 3, selected: 0), ), ), ), ); expect(tester.widget(entrance.first).opacity.value, 1); }, ); 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, with the larger named neighbour gap. 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 box = pill(tester); final edgeGap = last.top - box.top; final requestedNeighbourGap = cupertino ? FloatingDestinationBar.cupertinoNeighbourGap : FloatingDestinationBar.materialNeighbourGap; final neighbourGap = math.min( requestedNeighbourGap, (box.width - 2 * (cupertino ? FloatingDestinationBar.cupertinoEdgeGap : FloatingDestinationBar.materialEdgeGap) - count * last.width) / (count - 1), ); for (var index = 1; index < count; index += 1) { expect( centres[index] - centres[index - 1] - last.width, moreOrLessEquals(neighbourGap, epsilon: 1), ); } expect( neighbourGap - edgeGap, moreOrLessEquals( neighbourGap - (cupertino ? FloatingDestinationBar.cupertinoEdgeGap : FloatingDestinationBar.materialEdgeGap), 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)); }); } }