moon_extension 0.0.1 copy "moon_extension: ^0.0.1" to clipboard
moon_extension: ^0.0.1 copied to clipboard

A comprehensive Flutter package providing useful extensions for strings, dates, and context utilities with Turkish phone number support.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:moon/moon_extension.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Moon Package Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MoonDemoPage(),
    );
  }
}

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

  @override
  State<MoonDemoPage> createState() => _MoonDemoPageState();
}

class _MoonDemoPageState extends State<MoonDemoPage> {
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _urlController = TextEditingController();
  final TextEditingController _htmlController = TextEditingController();

  String _emailValidationResult = '';
  String _phoneValidationResult = '';
  String _urlValidationResult = '';
  String _htmlResult = '';

  @override
  void dispose() {
    _emailController.dispose();
    _phoneController.dispose();
    _urlController.dispose();
    _htmlController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Moon Package Demo'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: SingleChildScrollView(
        padding: context.responsivePadding,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildScreenInfoSection(),
            const SizedBox(height: 24),
            _buildPaddingSection(),
            const SizedBox(height: 24),
            _buildResponsiveSection(),
            const SizedBox(height: 24),
            _buildDateTimeSection(),
            const SizedBox(height: 24),
            _buildStringValidationSection(),
            const SizedBox(height: 24),
            _buildPhoneFormattingSection(),
            const SizedBox(height: 24),
            _buildHtmlSection(),
          ],
        ),
      ),
    );
  }

  Widget _buildScreenInfoSection() {
    return _DemoSection(
      title: 'Screen Size Extensions',
      children: [
        _InfoCard(
          title: 'Screen Dimensions',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('Width: ${context.screenWidth().toStringAsFixed(1)}px'),
              Text('Height: ${context.screenHeight().toStringAsFixed(1)}px'),
              Text(
                'Half Width: ${context.screenWidth(0.5).toStringAsFixed(1)}px',
              ),
              Text(
                'Half Height: ${context.screenHeight(0.5).toStringAsFixed(1)}px',
              ),
              Text('Aspect Ratio: ${context.aspectRatio.toStringAsFixed(2)}'),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildPaddingSection() {
    return _DemoSection(
      title: 'Padding Extensions',
      children: [
        _InfoCard(
          title: 'Horizontal Padding',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: context.horizontal4,
                color: Colors.blue.shade100,
                child: const Text('horizontal4'),
              ),
              const SizedBox(height: 8),
              Container(
                padding: context.horizontal8,
                color: Colors.green.shade100,
                child: const Text('horizontal8'),
              ),
              const SizedBox(height: 8),
              Container(
                padding: context.horizontal16,
                color: Colors.orange.shade100,
                child: const Text('horizontal16'),
              ),
            ],
          ),
        ),
        const SizedBox(height: 16),
        _InfoCard(
          title: 'Vertical Padding',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: context.vertical8,
                color: Colors.purple.shade100,
                child: const Text('vertical8'),
              ),
              const SizedBox(height: 8),
              Container(
                padding: context.vertical16,
                color: Colors.red.shade100,
                child: const Text('vertical16'),
              ),
            ],
          ),
        ),
        const SizedBox(height: 16),
        _InfoCard(
          title: 'All Sides Padding',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: context.all8,
                color: Colors.teal.shade100,
                child: const Text('all8'),
              ),
              const SizedBox(height: 8),
              Container(
                padding: context.all16,
                color: Colors.indigo.shade100,
                child: const Text('all16'),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildResponsiveSection() {
    return _DemoSection(
      title: 'Responsive Extensions',
      children: [
        _InfoCard(
          title: 'Device Type',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('Is Mobile: ${context.isMobile}'),
              Text('Is Tablet: ${context.isTablet}'),
              Text('Is Desktop: ${context.isDesktop}'),
            ],
          ),
        ),
        const SizedBox(height: 16),
        _InfoCard(
          title: 'Responsive Padding',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: context.responsivePadding,
                color: Colors.amber.shade100,
                child: Text('Responsive Padding: ${context.responsivePadding}'),
              ),
              const SizedBox(height: 8),
              Container(
                padding: context.responsiveHorizontalPadding,
                color: Colors.cyan.shade100,
                child: Text(
                  'Responsive Horizontal: ${context.responsiveHorizontalPadding}',
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildDateTimeSection() {
    final now = DateTime.now();
    final year = now.year;

    return _DemoSection(
      title: 'DateTime Extensions',
      children: [
        _InfoCard(
          title: 'Date Formatting',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('Current Date: ${now.dateTimeFormaterWithDate()}'),
              Text('Current DateTime: ${now.dateTimeFormaterWithClock()}'),
              Text('Current Year: ${now.dateTimeFormaterWithYear()}'),
              Text('UTC Format: ${now.formatDateTimeWithoutMilliseconds()}'),
            ],
          ),
        ),
        const SizedBox(height: 16),
        _InfoCard(
          title: 'Year Extensions',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('Current Year: $year'),
              Text('Is Leap Year: ${year.isLeapYear}'),
              Text('2024 is Leap Year: ${2024.isLeapYear}'),
              Text('2023 is Leap Year: ${2023.isLeapYear}'),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildStringValidationSection() {
    return _DemoSection(
      title: 'String Validation Extensions',
      children: [
        _InfoCard(
          title: 'Email Validation',
          content: Column(
            children: [
              TextField(
                controller: _emailController,
                decoration: const InputDecoration(
                  labelText: 'Enter email',
                  border: OutlineInputBorder(),
                ),
                onChanged: (value) {
                  setState(() {
                    _emailValidationResult =
                        value.isValidEmail()
                            ? '✅ Valid Email'
                            : '❌ Invalid Email';
                  });
                },
              ),
              const SizedBox(height: 8),
              Text(_emailValidationResult),
            ],
          ),
        ),
        const SizedBox(height: 16),
        _InfoCard(
          title: 'Phone Validation',
          content: Column(
            children: [
              TextField(
                controller: _phoneController,
                decoration: const InputDecoration(
                  labelText: 'Enter phone number',
                  border: OutlineInputBorder(),
                ),
                onChanged: (value) {
                  setState(() {
                    _phoneValidationResult =
                        value.isValidPhone()
                            ? '✅ Valid Phone'
                            : '❌ Invalid Phone';
                  });
                },
              ),
              const SizedBox(height: 8),
              Text(_phoneValidationResult),
            ],
          ),
        ),
        const SizedBox(height: 16),
        _InfoCard(
          title: 'URL Validation',
          content: Column(
            children: [
              TextField(
                controller: _urlController,
                decoration: const InputDecoration(
                  labelText: 'Enter URL',
                  border: OutlineInputBorder(),
                ),
                onChanged: (value) {
                  setState(() {
                    _urlValidationResult =
                        value.isValidUrl() ? '✅ Valid URL' : '❌ Invalid URL';
                  });
                },
              ),
              const SizedBox(height: 8),
              Text(_urlValidationResult),
            ],
          ),
        ),
        const SizedBox(height: 16),
        _InfoCard(
          title: 'String Utilities',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('null.orDefault(): ${null.orDefault()}'),
              Text('"".orDefault(): ${"".orDefault()}'),
              Text('"Hello".orDefault(): ${"Hello".orDefault()}'),
              const SizedBox(height: 8),
              Text('null.isNullOrEmpty: ${null.isNullOrEmpty}'),
              Text('"".isNullOrEmpty: ${"".isNullOrEmpty}'),
              Text('"Hello".isNullOrEmpty: ${"Hello".isNullOrEmpty}'),
              const SizedBox(height: 8),
              Text('"123".isNumeric: ${"123".isNumeric()}'),
              Text('"123abc".isNumeric: ${"123abc".isNumeric()}'),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildPhoneFormattingSection() {
    return _DemoSection(
      title: 'Phone Formatting Extensions',
      children: [
        _InfoCard(
          title: 'Phone Format Examples',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '"(555) 123 4567" → ${"(555) 123 4567".toInternationalFormat()}',
              ),
              Text('"05551234567" → ${"05551234567".toInternationalFormat()}'),
              Text(
                '"905551234567" → ${"905551234567".toInternationalFormat()}',
              ),
              const SizedBox(height: 8),
              Text('"(555) 123 4567" → ${"(555) 123 4567".toSimpleFormat()}'),
              Text('"05551234567" → ${"05551234567".toSimpleFormat()}'),
              Text('"+905551234567" → ${"+905551234567".toSimpleFormat()}'),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildHtmlSection() {
    return _DemoSection(
      title: 'HTML Extensions',
      children: [
        _InfoCard(
          title: 'HTML Tag Removal',
          content: Column(
            children: [
              TextField(
                controller: _htmlController,
                decoration: const InputDecoration(
                  labelText: 'Enter HTML text',
                  border: OutlineInputBorder(),
                ),
                onChanged: (value) {
                  setState(() {
                    _htmlResult = value.removeHtmlTags();
                  });
                },
              ),
              const SizedBox(height: 8),
              Text('Original: ${_htmlController.text}'),
              Text('Cleaned: $_htmlResult'),
            ],
          ),
        ),
        const SizedBox(height: 16),
        _InfoCard(
          title: 'HTML Examples',
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '"<p>Hello <strong>World</strong>!</p>" → ${"<p>Hello <strong>World</strong>!</p>".removeHtmlTags()}',
              ),
              Text(
                '"<div>Test <span>Content</span></div>" → ${"<div>Test <span>Content</span></div>".removeHtmlTags()}',
              ),
            ],
          ),
        ),
      ],
    );
  }
}

class _DemoSection extends StatelessWidget {
  final String title;
  final List<Widget> children;

  const _DemoSection({required this.title, required this.children});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: Theme.of(context).textTheme.headlineSmall?.copyWith(
            fontWeight: FontWeight.bold,
            color: Theme.of(context).colorScheme.primary,
          ),
        ),
        const SizedBox(height: 16),
        ...children,
      ],
    );
  }
}

class _InfoCard extends StatelessWidget {
  final String title;
  final Widget content;

  const _InfoCard({required this.title, required this.content});

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 2,
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: Theme.of(
                context,
              ).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w600),
            ),
            const SizedBox(height: 12),
            content,
          ],
        ),
      ),
    );
  }
}
2
likes
150
points
8
downloads

Documentation

Documentation
API reference

Publisher

unverified uploader

Weekly Downloads

A comprehensive Flutter package providing useful extensions for strings, dates, and context utilities with Turkish phone number support.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, intl

More

Packages that depend on moon_extension