Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/screens/main_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'network_screen.dart';
import 'battery_screen.dart';

class MainLayout extends StatefulWidget {
const MainLayout({super.key});
final List<Widget>? screens; // Allow injecting mock screens for testing

const MainLayout({super.key, this.screens});

@override
State<MainLayout> createState() => _MainLayoutState();
Expand All @@ -16,13 +18,19 @@ class MainLayout extends StatefulWidget {
class _MainLayoutState extends State<MainLayout> {
int _currentIndex = 0;

final List<Widget> _screens = const [
DashboardScreen(),
HardwareScreen(),
SystemScreen(),
BatteryScreen(),
NetworkScreen(), // We'll put Network here based on the icons design
];
late final List<Widget> _screens;

@override
void initState() {
super.initState();
_screens = widget.screens ?? const [
DashboardScreen(),
HardwareScreen(),
SystemScreen(),
BatteryScreen(),
NetworkScreen(), // We'll put Network here based on the icons design
];
}

@override
Widget build(BuildContext context) {
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.17.0"
mocktail:
dependency: "direct dev"
description:
name: mocktail
sha256: "890df3f9688106f25755f26b1c60589a92b3ab91a22b8b224947ad041bf172d8"
url: "https://pub.dev"
source: hosted
version: "1.0.4"
native_toolchain_c:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dev_dependencies:
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^5.0.0
mocktail: ^1.0.4

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
73 changes: 73 additions & 0 deletions test/screens/main_layout_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:myapp/screens/main_layout.dart';

void main() {
Widget createTestWidget() {
return MaterialApp(
home: MainLayout(
screens: [
Container(key: const Key('screen_0'), color: Colors.red),
Container(key: const Key('screen_1'), color: Colors.green),
Container(key: const Key('screen_2'), color: Colors.blue),
Container(key: const Key('screen_3'), color: Colors.yellow),
Container(key: const Key('screen_4'), color: Colors.purple),
],
),
);
}

testWidgets('MainLayout initializes properly and renders bottom navigation', (WidgetTester tester) async {
await tester.pumpWidget(createTestWidget());

// Initial load, IndexedStack index should be 0
final indexedStack = tester.widget<IndexedStack>(find.byType(IndexedStack));
expect(indexedStack.index, 0);

// Verify nav items exist
expect(find.text('Dashboard'), findsOneWidget);
expect(find.text('Device'), findsOneWidget);
expect(find.text('System'), findsOneWidget);
expect(find.text('Battery'), findsOneWidget);
expect(find.text('Network'), findsOneWidget);
});

testWidgets('MainLayout changes screens when bottom navigation items are tapped', (WidgetTester tester) async {
await tester.pumpWidget(createTestWidget());

// Tap on Device (index 1)
await tester.tap(find.text('Device'));
await tester.pumpAndSettle();

var indexedStack = tester.widget<IndexedStack>(find.byType(IndexedStack));
expect(indexedStack.index, 1);

// Tap on System (index 2)
await tester.tap(find.text('System'));
await tester.pumpAndSettle();

indexedStack = tester.widget<IndexedStack>(find.byType(IndexedStack));
expect(indexedStack.index, 2);

// Tap on Battery (index 3)
await tester.tap(find.text('Battery'));
await tester.pumpAndSettle();

indexedStack = tester.widget<IndexedStack>(find.byType(IndexedStack));
expect(indexedStack.index, 3);

// Tap on Network (index 4)
await tester.tap(find.text('Network'));
await tester.pumpAndSettle();

indexedStack = tester.widget<IndexedStack>(find.byType(IndexedStack));
expect(indexedStack.index, 4);

// Tap on Dashboard (index 0)
await tester.tap(find.text('Dashboard'));
await tester.pumpAndSettle();

indexedStack = tester.widget<IndexedStack>(find.byType(IndexedStack));
expect(indexedStack.index, 0);
});
}