Flutter ShadcnUI

repository·main·Indexed 25 days ago

https://github.com/nank1ro/flutter-shadcn-ui

A library of highly customizable UI components for Flutter inspired by the Shadcn UI design system. It provides a collection of high-quality widgets—including ShadButton, ShadAccordion, ShadCalendar, and ShadAvatar—designed for simplicity and deep customization. The library includes a CLI for installing specific components directly into project source code to allow full ownership of the implementation.

Tokens
55.6K
Snippets
170
Records
236
Agent score
81%

What's inside flutter-shadcn-ui

  1. Use the Breadcrumb component

    main

    The ShadBreadcrumb component displays the path to the current resource using a hierarchy of links. It accepts a list of children which can include ShadBreadcrumbLink for clickable navigation, ShadBreadcrumbDropdown for collapsed menus, or standard widgets like Text for the current page label.

    class PrimaryBreadcrumb extends StatelessWidget {
      const PrimaryBreadcrumb({super.key});
    
      @override
      Widget build(BuildContext context) {
        return ShadBreadcrumb(
          children: [
            ShadBreadcrumbLink(
              onPressed: () => print('Navigating to Home'),
              child: const Text('Home'),
            ),
            ShadBreadcrumbDropdown(
              items: [
                ShadBreadcrumbDropMenuItem(
                  onPressed: () => print('Navigating to Documentation'),
                  child: const Text('Documentation'),
                ),
                ShadBreadcrumbDropMenuItem(
                  onPressed: () => print('Navigating to Themes'),
                  child: const Text('Themes'),
                ),
                ShadBreadcrumbDropMenuItem(
                  onPressed: () => print('Navigating to Github'),
                  child: const Text('Github'),
                ),
              ],
              showDropdownArrow: false,
              child: ShadBreadcrumbEllipsis(),
            ),
            Text('Components'),
            Text('Breadcrumb'),
          ],
        );
      }
    }
  2. Use the ShadTooltip component

    main

    The ShadTooltip component displays a popup containing information related to an element when the element receives keyboard focus or mouse hover.

    To implement a tooltip, wrap your target widget with ShadTooltip and provide a builder function that returns the content to be displayed. The child parameter should be the widget that triggers the tooltip.

    Important Requirement: For hover functionality to work, the child widget must implement ShadGestureDetector. Standard widgets like ShadButton already implement this, but if you are using a plain widget (like an Image), you must wrap it in a ShadGestureDetector to enable hover triggers.

    ShadTooltip(
      builder: (context) => const Text('Add to library'),
      child: ShadButton.outline(
        child: const Text('Hover/Focus'),
        onPressed: () {},
      ),
    ),
  3. Configure the app theme and color scheme

    main

    Use ShadApp to define the application's theme. You can specify theme and darkTheme using ShadThemeData. Each ShadThemeData requires a brightness and a colorScheme.

    Supported color scheme names include: blue, gray, green, neutral, orange, red, rose, slate, stone, violet, yellow, and zinc.

    @override
    Widget build(BuildContext context) {
      return ShadApp(
        darkTheme: ShadThemeData(
          brightness: Brightness.dark,
          colorScheme: const ShadSlateColorScheme.dark(),
        ),
        child: ...
      );
    }
  4. Use the ShadBadge component

    main

    The ShadBadge component displays a badge or a component that looks like a badge. It provides several constructors to achieve different visual styles: Primary (default), Secondary, Destructive, and Outline.

    // Primary (Default)
    ShadBadge(
      child: const Text('Primary'),
    )
    
    // Secondary
    ShadBadge.secondary(
      child: const Text('Secondary'),
    )
    
    // Destructive
    ShadBadge.destructive(
      child: const Text('Destructive'),
    )
    
    // Outline
    ShadBadge.outline(
      child: const Text('Outline'),
    )
  5. Use Shadcn with Cupertino components

    main

    To use Shadcn components alongside Cupertino components, use ShadApp.custom with an appBuilder that returns a CupertinoApp. Include the necessary localization delegates. If using a Router, use CupertinoApp.router.

    import 'package:shadcn_ui/shadcn_ui.dart';
    import 'package:flutter/cupertino.dart';
    import 'package:flutter_localizations/flutter_localizations.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return ShadApp.custom(
          themeMode: ThemeMode.dark,
          darkTheme: ShadThemeData(
            brightness: Brightness.dark,
            colorScheme: const ShadSlateColorScheme.dark(),
          ),
          appBuilder: (context) {
            return CupertinoApp(
              theme: CupertinoTheme.of(context),
              localizationsDelegates: const [
                GlobalShadLocalizations.delegate,
                DefaultMaterialLocalizations.delegate,
                DefaultCupertinoLocalizations.delegate,
                DefaultWidgetsLocalizations.delegate,
              ],
              builder: (context, child) {
                return ShadAppBuilder(child: child!);
              },
            );
          },
        );
      }
    }
  6. Implement a form with ShadForm

    main

    Use ShadForm to manage form state, validation, and field values centrally. This eliminates the need to manage individual controllers for every field. To interact with the form, use a GlobalKey<ShadFormState>.

    Key benefits:

    • Centralized state management.
    • Access all values as a single Map<String, dynamic>.
    • Automatic validation via saveAndValidate().
    final formKey = GlobalKey<ShadFormState>();
    
    // In your build method
    ShadForm(
      key: formKey,
      child: Column(
        children: [
          ShadInputFormField(
            id: 'username',
            label: const Text('Username'),
            validator: (v) {
              if (v.length < 2) return 'Username must be at least 2 characters.';
              return null;
            },
          ),
          ShadButton(
            child: const Text('Submit'),
            onPressed: () {
              if (formKey.currentState!.saveAndValidate()) {
                print('Success: ${formKey.currentState!.value}');
              }
            },
          ),
        ],
      ),
    )
  7. Configure Theme and Color Scheme in ShadApp

    main

    To define the theme and color scheme for your application, use the ShadThemeData class within the theme or darkTheme properties of ShadApp. You can specify the brightness and a specific colorScheme (e.g., ShadSlateColorScheme).

    You can override specific properties of a theme, such as individual color scheme properties (like background) or component themes (like primaryButtonTheme).

    import 'package:shadcn_ui/shadcn_ui.dart';
    
    @override
    Widget build(BuildContext context) {
      return ShadApp(
        darkTheme: ShadThemeData(
          brightness: Brightness.dark,
          colorScheme: const ShadSlateColorScheme.dark(
            background: Colors.blue,
          ),
          primaryButtonTheme: const ShadButtonTheme(
            backgroundColor: Colors.cyan,
          ),
        ),
        child: ...
      );
    }