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

Counter Service for WebSocket communication

example/lib/main.dart

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Counter Service Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const CounterPage(),
    );
  }
}

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

  @override
  _CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  final CounterService _counterService = CounterService();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Counter Service Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Counter Value:',
              style: TextStyle(fontSize: 20),
            ),
            StreamBuilder<int>(
              stream: _counterService.counterStream,
              builder: (context, snapshot) {
                return Text(
                  '${snapshot.data ?? _counterService.counterValue}',
                  style: const TextStyle(fontSize: 48, fontWeight: FontWeight.bold),
                );
              },
            ),
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: _counterService.decrementCounter,
                  child: const Text('-'),
                ),
                const SizedBox(width: 20),
                ElevatedButton(
                  onPressed: _counterService.incrementCounter,
                  child: const Text('+'),
                ),
              ],
            ),
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: _connectToServer,
                  child: const Text('Connect'),
                ),
                const SizedBox(width: 20),
                ElevatedButton(
                  onPressed: _disconnectFromServer,
                  child: const Text('Disconnect'),
                ),
              ],
            ),
            const SizedBox(height: 20),
            StreamBuilder<int>(
              stream: _counterService.counterStream,
              builder: (context, snapshot) {
                return Text(
                  'Connection Status: ${_counterService.isConnected ? 'Connected' : 'Disconnected'}',
                  style: TextStyle(
                    color: _counterService.isConnected ? Colors.green : Colors.red,
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _connectToServer() async {
    await _counterService.connect('ws://127.0.0.1:5000/ws-counter');
    setState(() {}); // Trigger a rebuild to update the connection status
  }

  void _disconnectFromServer() {
    _counterService.disconnect();
    setState(() {}); // Trigger a rebuild to update the connection status
  }

  @override
  void dispose() {
    _counterService.disconnect();
    super.dispose();
  }
}
0
likes
130
points
17
downloads

Publisher

unverified uploader

Weekly Downloads

Counter Service for WebSocket communication

Documentation

API reference

License

MIT (license)

Dependencies

flutter, web_socket_channel

More

Packages that depend on counter_service