drawer_state_observer 0.0.2
drawer_state_observer: ^0.0.2 copied to clipboard
A hacky solution to detect Scaffold drawer open state for cases where you cannot customize the Scaffold/Drawer implementation. WARNING: Prefer customizing your Scaffold with onDrawerChanged/onEndDrawe [...]
import 'package:material_ui/material_ui.dart';
import 'widget_example.dart';
import 'stream_example.dart';
import 'proper_approach_example.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Drawer State Observer Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Drawer State Observer Examples'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
_buildExampleCard(
context,
title: 'Widget Approach',
description: 'Using DrawerStateObserver widget',
icon: Icons.widgets,
color: Colors.blue,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const WidgetExample()),
);
},
),
const SizedBox(height: 16),
_buildExampleCard(
context,
title: 'Stream Approach',
description: 'Using StreamSubscription for reactive programming',
icon: Icons.stream,
color: Colors.purple,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const StreamExample()),
);
},
),
const SizedBox(height: 16),
_buildExampleCard(
context,
title: 'Proper Flutter Approach',
description: 'Using Scaffold callbacks (RECOMMENDED)',
icon: Icons.check_circle,
color: Colors.green,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ProperApproachExample(),
),
);
},
),
const SizedBox(height: 32),
Card(
color: Colors.red.shade50,
child: const Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.warning, color: Colors.red),
SizedBox(width: 8),
Text(
'Important Note',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.red,
),
),
],
),
SizedBox(height: 8),
Text(
'The drawer_state_observer package is a workaround for cases where you cannot '
'modify the Scaffold widget. In production code, prefer using Scaffold\'s '
'onDrawerChanged and onEndDrawerChanged callbacks.',
style: TextStyle(fontSize: 14),
),
],
),
),
),
],
),
);
}
Widget _buildExampleCard(
BuildContext context, {
required String title,
required String description,
required IconData icon,
required Color color,
required VoidCallback onTap,
}) {
return Card(
elevation: 2,
child: InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(icon, color: color, size: 32),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: Theme.of(context).textTheme.titleLarge
?.copyWith(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(
description,
style: Theme.of(context).textTheme.bodyMedium
?.copyWith(color: Colors.grey.shade600),
),
],
),
),
const Icon(Icons.arrow_forward_ios, color: Colors.grey),
],
),
),
),
);
}
}