data_handler 0.0.4
data_handler: ^0.0.4 copied to clipboard
Refactored architecture with enhanced global configuration for state management and data handling in Flutter applications. This package provides a robust solution for managing API states, including lo [...]
import 'package:data_handler/data_handler.dart';
import 'package:example/screen/public_api_screen.dart';
import 'package:example/theme/app_theme.dart';
import 'package:flutter/material.dart';
// Import your enhanced DataHandler here
// import 'package:data_handler/data_handler.dart';
void main() {
// Setup global widgets before running the app
_setupGlobalWidgets();
runApp(const MyApp());
}
/// Setup global widgets for consistent UI across the app
void _setupGlobalWidgets() {
DataHandlerConfig.setGlobalWidgets(
loadingWidget: () => const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(
strokeWidth: 3,
valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF6C5CE7)),
),
SizedBox(height: 20),
Text(
'Loading...',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.grey,
),
),
],
),
),
errorWidget: (error) => Center(
child: Container(
margin: const EdgeInsets.all(24),
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Colors.red.shade50,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: Colors.red.shade200),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.error_outline_rounded,
size: 64,
color: Colors.red.shade400,
),
const SizedBox(height: 16),
const Text(
'Oops! Something went wrong',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.red,
),
),
const SizedBox(height: 12),
Text(
error,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
color: Colors.red.shade700,
height: 1.5,
),
),
],
),
),
),
emptyWidget: (message) => Center(
child: Container(
margin: const EdgeInsets.all(24),
padding: const EdgeInsets.all(32),
decoration: BoxDecoration(
color: Colors.grey.shade100,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: Colors.grey.shade300),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.inbox_outlined,
size: 80,
color: Colors.grey.shade400,
),
const SizedBox(height: 20),
Text(
message.isEmpty ? 'No data available' : message,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.grey.shade600,
),
),
const SizedBox(height: 8),
Text(
'Pull to refresh or try again',
style: TextStyle(
fontSize: 14,
color: Colors.grey.shade500,
),
),
],
),
),
),
);
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isDarkMode = false;
void _toggleTheme() {
setState(() {
_isDarkMode = !_isDarkMode;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Enhanced Data Handler Example',
debugShowCheckedModeBanner: false,
themeMode: _isDarkMode ? ThemeMode.dark : ThemeMode.light,
theme: AppTheme.buildLightTheme(),
darkTheme: AppTheme.buildDarkTheme(),
home:
PublicApiExample(toggleTheme: _toggleTheme, isDarkMode: !_isDarkMode),
);
}
}