Last active
July 28, 2025 18:42
-
-
Save mhadaily/75f4d8125f141a5f8f1dfd3ffcaa422c to your computer and use it in GitHub Desktop.
Loading Progress in Flutter With Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Progress App', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const ProgressScreen(), | |
); | |
} | |
} | |
class ProgressScreen extends StatelessWidget { | |
const ProgressScreen({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Loading Progress'), | |
), | |
body: Center( | |
child: Padding( | |
padding: const EdgeInsets.all(24.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'Fetching data...', | |
style: TextStyle(fontSize: 18), | |
), | |
const SizedBox(height: 20), | |
DelayedProgressWidget(), | |
const SizedBox(height: 10), | |
const Text( | |
'Please wait', | |
style: TextStyle(fontSize: 14, color: Colors.grey), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class DelayedProgressWidget extends StatefulWidget { | |
const DelayedProgressWidget({super.key}); | |
@override | |
_DelayedProgressWidgetState createState() => _DelayedProgressWidgetState(); | |
} | |
class _DelayedProgressWidgetState extends State<DelayedProgressWidget> { | |
double progress = 0.0; | |
Timer? _timer; | |
@override | |
void initState() { | |
super.initState(); | |
_timer = Timer.periodic(const Duration(seconds: 1), (Timer timer) { | |
setState(() { | |
progress += 1 / 3; | |
if (progress > 1.0) { | |
progress = 1.0; // Cap progress at 1.0 | |
timer.cancel(); | |
} | |
}); | |
}); | |
} | |
@override | |
void dispose() { | |
_timer?.cancel(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return LinearProgressIndicator( | |
value: progress, | |
minHeight: 10.0, | |
backgroundColor: Colors.grey[300], | |
valueColor: const AlwaysStoppedAnimation<Color>(Colors.blueAccent), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:flutter/material.dart'; | |
import 'delayed_progress_widget.dart'; | |
void main() { | |
testWidgets('progress bar completes after 3 seconds', (tester) async { | |
await tester.pumpWidget(MaterialApp(home: DelayedProgressWidget())); | |
// Initially, progress should be zero. | |
var progressIndicator = tester.widget<LinearProgressIndicator>( | |
find.byType(LinearProgressIndicator), | |
); | |
expect(progressIndicator.value, 0.0); | |
// Simulate 1 second passing. | |
await tester.pump(Duration(seconds: 1)); | |
progressIndicator = tester.widget(find.byType(LinearProgressIndicator)); | |
expect(progressIndicator.value, closeTo(1 / 3, 0.01)); | |
// Simulate total of 3 seconds passing. | |
await tester.pump(Duration(seconds: 2)); | |
progressIndicator = tester.widget(find.byType(LinearProgressIndicator)); | |
expect(progressIndicator.value, closeTo(1.0, 0.01)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment