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

Simplified platform channel communication for Flutter with support for all 6 platforms (iOS, Android, Web, Windows, macOS, Linux) and WASM compatibility.

example/lib/main.dart

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Platform Channels Plus Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Platform Channels Plus Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _platformInfo = 'Loading...';
  String _methodResult = 'No method called yet';
  String _eventData = 'No events received';
  String _messageResult = 'No message sent yet';

  @override
  void initState() {
    super.initState();
    _loadPlatformInfo();
    _setupEventChannel();
  }

  Future<void> _loadPlatformInfo() async {
    final platform = PlatformDetector.currentPlatform;
    final capabilities = PlatformDetector.supportedCapabilities;

    setState(() {
      _platformInfo = '''
Platform: ${platform.name}
Is Web: ${PlatformDetector.isWeb}
Is Mobile: ${PlatformDetector.isMobile}
Is Desktop: ${PlatformDetector.isDesktop}
Native Code: ${PlatformDetector.supportsNativeCode}
WASM: ${PlatformDetector.supportsWasm}
Capabilities: ${capabilities.join(', ')}
''';
    });
  }

  void _setupEventChannel() {
    final eventChannel = EventChannelPlus('demo_events');
    eventChannel.receiveBroadcastStream().listen((event) {
      setState(() {
        _eventData = 'Event: $event';
      });
    });
  }

  Future<void> _testMethodChannel() async {
    final channel = MethodChannelPlus('demo_methods');

    try {
      final result = await channel.invokeMethod('getData', {'test': true});

      setState(() {
        if (result.isSuccess) {
          _methodResult = 'Success: ${result.data}';
        } else {
          _methodResult = 'Error: ${result.error}';
        }
      });
    } catch (e) {
      setState(() {
        _methodResult = 'Exception: $e';
      });
    }
  }

  Future<void> _testMessageChannel() async {
    final messageChannel = BasicMessageChannelPlus.forText('demo_messages');

    try {
      final result = await messageChannel.sendAndReceive('Hello from Flutter!');

      setState(() {
        if (result.isSuccess) {
          _messageResult = 'Response: ${result.data}';
        } else {
          _messageResult = 'Error: ${result.error}';
        }
      });
    } catch (e) {
      setState(() {
        _messageResult = 'Exception: $e';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Platform Information',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
                    const SizedBox(height: 8),
                    Text(_platformInfo,
                        style: Theme.of(context).textTheme.bodyMedium),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Method Channel Test',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
                    const SizedBox(height: 8),
                    Text(_methodResult,
                        style: Theme.of(context).textTheme.bodyMedium),
                    const SizedBox(height: 8),
                    ElevatedButton(
                      onPressed: _testMethodChannel,
                      child: const Text('Test Method Channel'),
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Event Channel Test',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
                    const SizedBox(height: 8),
                    Text(_eventData,
                        style: Theme.of(context).textTheme.bodyMedium),
                    const SizedBox(height: 8),
                    const Text(
                        'Events are automatically received every 2 seconds'),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Message Channel Test',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
                    const SizedBox(height: 8),
                    Text(_messageResult,
                        style: Theme.of(context).textTheme.bodyMedium),
                    const SizedBox(height: 8),
                    ElevatedButton(
                      onPressed: _testMessageChannel,
                      child: const Text('Test Message Channel'),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
160
points
0
downloads

Publisher

verified publisherbechattaoui.dev

Weekly Downloads

Simplified platform channel communication for Flutter with support for all 6 platforms (iOS, Android, Web, Windows, macOS, Linux) and WASM compatibility.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on flutter_platform_channels_plus