Sidebar Library

A responsive, multi-level sidebar library for Flutter applications that works seamlessly on both web and mobile platforms.

Features

✅ Unlimited Nesting Levels - Create categories, subcategories, sub-subcategories, and beyond
✅ Responsive Design - Automatically adapts to mobile (drawer) and desktop (fixed sidebar) layouts
✅ Expandable/Collapsible Items - Smooth animations for expanding nested menus
✅ Fully Customizable - Light/dark themes with extensive styling options
✅ Icon Support - Add icons to any menu item
✅ Badge Support - Display badges for notifications or counts
✅ Selection Tracking - Track and highlight selected items
✅ Callbacks - Handle item taps with custom actions

Installation

Add this library to your Flutter project's pubspec.yaml:

dependencies:
  sidebar_library:
    path: ../sidebar_library  # Adjust path as needed

Or if using from the same project:

import 'package:sidebar_library/sidebar_library.dart';

Usage

Basic Example

import 'package:flutter/material.dart';
import 'package:sidebar_library/sidebar_library.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SidebarLayout(
          sidebar: ResponsiveSidebar(
            items: [
              SidebarItem(
                id: '1',
                title: 'Item 1',
                icon: Icons.home,
                children: [
                  SidebarItem(
                    id: '1.1',
                    title: 'Item 1.1',
                    onTap: () => print('Item 1.1 tapped'),
                  ),
                ],
              ),
            ],
          ),
          content: Center(child: Text('Your content here')),
        ),
      ),
    );
  }
}

Multi-Level Nesting

final items = [
  SidebarItem(
    id: '1',
    title: 'Item 1',
    icon: Icons.dashboard,
    children: [
      SidebarItem(
        id: '1.1',
        title: 'Item 1.1',
        children: [
          SidebarItem(
            id: '1.1.a',
            title: 'Item 1.1.a',
            children: [
              SidebarItem(
                id: '1.1.a.i',
                title: 'Item 1.1.a.i',
                onTap: () => print('Deep nesting!'),
              ),
            ],
          ),
        ],
      ),
    ],
  ),
];

Custom Theming

ResponsiveSidebar(
  items: items,
  theme: SidebarTheme(
    backgroundColor: Color(0xFF1E1E1E),
    itemTextStyle: TextStyle(color: Colors.white, fontSize: 15),
    selectedColor: Colors.blue,
    iconColor: Colors.white70,
    indentationWidth: 20.0,
    sidebarWidth: 280.0,
  ),
)

Or use predefined themes:

// Dark theme
ResponsiveSidebar(
  items: items,
  theme: SidebarTheme.dark(),
)

// Light theme
ResponsiveSidebar(
  items: items,
  theme: SidebarTheme.light(),
)
ResponsiveSidebar(
  items: items,
  header: Container(
    padding: EdgeInsets.all(20),
    child: Column(
      children: [
        CircleAvatar(child: Icon(Icons.person)),
        SizedBox(height: 10),
        Text('John Doe'),
      ],
    ),
  ),
  footer: Container(
    padding: EdgeInsets.all(16),
    child: Text('Version 1.0.0'),
  ),
)

Track Selection

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  String? selectedItemId;

  @override
  Widget build(BuildContext context) {
    return ResponsiveSidebar(
      items: items,
      selectedItemId: selectedItemId,
      onItemSelected: (itemId) {
        setState(() {
          selectedItemId = itemId;
        });
        print('Selected: $itemId');
      },
    );
  }
}

Add Badges

SidebarItem(
  id: '1',
  title: 'Notifications',
  icon: Icons.notifications,
  badge: '5',  // Shows a red badge with "5"
)

SidebarItem(
  id: '2',
  title: 'New Feature',
  icon: Icons.star,
  badge: 'New',  // Shows a badge with "New"
)

Custom Text Color Per Item

You can now customize the text color of individual items, including subitems:

SidebarItem(
  id: '1',
  title: 'Important Item',
  icon: Icons.warning,
  textColor: Colors.red,  // Custom color for this item
  children: [
    SidebarItem(
      id: '1.1',
      title: 'Red Subitem',
      textColor: Colors.red,  // Subitems can have custom colors too!
    ),
    SidebarItem(
      id: '1.2',
      title: 'Blue Subitem',
      textColor: Colors.blue,
    ),
  ],
)

SidebarItem(
  id: '2',
  title: 'Success Item',
  icon: Icons.check_circle,
  textColor: Colors.green,
)

Set Same Color for All Child Items

Use childrenTextColor to apply the same color to all child items at once:

SidebarItem(
  id: '1',
  title: 'Parent Item',
  textColor: Colors.red,
  childrenTextColor: Colors.orange,  // All children will use this color
  children: [
    SidebarItem(id: '1.1', title: 'Subitem 1'),
    SidebarItem(id: '1.2', title: 'Subitem 2'),
    SidebarItem(id: '1.3', title: 'Subitem 3'),
  ],
)

You can still override the childrenTextColor for specific items:

SidebarItem(
  id: '1',
  title: 'Parent Item',
  childrenTextColor: Colors.orange,  // Default for all children
  children: [
    SidebarItem(id: '1.1', title: 'Orange Subitem 1'),
    SidebarItem(id: '1.2', title: 'Blue Subitem', textColor: Colors.blue),  // Override
    SidebarItem(id: '1.3', title: 'Orange Subitem 3'),
  ],
)

API Reference

SidebarItem

Property Type Description
id String Unique identifier for the item
title String Display text
icon IconData? Optional icon
onTap VoidCallback? Callback when item is tapped
children List<SidebarItem>? Nested child items
initiallyExpanded bool Whether to start expanded (default: false)
badge String? Optional badge text
textColor Color? Optional custom text color for this item (overrides theme)
childrenTextColor Color? Optional custom text color for all child items (can be overridden by child's textColor)
data dynamic Custom data to attach to item

ResponsiveSidebar

Property Type Description
items List<SidebarItem> Top-level sidebar items
theme SidebarTheme? Theme configuration
header Widget? Header widget
footer Widget? Footer widget
selectedItemId String? Currently selected item ID
onItemSelected Function(String)? Selection callback
mobileBreakpoint double Width below which mobile layout is used (default: 768)

SidebarTheme

Property Type Default
backgroundColor Color Color(0xFF2C3E50)
itemTextStyle TextStyle? -
selectedColor Color? -
iconColor Color? -
sidebarWidth double 280.0
indentationWidth double 20.0
itemPadding EdgeInsets EdgeInsets.symmetric(h: 16, v: 12)
badgeColor Color? -

SidebarLayout

Property Type Description
sidebar ResponsiveSidebar The sidebar widget
content Widget Main content area
sidebarOnRight bool Position sidebar on right (default: false)

How to Use in Another Project

  1. Copy the library: Copy the entire sidebar_library folder to your workspace

  2. Add dependency in your project's pubspec.yaml:

    dependencies:
      flutter:
        sdk: flutter
      sidebar_library:
        path: ../sidebar_library  # Adjust path relative to your project
    
  3. Import and use:

    import 'package:sidebar_library/sidebar_library.dart';
    

Responsive Behavior

  • Desktop/Web (width ≥ 768px): Sidebar is fixed and always visible
  • Mobile (width < 768px): Sidebar appears as a drawer that can be toggled
  • Custom breakpoint can be set via mobileBreakpoint property

Examples

Check lib/main.dart for a comprehensive example showing:

  • Multi-level nesting (up to 4 levels deep)
  • Light and dark themes
  • Icons and badges
  • Header and footer
  • Selection tracking
  • Responsive layout

License

This project is available for use in any Flutter project.

Contributing

Feel free to customize and extend this library for your needs!

Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter development, view the online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Libraries

A responsive, multi-level sidebar library for Flutter applications