|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:getwidget/getwidget.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + final Key segmentTabsKey = UniqueKey(); |
| 7 | + |
| 8 | + final List<Widget> tabList = [ |
| 9 | + const Text( |
| 10 | + 'Tab1', |
| 11 | + ), |
| 12 | + const Tab( |
| 13 | + icon: Icon(Icons.favorite), |
| 14 | + ), |
| 15 | + const Text( |
| 16 | + 'Tab3', |
| 17 | + ), |
| 18 | + ]; |
| 19 | + |
| 20 | + final indicator = BoxDecoration( |
| 21 | + color: Colors.teal, |
| 22 | + border: Border.all(color: Colors.tealAccent, width: 2)); |
| 23 | + |
| 24 | + testWidgets('GFSegmentTabs without length, isScrollable', (tester) async { |
| 25 | + // `GFSegmentTabs.length` null |
| 26 | + expect( |
| 27 | + () => GFSegmentTabs( |
| 28 | + length: null, |
| 29 | + ), |
| 30 | + throwsAssertionError, |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + testWidgets('GFSegmentTabs can be constructed', (tester) async { |
| 35 | + final TabController tabController = |
| 36 | + TabController(length: 3, initialIndex: 0, vsync: tester); |
| 37 | + |
| 38 | + final GFSegmentTabs segmentTabs = GFSegmentTabs( |
| 39 | + key: segmentTabsKey, |
| 40 | + tabController: tabController, |
| 41 | + length: tabList.length, |
| 42 | + tabs: tabList, |
| 43 | + ); |
| 44 | + |
| 45 | + final TestApp app = TestApp(segmentTabs); |
| 46 | + await tester.pumpWidget(app); |
| 47 | + |
| 48 | + // find segmentTabs by key |
| 49 | + expect(find.byKey(segmentTabsKey), findsOneWidget); |
| 50 | + // find the 'Tab1' text in segmentTabs |
| 51 | + expect(find.text('Tab1'), findsOneWidget); |
| 52 | + |
| 53 | + expect(tabController, isNotNull); |
| 54 | + expect(tabController.index, 0); |
| 55 | + expect(tabController.previousIndex, 0); |
| 56 | + await tester.tap(find.byIcon(Icons.favorite)); |
| 57 | + await tester.pump(); |
| 58 | + expect(tabController.indexIsChanging, true); |
| 59 | + await tester.pump(const Duration(seconds: 1)); |
| 60 | + expect(tabController.index, 1); |
| 61 | + expect(tabController.previousIndex, 0); |
| 62 | + expect(tabController.indexIsChanging, false); |
| 63 | + await tester.tap(find.byWidget(tabList[2])); |
| 64 | + await tester.pump(); |
| 65 | + await tester.pump(const Duration(seconds: 1)); |
| 66 | + expect(tabController.index, 2); |
| 67 | + expect(tabController.previousIndex, 1); |
| 68 | + await tester.tap(find.text('Tab1')); |
| 69 | + await tester.pump(); |
| 70 | + await tester.pump(const Duration(seconds: 1)); |
| 71 | + expect(tabController.index, 0); |
| 72 | + expect(tabController.previousIndex, 2); |
| 73 | + }); |
| 74 | + |
| 75 | + testWidgets('GFSegmentTabs with on taps selected tab', (tester) async { |
| 76 | + final TabController tabController = |
| 77 | + TabController(length: 3, initialIndex: 0, vsync: tester); |
| 78 | + |
| 79 | + final GFSegmentTabs segmentTabs = GFSegmentTabs( |
| 80 | + key: segmentTabsKey, |
| 81 | + tabController: tabController, |
| 82 | + length: tabList.length, |
| 83 | + tabs: tabList, |
| 84 | + ); |
| 85 | + |
| 86 | + final TestApp app = TestApp(segmentTabs); |
| 87 | + await tester.pumpWidget(app); |
| 88 | + |
| 89 | + // find segmentTabs by key |
| 90 | + expect(find.byKey(segmentTabsKey), findsOneWidget); |
| 91 | + // find the 'Tab1' text in segmentTabs |
| 92 | + expect(find.text('Tab1'), findsOneWidget); |
| 93 | + expect(find.byIcon(Icons.favorite), findsOneWidget); |
| 94 | + expect(find.byWidget(tabList[2]), findsOneWidget); |
| 95 | + expect(tabController.index, 0); |
| 96 | + expect(tabController.previousIndex, 0); |
| 97 | + await tester.tap(find.byWidget(tabList[2])); |
| 98 | + await tester.pumpAndSettle(); |
| 99 | + expect(tabController.index, 2); |
| 100 | + await tester.tap(find.byIcon(Icons.favorite)); |
| 101 | + await tester.pumpAndSettle(); |
| 102 | + expect(tabController.index, 1); |
| 103 | + await tester.tap(find.text('Tab1')); |
| 104 | + await tester.pumpAndSettle(); |
| 105 | + expect(tabController.index, 0); |
| 106 | + }); |
| 107 | + |
| 108 | + testWidgets('GFSegmentTabs with on taps center selected tab', (tester) async { |
| 109 | + final TabController controller = |
| 110 | + TabController(length: 12, initialIndex: 0, vsync: tester); |
| 111 | + final List<Widget> tabs = [ |
| 112 | + const Text( |
| 113 | + 'Tab_A', |
| 114 | + ), |
| 115 | + const Text( |
| 116 | + 'Tab_B', |
| 117 | + ), |
| 118 | + const Text( |
| 119 | + 'Tab_C', |
| 120 | + ), |
| 121 | + const Text( |
| 122 | + 'Tab_D', |
| 123 | + ), |
| 124 | + const Text( |
| 125 | + 'Tab_E', |
| 126 | + ), |
| 127 | + const Text( |
| 128 | + 'Tab_F', |
| 129 | + ), |
| 130 | + const Text( |
| 131 | + 'Tab_G', |
| 132 | + ), |
| 133 | + const Text( |
| 134 | + 'Tab_H', |
| 135 | + ), |
| 136 | + const Text( |
| 137 | + 'Tab_I', |
| 138 | + ), |
| 139 | + const Text( |
| 140 | + 'Tab_J', |
| 141 | + ), |
| 142 | + const Text( |
| 143 | + 'Tab_K', |
| 144 | + ), |
| 145 | + const Text( |
| 146 | + 'Tab_L', |
| 147 | + ), |
| 148 | + ]; |
| 149 | + |
| 150 | + final GFSegmentTabs segmentTabs = GFSegmentTabs( |
| 151 | + key: segmentTabsKey, |
| 152 | + tabController: controller, |
| 153 | + length: tabs.length, |
| 154 | + tabs: tabs, |
| 155 | + ); |
| 156 | + |
| 157 | + final TestApp app = TestApp(segmentTabs); |
| 158 | + await tester.pumpWidget(app); |
| 159 | + |
| 160 | + // find segmentTabs by key |
| 161 | + expect(find.byKey(segmentTabsKey), findsOneWidget); |
| 162 | + // find the 'Tab_A' text in segmentTabs |
| 163 | + expect(find.text('Tab_A'), findsOneWidget); |
| 164 | + expect(controller, isNotNull); |
| 165 | + expect(controller.index, 0); |
| 166 | + expect(tester.getSize(find.byKey(segmentTabsKey)).width, equals(800.0)); |
| 167 | + // The center of the 'Tab_F' item is to the right of the TabBar's center |
| 168 | + expect(tester.getCenter(find.text('Tab_F')).dx, greaterThan(301.0)); |
| 169 | + await tester.tap(find.text('Tab_F')); |
| 170 | + await tester.pumpAndSettle(); |
| 171 | + expect(controller.index, 5); |
| 172 | + // The center of the 'Tab_F' item is now at the TabBar's center |
| 173 | + expect(tester.getCenter(find.text('Tab_F')).dx, closeTo(366.0, 1.0)); |
| 174 | + }); |
| 175 | + |
| 176 | + testWidgets('GFSegmentTabs can be constructed with properties', |
| 177 | + (tester) async { |
| 178 | + final TabController tabController = |
| 179 | + TabController(length: 3, initialIndex: 0, vsync: tester); |
| 180 | + |
| 181 | + final GFSegmentTabs segmentTabs = GFSegmentTabs( |
| 182 | + key: segmentTabsKey, |
| 183 | + tabController: tabController, |
| 184 | + length: tabList.length, |
| 185 | + tabs: tabList, |
| 186 | + tabBarColor: Colors.blueGrey, |
| 187 | + height: 66, |
| 188 | + width: 244, |
| 189 | + border: Border.all(color: Colors.teal), |
| 190 | + borderRadius: BorderRadius.circular(5), |
| 191 | + indicator: indicator, |
| 192 | + indicatorWeight: 5, |
| 193 | + indicatorPadding: const EdgeInsets.all(8), |
| 194 | + indicatorColor: GFColors.WHITE, |
| 195 | + indicatorSize: TabBarIndicatorSize.tab, |
| 196 | + labelPadding: const EdgeInsets.all(8), |
| 197 | + labelColor: Colors.lightGreen, |
| 198 | + unselectedLabelColor: Colors.black, |
| 199 | + labelStyle: const TextStyle( |
| 200 | + fontWeight: FontWeight.w500, |
| 201 | + fontSize: 16, |
| 202 | + ), |
| 203 | + unselectedLabelStyle: const TextStyle( |
| 204 | + fontWeight: FontWeight.w500, |
| 205 | + fontSize: 12, |
| 206 | + ), |
| 207 | + ); |
| 208 | + |
| 209 | + final TestApp app = TestApp(segmentTabs); |
| 210 | + await tester.pumpWidget(app); |
| 211 | + |
| 212 | + expect(app.segmentTabs.tabController, tabController); |
| 213 | + expect(app.segmentTabs.length, tabList.length); |
| 214 | + expect(app.segmentTabs.tabs, tabList); |
| 215 | + expect(app.segmentTabs.tabBarColor, Colors.blueGrey); |
| 216 | + expect(app.segmentTabs.height, 66); |
| 217 | + expect(app.segmentTabs.width, 244); |
| 218 | + expect(app.segmentTabs.border, Border.all(color: Colors.teal)); |
| 219 | + expect(app.segmentTabs.borderRadius, BorderRadius.circular(5)); |
| 220 | + expect(app.segmentTabs.indicator, indicator); |
| 221 | + expect(app.segmentTabs.indicatorWeight, 5); |
| 222 | + expect(app.segmentTabs.indicatorPadding, const EdgeInsets.all(8)); |
| 223 | + expect(app.segmentTabs.indicatorColor, GFColors.WHITE); |
| 224 | + expect(app.segmentTabs.indicatorSize, TabBarIndicatorSize.tab); |
| 225 | + expect(app.segmentTabs.labelPadding, const EdgeInsets.all(8)); |
| 226 | + expect(app.segmentTabs.labelColor, Colors.lightGreen); |
| 227 | + expect(app.segmentTabs.unselectedLabelColor, Colors.black); |
| 228 | + expect( |
| 229 | + app.segmentTabs.labelStyle, |
| 230 | + const TextStyle( |
| 231 | + fontWeight: FontWeight.w500, |
| 232 | + fontSize: 16, |
| 233 | + )); |
| 234 | + expect( |
| 235 | + app.segmentTabs.unselectedLabelStyle, |
| 236 | + const TextStyle( |
| 237 | + fontWeight: FontWeight.w500, |
| 238 | + fontSize: 12, |
| 239 | + )); |
| 240 | + }); |
| 241 | +} |
| 242 | + |
| 243 | +class TestApp extends StatefulWidget { |
| 244 | + const TestApp(this.segmentTabs); |
| 245 | + |
| 246 | + final GFSegmentTabs segmentTabs; |
| 247 | + |
| 248 | + @override |
| 249 | + _TestAppState createState() => _TestAppState(); |
| 250 | +} |
| 251 | + |
| 252 | +class _TestAppState extends State<TestApp> { |
| 253 | + @override |
| 254 | + Widget build(BuildContext context) => MaterialApp( |
| 255 | + home: Scaffold( |
| 256 | + appBar: AppBar( |
| 257 | + title: const Text('Segment Tabs'), |
| 258 | + ), |
| 259 | + body: const Text('Body'), |
| 260 | + bottomNavigationBar: widget.segmentTabs, |
| 261 | + ), |
| 262 | + ); |
| 263 | +} |
0 commit comments