zh_native_channel 0.0.3 copy "zh_native_channel: ^0.0.3" to clipboard
zh_native_channel: ^0.0.3 copied to clipboard

Reusable platform channel message framework for Flutter.

example/lib/main.dart

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

import 'base/zHNativeChannel/ChannelGeneratedRegister.g.dart';
import 'base/zHNativeChannel/msgs/ping_msg.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ZH Native Channel Example',
      theme: ThemeData(colorScheme: .fromSeed(seedColor: Colors.deepPurple)),
      home: const MyHomePage(),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> {
  late final _channel = ZHNativeChannel.instance;

  String _result = '尚未调用';

  Future<void> _sendPing() async {
    setState(() {
      _result = '调用中...';
    });

    try {
      final response = await _channel.invoke(PingMsg(text: 'hello'));
      final ping = response as PingMsg;
      setState(() {
        _result = ping.text;
      });
    } catch (error) {
      setState(() {
        _result = '调用失败: $error';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('ZH Native Channel Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: .center,
          children: [
            const Text('点击按钮向原生发送 PingMsg'),
            const SizedBox(height: 16),
            Text(_result, style: Theme.of(context).textTheme.titleMedium),
            const SizedBox(height: 24),
            FilledButton(onPressed: _sendPing, child: const Text('发送 Ping')),
          ],
        ),
      ),
    );
  }
}
0
likes
0
points
193
downloads

Publisher

unverified uploader

Weekly Downloads

Reusable platform channel message framework for Flutter.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_web_plugins

More

Packages that depend on zh_native_channel

Packages that implement zh_native_channel