block_dialog 2.0.0 copy "block_dialog: ^2.0.0" to clipboard
block_dialog: ^2.0.0 copied to clipboard

Block-based, animated dialogs for Flutter with composable layout, typed results, and fine-grained dismissal control.

example/lib/main.dart

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

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

/// The main example application.
class BlockDialogExampleApp extends StatelessWidget {
  const BlockDialogExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Block Dialog Example',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
      home: const ExampleHome(),
    );
  }
}

/// The home screen with example dialogs.
class ExampleHome extends StatelessWidget {
  const ExampleHome({super.key});

  // placeholder for a preloaded banner ad widget, which can be used in the dialog
  BlockRow adRow(BuildContext context) {
    return BlockRow(
      ignoreInPositionResolving: true,
      width: MediaQuery.of(context).size.width,
      blocks: [
        BlockCustom(
          matchDialogTheme: false,
          override: BlockOverride(
            animationPosition: BlockPosition.top,
            customAnimation: BlockAnimation.slide(distanceMultiplier: 2),
          ),
          builder: (context, dialogController, blockController, configs) =>
              Container(
                height: 50,
                color: Colors.blue[50],
                child: const Center(child: Text('Banner Ad Placeholder')),
              ),
        ),
      ],
    );
  }

  void showSimpleConfirmationDialog(BuildContext context) {
    BlockDialog.show(
      context,
      rows: [
        adRow(context),
        BlockRow(blocks: [BlockText('Confirmation', isDialogTitle: true)]),
        BlockRow(blocks: [BlockText('Are you sure?')]),
        BlockRow(
          blocks: [
            BlockButton(label: 'Cancel'),
            BlockButton(
              label: 'Confirm',
              isPositive: true,
              onPressed: (results, dialogController) =>
                  showResult(context, 'Confirmed!'),
            ),
          ],
        ),
      ],
    );
  }

  void showUseFormDialog(BuildContext context) {
    BlockDialog.show(
      context,
      rows: [
        adRow(context),
        BlockRow(blocks: [BlockText('User Form', isDialogTitle: true)]),
        BlockRow(
          blocks: [
            BlockInputField(
              resultId: 'email',
              blockTag: 'emailInput',
              hintText: 'Please enter your Email',
            ),
          ],
        ),
        BlockRow(
          blocks: [
            BlockInputField(
              resultId: 'password',
              blockTag: 'passwordInput',
              hintText: 'Please enter your Password',
              obscureText: true,
            ),
          ],
        ),
        BlockRow(
          blocks: [
            BlockRadioGroup<String>(
              resultId: 'switch',
              options: ['Log In', 'Sign Up'],
              initialValue: 'Log In',
              onChanged: (value, controller) {
                controller.setBlockButtonLabel('button', value);
              },
            ),
          ],
        ),
        BlockRow(
          blocks: [
            BlockButton(
              label: 'Log In',
              blockTag: 'button',
              isPositive: true,
              onPressedWithError: (results, dialogController) {
                final email = results.get('email');
                final password = results.get('password');
                if (email == null || email.isEmpty) {
                  dialogController.shakeBlock('emailInput');
                  dialogController.setBlockInputFieldErrorText(
                    'emailInput',
                    'Email is required',
                  );
                  return 'Email is required';
                }
                if (password == null || password.isEmpty) {
                  dialogController.shakeBlock('passwordInput');
                  dialogController.setBlockInputFieldErrorText(
                    'passwordInput',
                    'Password is required',
                  );
                  return 'Password is required';
                }
                if (password.length < 6) {
                  dialogController.shakeBlock('passwordInput');
                  dialogController.setBlockInputFieldErrorText(
                    'passwordInput',
                    'Password must be at least 6 characters',
                  );
                  return 'Password must be at least 6 characters';
                }
                showResult(context, 'Email: $email\nPassword: $password');
                return null;
              },
            ),
          ],
        ),
      ],
    );
  }

  void showResult(BuildContext context, String result) {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(result)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Block Dialog Example')),
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              child: const Text('Simple Confirmation Dialog'),
              onPressed: () => showSimpleConfirmationDialog(context),
            ),
            ElevatedButton(
              child: const Text('User Form Dialog'),
              onPressed: () => showUseFormDialog(context),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
0
points
168
downloads

Publisher

verified publishertripletroop.com

Weekly Downloads

Block-based, animated dialogs for Flutter with composable layout, typed results, and fine-grained dismissal control.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on block_dialog