Skip to main content

gt_dropdown 1.3.0 copy "gt_dropdown: ^1.3.0" to clipboard
gt_dropdown: ^1.3.0 copied to clipboard

A highly customizable, feature-rich dropdown widget for Flutter with form validation, search, animations, and extensive styling options.

example/lib/main.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gt_dropdown/gt_dropdown.dart';

/// The main entry point of the Flutter application.
void main() {
  runApp(const MyApp());
}

/// The root widget of the application with light/dark theme support.
class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ThemeMode _themeMode = ThemeMode.system;

  void _toggleTheme(ThemeMode mode) {
    setState(() {
      _themeMode = mode;
    });
  }

  @override
  Widget build(BuildContext context) {
    const seedColor = Color(0xFF6366F1); // Modern Indigo

    return MaterialApp(
      title: 'GT Dropdown Modern Showcase',
      debugShowCheckedModeBanner: false,
      themeMode: _themeMode,
      theme: ThemeData(
        useMaterial3: true,
        brightness: Brightness.light,
        colorScheme: ColorScheme.fromSeed(
          seedColor: seedColor,
          brightness: Brightness.light,
        ),
        scaffoldBackgroundColor: const Color(0xFFF8FAFC),
        cardTheme: const CardThemeData(
          elevation: 0,
          color: Colors.white,
          margin: EdgeInsets.zero,
        ),
        appBarTheme: const AppBarTheme(
          centerTitle: false,
          elevation: 0,
          scrolledUnderElevation: 2,
        ),
      ),
      darkTheme: ThemeData(
        useMaterial3: true,
        brightness: Brightness.dark,
        colorScheme: ColorScheme.fromSeed(
          seedColor: seedColor,
          brightness: Brightness.dark,
        ),
        scaffoldBackgroundColor: const Color(0xFF0F172A),
        cardTheme: const CardThemeData(
          elevation: 0,
          color: Color(0xFF1E293B),
          margin: EdgeInsets.zero,
        ),
        appBarTheme: const AppBarTheme(
          centerTitle: false,
          elevation: 0,
          scrolledUnderElevation: 2,
        ),
      ),
      home: DropdownShowcaseScreen(
        themeMode: _themeMode,
        onThemeChanged: _toggleTheme,
      ),
    );
  }
}

/// Main showcase page demonstrating modern dropdown layouts.
class DropdownShowcaseScreen extends StatefulWidget {
  final ThemeMode themeMode;
  final ValueChanged<ThemeMode> onThemeChanged;

  const DropdownShowcaseScreen({
    super.key,
    required this.themeMode,
    required this.onThemeChanged,
  });

  @override
  State<DropdownShowcaseScreen> createState() => _DropdownShowcaseScreenState();
}

class _DropdownShowcaseScreenState extends State<DropdownShowcaseScreen> {
  // Selections
  String? _selectedCategory = 'Mobile Development';
  Country? _selectedCountry;
  String? _selectedCity;
  String? _selectedPriority = 'High';
  String? _selectedCupertinoItem = 'Express Delivery';
  String? _selectedAdaptiveItem = 'Cloud Storage';
  bool _formSubmitted = false;

  // Sample data
  final List<String> _categories = [
    'Mobile Development',
    'Web Architecture',
    'Cloud Computing',
    'Machine Learning',
    'DevOps & CI/CD',
    'UI/UX Design',
  ];

  final List<Country> _countries = [
    Country('United States', '🇺🇸', 'North America'),
    Country('Germany', '🇩🇪', 'Europe'),
    Country('Japan', '🇯🇵', 'Asia'),
    Country('India', '🇮🇳', 'Asia'),
    Country('Brazil', '🇧🇷', 'South America'),
    Country('United Kingdom', '🇬🇧', 'Europe'),
    Country('Australia', '🇦🇺', 'Oceania'),
  ];

  final List<String> _cities = [
    'San Francisco',
    'Berlin',
    'Tokyo',
    'Bengaluru',
    'London',
    'Sydney',
    'Toronto',
    'Singapore',
    'Paris',
    'Zurich',
  ];

  final List<PriorityItem> _priorities = [
    PriorityItem('Low', Icons.arrow_downward, AppColors.emerald),
    PriorityItem('Medium', Icons.remove, AppColors.amber),
    PriorityItem('High', Icons.arrow_upward, Colors.orange),
    PriorityItem('Critical', Icons.priority_high, AppColors.rose),
  ];

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;

    return Scaffold(
      appBar: AppBar(
        title: Row(
          children: [
            Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: colorScheme.primary.withValues(alpha: 0.15),
                borderRadius: BorderRadius.circular(10),
              ),
              child: Icon(
                Icons.layers_rounded,
                color: colorScheme.primary,
                size: 22,
              ),
            ),
            const SizedBox(width: 12),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  'GT Dropdown Examples',
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                Text(
                  'Material 3 • Cupertino • Adaptive',
                  style: TextStyle(
                    fontSize: 11,
                    color: colorScheme.onSurfaceVariant,
                  ),
                ),
              ],
            ),
          ],
        ),
        actions: [
          _buildThemeToggle(context),
          const SizedBox(width: 8),
        ],
      ),
      body: ListView(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
        children: [
          // Hero Header Card
          _buildHeroHeader(context),
          const SizedBox(height: 20),

          // Live State Inspector Bar
          _buildLiveStateBar(context),
          const SizedBox(height: 24),

          // Section 1: Adaptive Dropdown
          _buildCard(
            context,
            badge: 'PLATFORM AWARE',
            badgeColor: Colors.teal,
            title: 'Adaptive Dropdown',
            description:
                'Switches smoothly between Material 3 on Android/Web/Desktop and native Cupertino on iOS/macOS.',
            child: AdaptiveDropdown<String>(
              items: const [
                'Cloud Storage',
                'Dedicated Database',
                'Serverless Functions',
                'Edge Network',
              ],
              initialSelection: _selectedAdaptiveItem,
              itemBuilder: (context, item) => Text(item),
              onSelected: (val) => setState(() => _selectedAdaptiveItem = val),
              label: const Text('Architecture Service'),
              hintText: 'Select cloud service',
              prefixIcon: const Icon(Icons.cloud_outlined, size: 20),
            ),
          ),
          const SizedBox(height: 20),

          // Section 2: Cupertino Native Dropdown
          _buildCard(
            context,
            badge: 'iOS NATIVE',
            badgeColor: Colors.blue,
            title: 'Cupertino Dropdown (iOS Native)',
            description:
                'Apple Human Interface Guideline experience with CupertinoPicker wheel and modal sheet workflow.',
            child: CupertinoDropdown<String>(
              items: const [
                'Standard Delivery',
                'Express Delivery',
                'Priority Air',
                'Same Day Courier',
              ],
              initialSelection: _selectedCupertinoItem,
              itemBuilder: (context, item) => Text(item),
              onSelected: (val) => setState(() => _selectedCupertinoItem = val),
              label: const Text('Shipping Method'),
              placeholder: 'Select shipping speed',
              prefix: const Icon(CupertinoIcons.cube_box, size: 18),
            ),
          ),
          const SizedBox(height: 20),

          // Section 3: Material 3 Custom Model Dropdown
          _buildCard(
            context,
            badge: 'MATERIAL 3',
            badgeColor: colorScheme.primary,
            title: 'Model Class Dropdown',
            description:
                'Fully typed generic model rendering with custom emoji flag and region subtitle.',
            child: CustomDropdown<Country>(
              items: _countries,
              initialSelection: _selectedCountry,
              itemBuilder: (context, country) => Row(
                children: [
                  Text(country.flag, style: const TextStyle(fontSize: 22)),
                  const SizedBox(width: 12),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text(
                          country.name,
                          style: const TextStyle(fontWeight: FontWeight.w600),
                        ),
                        Text(
                          country.region,
                          style: TextStyle(
                            fontSize: 11,
                            color: colorScheme.onSurfaceVariant,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              onSelected: (country) =>
                  setState(() => _selectedCountry = country),
              label: const Text('Select Country'),
              hintText: 'Choose region',
              prefixIcon: const Icon(Icons.public, size: 20),
              borderRadius: 14,
              menuElevation: 3,
            ),
          ),
          const SizedBox(height: 20),

          // Section 4: Searchable Dropdown with Validation
          _buildCard(
            context,
            badge: 'SEARCH & VALIDATION',
            badgeColor: Colors.purple,
            title: 'Searchable Form Dropdown',
            description:
                'Integrated filtering, search callback, and form validation state handling.',
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CustomDropdown<String>(
                  items: _cities,
                  initialSelection: _selectedCity,
                  itemBuilder: (context, city) => Text(city),
                  onSelected: (city) => setState(() => _selectedCity = city),
                  enableSearch: true,
                  enableFilter: true,
                  label: const Text('Destination City'),
                  hintText: 'Type to filter cities...',
                  menuHeight: 280,
                  prefixIcon: const Icon(Icons.location_city, size: 20),
                  validationMessage: _formSubmitted && _selectedCity == null
                      ? 'Destination city is required'
                      : null,
                ),
                const SizedBox(height: 14),
                Row(
                  children: [
                    FilledButton.icon(
                      onPressed: () {
                        setState(() => _formSubmitted = true);
                        if (_selectedCity != null) {
                          ScaffoldMessenger.of(context).showSnackBar(
                            SnackBar(
                              content: Text(
                                'Validated city selection: $_selectedCity',
                              ),
                              behavior: SnackBarBehavior.floating,
                            ),
                          );
                        }
                      },
                      icon: const Icon(Icons.check_circle_outline, size: 18),
                      label: const Text('Validate & Submit'),
                    ),
                    const SizedBox(width: 12),
                    OutlinedButton(
                      onPressed: () {
                        setState(() {
                          _selectedCity = null;
                          _formSubmitted = false;
                        });
                      },
                      child: const Text('Reset'),
                    ),
                  ],
                ),
              ],
            ),
          ),
          const SizedBox(height: 20),

          // Section 5: Custom Styling Dropdown
          _buildCard(
            context,
            badge: 'CUSTOM STYLED',
            badgeColor: Colors.amber.shade800,
            title: 'Custom Styled Dropdown',
            description:
                'Custom priority indicators with badges, customized border radius, and elevation.',
            child: CustomDropdown<String>(
              items: _priorities.map((p) => p.label).toList(),
              initialSelection: _selectedPriority,
              itemBuilder: (context, label) {
                final item = _priorities.firstWhere((p) => p.label == label);
                return Row(
                  children: [
                    Container(
                      padding: const EdgeInsets.all(6),
                      decoration: BoxDecoration(
                        color: item.color.withValues(alpha: 0.15),
                        shape: BoxShape.circle,
                      ),
                      child: Icon(item.icon, color: item.color, size: 16),
                    ),
                    const SizedBox(width: 12),
                    Text(
                      item.label,
                      style: const TextStyle(fontWeight: FontWeight.w600),
                    ),
                  ],
                );
              },
              onSelected: (val) => setState(() => _selectedPriority = val),
              label: const Text('Priority Level'),
              hintText: 'Set severity',
              borderRadius: 16,
              menuElevation: 4,
              prefixIcon: const Icon(Icons.flag_outlined, size: 20),
            ),
          ),
          const SizedBox(height: 20),

          // Section 6: Simple String Dropdown
          _buildCard(
            context,
            badge: 'SIMPLE STRING',
            badgeColor: Colors.blueGrey,
            title: 'Simple String Dropdown',
            description: 'Lightweight dropdown with plain strings.',
            child: CustomDropdown<String>(
              items: _categories,
              initialSelection: _selectedCategory,
              itemBuilder: (context, cat) => Text(cat),
              onSelected: (val) => setState(() => _selectedCategory = val),
              label: const Text('Domain Category'),
              hintText: 'Select domain',
              prefixIcon: const Icon(Icons.category_outlined, size: 20),
            ),
          ),
          const SizedBox(height: 20),

          // Section 7: Disabled Dropdown
          _buildCard(
            context,
            badge: 'DISABLED STATE',
            badgeColor: Colors.grey,
            title: 'Disabled Dropdown',
            description:
                'Interaction locked for read-only or restricted access.',
            child: CustomDropdown<String>(
              items: const ['Administrator', 'Moderator', 'Member'],
              initialSelection: 'Administrator',
              itemBuilder: (context, item) => Text(item),
              onSelected: (val) {},
              enabled: false,
              label: const Text('System Role (Locked)'),
              hintText: 'Role is locked',
              prefixIcon: const Icon(Icons.lock_outline, size: 20),
            ),
          ),
          const SizedBox(height: 32),
        ],
      ),
    );
  }

  Widget _buildThemeToggle(BuildContext context) {
    final theme = Theme.of(context);
    final isDark = theme.brightness == Brightness.dark;

    return PopupMenuButton<ThemeMode>(
      tooltip: 'Switch Theme',
      icon: Container(
        padding: const EdgeInsets.all(8),
        decoration: BoxDecoration(
          color: theme.colorScheme.surfaceContainerHighest,
          borderRadius: BorderRadius.circular(10),
        ),
        child: Icon(
          isDark ? Icons.dark_mode_rounded : Icons.light_mode_rounded,
          size: 20,
          color: theme.colorScheme.primary,
        ),
      ),
      initialValue: widget.themeMode,
      onSelected: widget.onThemeChanged,
      itemBuilder: (context) => [
        const PopupMenuItem(
          value: ThemeMode.system,
          child: Row(
            children: [
              Icon(Icons.brightness_auto, size: 18),
              SizedBox(width: 10),
              Text('System Theme'),
            ],
          ),
        ),
        const PopupMenuItem(
          value: ThemeMode.light,
          child: Row(
            children: [
              Icon(Icons.light_mode, size: 18),
              SizedBox(width: 10),
              Text('Light Theme'),
            ],
          ),
        ),
        const PopupMenuItem(
          value: ThemeMode.dark,
          child: Row(
            children: [
              Icon(Icons.dark_mode, size: 18),
              SizedBox(width: 10),
              Text('Dark Theme'),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildHeroHeader(BuildContext context) {
    final isDark = Theme.of(context).brightness == Brightness.dark;

    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: isDark
              ? [const Color(0xFF1E1B4B), const Color(0xFF312E81)]
              : [const Color(0xFF4F46E5), const Color(0xFF6366F1)],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
        borderRadius: BorderRadius.circular(20),
        boxShadow: [
          BoxShadow(
            color: const Color(0xFF6366F1).withValues(alpha: 0.25),
            blurRadius: 16,
            offset: const Offset(0, 6),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding:
                    const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                decoration: BoxDecoration(
                  color: Colors.white.withValues(alpha: 0.2),
                  borderRadius: BorderRadius.circular(20),
                ),
                child: const Text(
                  'v1.3.0 Release',
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                    fontSize: 11,
                    letterSpacing: 0.5,
                  ),
                ),
              ),
              const Spacer(),
              Container(
                padding:
                    const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                decoration: BoxDecoration(
                  color: AppColors.emerald.withValues(alpha: 0.25),
                  borderRadius: BorderRadius.circular(20),
                ),
                child: const Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Icon(Icons.verified,
                        color: AppColors.emeraldAccent, size: 14),
                    SizedBox(width: 4),
                    Text(
                      '160/160 Score',
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 11,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          const SizedBox(height: 14),
          const Text(
            'Modernized GT Dropdown',
            style: TextStyle(
              color: Colors.white,
              fontSize: 22,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 6),
          Text(
            'Production-ready Flutter package with Material 3, native iOS Cupertino, and platform-adaptive dropdowns.',
            style: TextStyle(
              color: Colors.white.withValues(alpha: 0.85),
              fontSize: 13,
              height: 1.4,
            ),
          ),
          const SizedBox(height: 14),
          Wrap(
            spacing: 8,
            runSpacing: 6,
            children: [
              _buildFeaturePill('Material 3'),
              _buildFeaturePill('Cupertino Picker'),
              _buildFeaturePill('Platform Adaptive'),
              _buildFeaturePill('WASM Ready'),
              _buildFeaturePill('6 Platforms'),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildFeaturePill(String label) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
      decoration: BoxDecoration(
        color: Colors.white.withValues(alpha: 0.15),
        borderRadius: BorderRadius.circular(8),
        border:
            Border.all(color: Colors.white.withValues(alpha: 0.2), width: 0.5),
      ),
      child: Text(
        label,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 11,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }

  Widget _buildLiveStateBar(BuildContext context) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;

    return Container(
      padding: const EdgeInsets.all(14),
      decoration: BoxDecoration(
        color: colorScheme.surfaceContainerHighest.withValues(alpha: 0.5),
        borderRadius: BorderRadius.circular(14),
        border: Border.all(
          color: colorScheme.outlineVariant.withValues(alpha: 0.5),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Icon(Icons.insights, size: 16, color: colorScheme.primary),
              const SizedBox(width: 8),
              Text(
                'LIVE SELECTION INSPECTOR',
                style: TextStyle(
                  fontSize: 11,
                  fontWeight: FontWeight.bold,
                  letterSpacing: 0.8,
                  color: colorScheme.primary,
                ),
              ),
            ],
          ),
          const SizedBox(height: 10),
          Wrap(
            spacing: 12,
            runSpacing: 8,
            children: [
              _buildStateChip(
                'Adaptive',
                _selectedAdaptiveItem ?? 'None',
                colorScheme,
              ),
              _buildStateChip(
                'Cupertino',
                _selectedCupertinoItem ?? 'None',
                colorScheme,
              ),
              _buildStateChip(
                'Country',
                _selectedCountry?.name ?? 'None',
                colorScheme,
              ),
              _buildStateChip(
                'Priority',
                _selectedPriority ?? 'None',
                colorScheme,
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildStateChip(
    String label,
    String value,
    ColorScheme colorScheme,
  ) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      decoration: BoxDecoration(
        color: colorScheme.surface,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(
          color: colorScheme.outlineVariant.withValues(alpha: 0.4),
        ),
      ),
      child: RichText(
        text: TextSpan(
          style: TextStyle(fontSize: 12, color: colorScheme.onSurface),
          children: [
            TextSpan(
              text: '$label: ',
              style: TextStyle(
                fontWeight: FontWeight.w500,
                color: colorScheme.onSurfaceVariant,
              ),
            ),
            TextSpan(
              text: value,
              style: const TextStyle(fontWeight: FontWeight.bold),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCard(
    BuildContext context, {
    required String badge,
    required Color badgeColor,
    required String title,
    required String description,
    required Widget child,
  }) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;
    final isDark = theme.brightness == Brightness.dark;

    return Container(
      padding: const EdgeInsets.all(18),
      decoration: BoxDecoration(
        color: isDark ? const Color(0xFF1E293B) : Colors.white,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(
          color: isDark ? const Color(0xFF334155) : const Color(0xFFE2E8F0),
          width: 1,
        ),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: isDark ? 0.3 : 0.04),
            blurRadius: 10,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
                decoration: BoxDecoration(
                  color: badgeColor.withValues(alpha: 0.15),
                  borderRadius: BorderRadius.circular(6),
                ),
                child: Text(
                  badge,
                  style: TextStyle(
                    color: badgeColor,
                    fontSize: 10,
                    fontWeight: FontWeight.bold,
                    letterSpacing: 0.6,
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 10),
          Text(
            title,
            style: const TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 4),
          Text(
            description,
            style: TextStyle(
              fontSize: 12.5,
              color: colorScheme.onSurfaceVariant,
              height: 1.35,
            ),
          ),
          const SizedBox(height: 16),
          child,
        ],
      ),
    );
  }
}

// Helpers & Models

class Country {
  final String name;
  final String flag;
  final String region;

  Country(this.name, this.flag, this.region);

  @override
  String toString() => name;
}

class PriorityItem {
  final String label;
  final IconData icon;
  final Color color;

  PriorityItem(this.label, this.icon, this.color);
}

// App Color Palette
abstract final class AppColors {
  static const Color emerald = Color(0xFF10B981);
  static const Color emeraldAccent = Color(0xFF34D399);
  static const Color rose = Color(0xFFF43F5E);
  static const Color amber = Color(0xFFF59E0B);
}
4
likes
150
points
47
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A highly customizable, feature-rich dropdown widget for Flutter with form validation, search, animations, and extensive styling options.

Repository (GitHub)
View/report issues

Topics

#dropdown #form-field #select #ui #widget

License

GPL-3.0 (license)

Dependencies

flutter

More

Packages that depend on gt_dropdown