flutter_test_helpers 0.0.2
flutter_test_helpers: ^0.0.2 copied to clipboard
Mock data generators, test utilities, and widget testing helpers for Flutter applications.
example/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_test_helpers/flutter_test_helpers.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Test Helpers Example',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Test Helpers Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({required this.title, super.key});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Color _randomColor = Colors.blue;
String _randomString = '';
DateTime _randomDate = DateTime.now();
List<String> _randomStrings = <String>[];
List<Color> _randomColors = <Color>[];
void _generateRandomData() {
setState(() {
_randomColor = MockDataGenerators.randomColor();
_randomString = MockDataGenerators.randomString(15);
_randomDate = MockDataGenerators.randomDate();
_randomStrings = MockDataGenerators.randomStringList(5);
_randomColors = MockDataGenerators.randomColorList(5);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Mock Data Generators Section
_buildSection(
'Mock Data Generators',
'Generate random test data for your Flutter tests',
[
_buildDataCard(
'Random Color', _randomColor.toString(), _randomColor),
_buildDataCard('Random String', _randomString, null),
_buildDataCard('Random Date', _randomDate.toString(), null),
_buildDataCard(
'Random String List', _randomStrings.join(', '), null),
_buildDataCard('Random Color List',
'${_randomColors.length} colors', null),
],
),
const SizedBox(height: 24),
// Extensions Section
_buildSection(
'Extensions',
'Useful extension methods for common Flutter types',
[
_buildExtensionExample(
'String.capitalize', 'hello world'.capitalize),
_buildExtensionExample(
'String.titleCase', 'hello world'.titleCase),
_buildExtensionExample('String.truncate',
'This is a very long string'.truncate(20)),
_buildExtensionExample(
'DateTime.isToday', DateTime.now().isToday.toString()),
_buildExtensionExample('DateTime.startOfDay',
DateTime.now().startOfDay.toString()),
_buildExtensionExample(
'Color.toHex', _randomColor.toHex().toString()),
],
),
const SizedBox(height: 24),
// Test Utilities Section
_buildSection(
'Test Utilities',
'Helper methods for creating test apps and waiting for conditions',
[
_buildUtilityExample('createTestApp',
'Creates a MaterialApp wrapper for testing'),
_buildUtilityExample(
'createTestAppWithTheme', 'Creates a themed test app'),
_buildUtilityExample(
'waitForCondition', 'Waits for a condition to be true'),
],
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _generateRandomData,
tooltip: 'Generate Random Data',
child: const Icon(Icons.refresh),
),
);
}
Widget _buildSection(
String title, String description, List<Widget> children) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(
description,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.grey[600],
),
),
const SizedBox(height: 16),
...children,
],
);
}
Widget _buildDataCard(String title, Object value, Color? color) {
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
children: [
if (color != null)
Container(
width: 24,
height: 24,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.grey[300]!),
),
),
if (color != null) const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(fontWeight: FontWeight.w500),
),
const SizedBox(height: 4),
Text(
value.toString(),
style: TextStyle(
color: Colors.grey[600],
fontSize: 12,
),
),
],
),
),
],
),
),
);
}
Widget _buildExtensionExample(String method, String result) {
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
method,
style: const TextStyle(
fontWeight: FontWeight.w500,
fontFamily: 'monospace',
),
),
const SizedBox(height: 4),
Text(
'Result: $result',
style: TextStyle(
color: Colors.grey[600],
fontSize: 12,
),
),
],
),
),
);
}
Widget _buildUtilityExample(String method, String description) {
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
method,
style: const TextStyle(
fontWeight: FontWeight.w500,
fontFamily: 'monospace',
),
),
const SizedBox(height: 4),
Text(
description,
style: TextStyle(
color: Colors.grey[600],
fontSize: 12,
),
),
],
),
),
);
}
}