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

Convert Listenable (e.g. ChangeNotifier), ValueListenable(e.g. ValueNotifier) to Stream / ValueStream.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';
import 'package:flutter_disposebag/flutter_disposebag.dart';
import 'package:listenable_stream/listenable_stream.dart';
import 'package:rxdart_ext/state_stream.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'listenable_stream example',
      theme: ThemeData.dark(),
      home: const MyHomePage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('listenable_stream example'),
      ),
      body: Center(
        child: ElevatedButton.icon(
          label: const Text('GO'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const MainPage()),
            );
          },
          icon: const Icon(Icons.home),
        ),
      ),
    );
  }
}

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

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> with DisposeBagMixin {
  final controller = TextEditingController();
  late final StateStream<String> stateStream;

  @override
  void initState() {
    super.initState();

    stateStream = controller
        .toValueStream(replayValue: true)
        .map((event) => event.text)
        .debounceTime(const Duration(milliseconds: 500))
        .where((s) => s.isNotEmpty)
        .distinct()
        .switchMap((value) => Stream.periodic(
            const Duration(milliseconds: 500), (i) => '$value..$i'))
        .publishState('initial')
      ..connect().disposedBy(bag);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Main'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            TextField(
              controller: controller,
              decoration: const InputDecoration(filled: true),
            ),
            Expanded(
              child: RxStreamBuilder<String>(
                stream: stateStream,
                builder: (context, state) {
                  return Center(
                    child: Text(
                      state,
                      style: Theme.of(context).textTheme.titleMedium,
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
9
likes
160
pub points
85%
popularity

Publisher

unverified uploader

Convert Listenable (e.g. ChangeNotifier), ValueListenable(e.g. ValueNotifier) to Stream / ValueStream.

Repository (GitHub)
View/report issues

Topics

#rxdart #hoc081098 #reactive-programming #bloc #state-management

Documentation

API reference

Funding

Consider supporting this project:

www.buymeacoffee.com

License

MIT (license)

Dependencies

flutter, rxdart

More

Packages that depend on listenable_stream