build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  return Card(
    child: Padding(
      padding: const EdgeInsets.all(SubZeroSpacing.lg),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Basic Cards
          Text('Card Variants', style: SubZeroTypography.textTheme.titleMedium),
          const SizedBox(height: SubZeroSpacing.sm),
          Text(
            'Static and interactive cards with customizable content sections',
            style: SubZeroTypography.textTheme.bodySmall,
          ),
          const SizedBox(height: SubZeroSpacing.lg),

          // Static Card with Header Only
          Text('Header Only', style: SubZeroTypography.textTheme.labelLarge),
          const SizedBox(height: SubZeroSpacing.sm),
          SubZeroCard(
            header: const SubZeroCardHeader(
              title: 'Card Title',
              subtitle: 'Supporting text for the card',
              leading: Icon(Icons.folder_outlined, color: SubZeroColors.primary),
              trailing: Icon(Icons.more_vert, color: SubZeroColors.textSecondary),
            ),
          ),

          const Divider(height: SubZeroSpacing.xxl),

          // Card with Media
          Text('Media Aspect Ratios', style: SubZeroTypography.textTheme.titleMedium),
          const SizedBox(height: SubZeroSpacing.sm),
          Text(
            '1:0.5 (wide), 1:1 (square), 4:3 (standard), 1:1.5 (portrait)',
            style: SubZeroTypography.textTheme.bodySmall,
          ),
          const SizedBox(height: SubZeroSpacing.lg),
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: [
                _MediaRatioCard(
                  label: '1:0.5 Wide',
                  ratio: SubZeroCardMediaRatio.wide,
                ),
                const SizedBox(width: SubZeroSpacing.md),
                _MediaRatioCard(
                  label: '1:1 Square',
                  ratio: SubZeroCardMediaRatio.square,
                ),
                const SizedBox(width: SubZeroSpacing.md),
                _MediaRatioCard(
                  label: '4:3 Standard',
                  ratio: SubZeroCardMediaRatio.standard,
                ),
                const SizedBox(width: SubZeroSpacing.md),
                _MediaRatioCard(
                  label: '1:1.5 Portrait',
                  ratio: SubZeroCardMediaRatio.portrait,
                ),
              ],
            ),
          ),

          const Divider(height: SubZeroSpacing.xxl),

          // Full Featured Card
          Text('Full Featured Card', style: SubZeroTypography.textTheme.titleMedium),
          const SizedBox(height: SubZeroSpacing.sm),
          Text(
            'Header + Media + Body + Actions',
            style: SubZeroTypography.textTheme.bodySmall,
          ),
          const SizedBox(height: SubZeroSpacing.lg),
          SizedBox(
            width: 320,
            child: SubZeroCard(
              media: SubZeroCardMedia(
                child: Container(
                  color: SubZeroColors.surfaceVariant,
                  child: const Center(
                    child: Icon(Icons.image_outlined, size: 64, color: SubZeroColors.textTertiary),
                  ),
                ),
                aspectRatio: SubZeroCardMediaRatio.standard,
                semanticLabel: 'Placeholder image',
              ),
              header: const SubZeroCardHeader(
                title: 'Featured Article',
                subtitle: 'Published Jan 15, 2025',
              ),
              body: Text(
                'This is the body content of the card. It can contain any widget including text, images, or custom layouts.',
                style: SubZeroTypography.textTheme.bodyMedium,
              ),
              actions: [
                SubZeroButton(
                  label: 'Learn More',
                  variant: SubZeroButtonVariant.primary,
                  size: SubZeroButtonSize.small,
                  onPressed: () {},
                ),
                SubZeroButton(
                  label: 'Share',
                  variant: SubZeroButtonVariant.tertiary,
                  size: SubZeroButtonSize.small,
                  onPressed: () {},
                ),
              ],
            ),
          ),

          const Divider(height: SubZeroSpacing.xxl),

          // Interactive Card
          Text('Interactive Cards', style: SubZeroTypography.textTheme.titleMedium),
          const SizedBox(height: SubZeroSpacing.sm),
          Text(
            'Tappable cards with hover/focus states',
            style: SubZeroTypography.textTheme.bodySmall,
          ),
          const SizedBox(height: SubZeroSpacing.lg),
          Wrap(
            spacing: SubZeroSpacing.md,
            runSpacing: SubZeroSpacing.md,
            children: [
              SizedBox(
                width: 240,
                child: SubZeroCard(
                  onTap: () => debugPrint('Card tapped!'),
                  header: const SubZeroCardHeader(
                    title: 'Tappable Card',
                    subtitle: 'Tap or hover over me',
                    leading: Icon(Icons.touch_app_outlined, color: SubZeroColors.accent),
                  ),
                  body: Text(
                    'This card responds to tap, hover, and focus.',
                    style: SubZeroTypography.textTheme.bodySmall,
                  ),
                  semanticLabel: 'Interactive card example',
                ),
              ),
              SizedBox(
                width: 240,
                child: SubZeroCard(
                  onTap: () {},
                  showBorder: true,
                  elevation: SubZeroCardElevation.none,
                  header: const SubZeroCardHeader(
                    title: 'Bordered Card',
                    subtitle: 'No shadow, with border',
                    leading: Icon(Icons.crop_square, color: SubZeroColors.primary),
                  ),
                ),
              ),
            ],
          ),

          const Divider(height: SubZeroSpacing.xxl),

          // Elevation Levels
          Text('Elevation Levels', style: SubZeroTypography.textTheme.titleMedium),
          const SizedBox(height: SubZeroSpacing.sm),
          Text(
            'None, Low, Medium (default), and High elevation',
            style: SubZeroTypography.textTheme.bodySmall,
          ),
          const SizedBox(height: SubZeroSpacing.lg),
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: [
                _ElevationCard(label: 'None', elevation: SubZeroCardElevation.none),
                const SizedBox(width: SubZeroSpacing.md),
                _ElevationCard(label: 'Low', elevation: SubZeroCardElevation.low),
                const SizedBox(width: SubZeroSpacing.md),
                _ElevationCard(label: 'Medium', elevation: SubZeroCardElevation.medium),
                const SizedBox(width: SubZeroSpacing.md),
                _ElevationCard(label: 'High', elevation: SubZeroCardElevation.high),
              ],
            ),
          ),

          const Divider(height: SubZeroSpacing.xxl),

          // Horizontal Card
          Text('Horizontal Card', style: SubZeroTypography.textTheme.titleMedium),
          const SizedBox(height: SubZeroSpacing.sm),
          Text(
            'Side-by-side layout for list items',
            style: SubZeroTypography.textTheme.bodySmall,
          ),
          const SizedBox(height: SubZeroSpacing.lg),
          SubZeroCardGroup(
            children: [
              SubZeroHorizontalCard(
                leading: Container(
                  width: 56,
                  height: 56,
                  decoration: BoxDecoration(
                    color: SubZeroColors.surfaceVariant,
                    borderRadius: BorderRadius.circular(SubZeroRadius.sm),
                  ),
                  child: const Icon(Icons.article_outlined, color: SubZeroColors.primary),
                ),
                title: 'Document Title',
                subtitle: 'Last modified 2 hours ago',
                trailing: const Icon(Icons.chevron_right, color: SubZeroColors.textTertiary),
                onTap: () => debugPrint('Horizontal card tapped'),
              ),
              SubZeroHorizontalCard(
                leading: const SubZeroAvatar(
                  initials: 'JD',
                  size: SubZeroAvatarSize.l,
                ),
                title: 'John Doe',
                subtitle: 'Product Designer • Online',
                trailing: SubZeroButton(
                  label: 'Follow',
                  variant: SubZeroButtonVariant.secondary,
                  size: SubZeroButtonSize.small,
                  onPressed: () {},
                ),
                onTap: () {},
              ),
              SubZeroHorizontalCard(
                leading: Container(
                  width: 56,
                  height: 56,
                  decoration: BoxDecoration(
                    color: SubZeroColors.success.withValues(alpha: 0.1),
                    borderRadius: BorderRadius.circular(SubZeroRadius.sm),
                  ),
                  child: const Icon(Icons.check_circle_outline, color: SubZeroColors.success),
                ),
                title: 'Task Completed',
                subtitle: 'Review design mockups',
                showBorder: true,
                elevation: SubZeroCardElevation.none,
              ),
            ],
          ),

          const Divider(height: SubZeroSpacing.xxl),

          // Card Group
          Text('Card Group', style: SubZeroTypography.textTheme.titleMedium),
          const SizedBox(height: SubZeroSpacing.sm),
          Text(
            'Multiple cards with consistent spacing',
            style: SubZeroTypography.textTheme.bodySmall,
          ),
          const SizedBox(height: SubZeroSpacing.lg),
          SubZeroCardGroup(
            direction: Axis.horizontal,
            children: [
              SizedBox(
                width: 180,
                child: SubZeroCard(
                  onTap: () {},
                  header: const SubZeroCardHeader(
                    title: 'Card 1',
                    leading: Icon(Icons.looks_one, color: SubZeroColors.accent),
                  ),
                ),
              ),
              SizedBox(
                width: 180,
                child: SubZeroCard(
                  onTap: () {},
                  header: const SubZeroCardHeader(
                    title: 'Card 2',
                    leading: Icon(Icons.looks_two, color: SubZeroColors.primary),
                  ),
                ),
              ),
              SizedBox(
                width: 180,
                child: SubZeroCard(
                  onTap: () {},
                  header: const SubZeroCardHeader(
                    title: 'Card 3',
                    leading: Icon(Icons.looks_3, color: SubZeroColors.success),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    ),
  );
}