Skip to main content

select_modal_flutter 1.0.0 copy "select_modal_flutter: ^1.0.0" to clipboard
select_modal_flutter: ^1.0.0 copied to clipboard

Flutter package to create a select field that opens its options in a modal window with search filter.

example/lib/main.dart

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

void main() {
  runApp(const ExampleApp());
}

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    final scheme = ColorScheme.fromSeed(seedColor: const Color(0xFF4F46E5));
    return MaterialApp(
      title: 'select_modal_flutter',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        useMaterial3: true,
        colorScheme: scheme,
        scaffoldBackgroundColor: scheme.surface,
        extensions: [
          SelectModalStyle(
            accentColor: scheme.primary,
            modalShape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(24.0),
            ),
          ),
        ],
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  static const listItemSelectCities = [
    ItemSelect<int>(value: 1, label: 'Caracas', subtitle: 'Capital'),
    ItemSelect<int>(value: 2, label: 'San Cristobal'),
    ItemSelect<int>(value: 3, label: 'Merida'),
    ItemSelect<int>(value: 4, label: 'Maracaibo'),
    ItemSelect<int>(value: 5, label: 'Barquisimeto'),
    ItemSelect<int>(value: 6, label: 'Los Teques'),
    ItemSelect<int>(value: 7, label: 'La Guaira'),
  ];

  static const listItemSelectNames = [
    ItemSelect<int>(value: 1, label: 'Pedro', subtitle: 'Frontend', icon: Icons.person),
    ItemSelect<int>(value: 2, label: 'Juan', subtitle: 'Backend', icon: Icons.person),
    ItemSelect<int>(value: 3, label: 'Maria', subtitle: 'Designer', icon: Icons.person),
    ItemSelect<int>(value: 4, label: 'Antonio', subtitle: 'Unavailable', icon: Icons.person, enabled: false),
  ];

  ItemSelect<int>? selectedName;
  ItemSelect<int>? asyncCity;
  List<ItemSelect<int>> selectedCities = const [];
  bool submitAttempted = false;

  Future<List<ItemSelect<int>>> _searchCities(String query) async {
    await Future<void>.delayed(const Duration(milliseconds: 400));
    final q = query.toLowerCase();
    if (q.isEmpty) return listItemSelectCities;
    return listItemSelectCities
        .where((city) => city.label.toLowerCase().contains(q))
        .toList();
  }

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

    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(child: _Header(scheme: scheme)),
          SliverPadding(
            padding: const EdgeInsets.fromLTRB(16.0, 24.0, 16.0, 40.0),
            sliver: SliverList(
              delegate: SliverChildListDelegate([
                const _SectionTitle(
                  icon: Icons.radio_button_checked,
                  title: 'Single select',
                ),
                _Card(
                  child: SelectModalFlutter<int>(
                    title: 'Names',
                    searchText: 'Search a name',
                    selectedItem: selectedName,
                    listItemSelect: listItemSelectNames,
                    icon: Icons.person_outline,
                    errorText: submitAttempted && selectedName == null
                        ? 'Please select a name'
                        : null,
                    onItemSelect: (item) =>
                        setState(() => selectedName = item),
                  ),
                ),
                const SizedBox(height: 28.0),
                const _SectionTitle(
                  icon: Icons.checklist_rounded,
                  title: 'Multi select',
                ),
                _Card(
                  child: MultiSelectModalFlutter<int>(
                    title: 'Cities',
                    searchText: 'Search a city',
                    listItemSelect: listItemSelectCities,
                    listSelectedItem: selectedCities,
                    icon: Icons.location_city_outlined,
                    showHorizontalSelected: false,
                    closeChipOnTap: true,
                    maxSelection: 3,
                    showSelectAll: true,
                    showSelectedCount: true,
                    onItemSelect: (items) =>
                        setState(() => selectedCities = items),
                  ),
                ),
                const SizedBox(height: 28.0),
                const _SectionTitle(
                  icon: Icons.cloud_download_outlined,
                  title: 'Async search ยท bottom sheet',
                ),
                _Card(
                  child: SelectModalFlutter<int>(
                    title: 'City loaded on demand',
                    searchText: 'Type to search cities',
                    asyncLoader: _searchCities,
                    selectedItem: asyncCity,
                    icon: Icons.travel_explore_outlined,
                    presentation: SelectModalPresentation.bottomSheet,
                    onItemSelect: (item) => setState(() => asyncCity = item),
                  ),
                ),
                const SizedBox(height: 32.0),
                _Summary(
                  name: selectedName?.label,
                  cities: selectedCities.map((c) => c.label).toList(),
                  asyncCity: asyncCity?.label,
                ),
                const SizedBox(height: 24.0),
                FilledButton.icon(
                  onPressed: () => setState(() => submitAttempted = true),
                  icon: const Icon(Icons.save_outlined),
                  label: const Text('Save'),
                ),
              ]),
            ),
          ),
        ],
      ),
    );
  }
}

class _Header extends StatelessWidget {
  const _Header({required this.scheme});

  final ColorScheme scheme;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.fromLTRB(24.0, 56.0, 24.0, 32.0),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [scheme.primary, scheme.tertiary],
        ),
        borderRadius: const BorderRadius.vertical(
          bottom: Radius.circular(32.0),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Icon(Icons.widgets_rounded, color: Colors.white, size: 34.0),
          const SizedBox(height: 16.0),
          const Text(
            'select_modal_flutter',
            style: TextStyle(
              color: Colors.white,
              fontSize: 26.0,
              fontWeight: FontWeight.w800,
              letterSpacing: -0.5,
            ),
          ),
          const SizedBox(height: 8.0),
          Text(
            'Single & multi select modals with search, async loading, '
            'theming and a polished UX.',
            style: TextStyle(
              color: Colors.white.withValues(alpha: .85),
              fontSize: 14.0,
              height: 1.4,
            ),
          ),
        ],
      ),
    );
  }
}

class _SectionTitle extends StatelessWidget {
  const _SectionTitle({required this.icon, required this.title});

  final IconData icon;
  final String title;

  @override
  Widget build(BuildContext context) {
    final scheme = Theme.of(context).colorScheme;
    return Padding(
      padding: const EdgeInsets.only(bottom: 12.0),
      child: Row(
        children: [
          Icon(icon, size: 20.0, color: scheme.primary),
          const SizedBox(width: 8.0),
          Text(
            title,
            style: Theme.of(context).textTheme.titleMedium?.copyWith(
                  fontWeight: FontWeight.w700,
                ),
          ),
        ],
      ),
    );
  }
}

class _Card extends StatelessWidget {
  const _Card({required this.child});

  final Widget child;

  @override
  Widget build(BuildContext context) {
    final scheme = Theme.of(context).colorScheme;
    return Container(
      padding: const EdgeInsets.all(16.0),
      decoration: BoxDecoration(
        color: scheme.surfaceContainerLow,
        borderRadius: BorderRadius.circular(20.0),
        border: Border.all(color: scheme.outlineVariant),
      ),
      child: child,
    );
  }
}

class _Summary extends StatelessWidget {
  const _Summary({this.name, this.cities, this.asyncCity});

  final String? name;
  final List<String>? cities;
  final String? asyncCity;

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

    Widget row(IconData icon, String label, String? value) => Padding(
          padding: const EdgeInsets.symmetric(vertical: 4.0),
          child: Row(
            children: [
              Icon(icon, size: 18.0, color: scheme.primary),
              const SizedBox(width: 10.0),
              Text('$label: ', style: textTheme.bodyMedium),
              Expanded(
                child: Text(
                  value ?? 'โ€”',
                  style: textTheme.bodyMedium?.copyWith(
                    fontWeight: FontWeight.w700,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          ),
        );

    return Container(
      padding: const EdgeInsets.all(16.0),
      decoration: BoxDecoration(
        color: scheme.primaryContainer.withValues(alpha: .4),
        borderRadius: BorderRadius.circular(20.0),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'Current selection',
            style: textTheme.titleSmall?.copyWith(fontWeight: FontWeight.w700),
          ),
          const SizedBox(height: 8.0),
          row(Icons.person_outline, 'Name', name),
          row(Icons.location_city_outlined, 'Cities',
              (cities == null || cities!.isEmpty) ? null : cities!.join(', ')),
          row(Icons.travel_explore_outlined, 'Async city', asyncCity),
        ],
      ),
    );
  }
}
1
likes
150
points
55
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter package to create a select field that opens its options in a modal window with search filter.

Repository (GitHub)
View/report issues

Topics

#select #dropdown #modal #form

License

MIT (license)

Dependencies

flutter

More

Packages that depend on select_modal_flutter