Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxCommandListener pass functions after initialization feature enquiry #25

Closed
MillerAdulu opened this issue Aug 20, 2019 · 0 comments
Closed

Comments

@MillerAdulu
Copy link

I am appreciative of the good work on this. Thank you a lot.

I am working on an application which I managed to find a workaround for. Currently, it's not possible to pass functions to some of the RxCommandListerner without going through the constructor as shown in the code sample below. My workaround is that I set a boolean variable in the class which is modified by the RxCommandListener which I then use elsewhere.

import 'package:flutter/material.dart';
import 'package:rx_command/rx_command_listener.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  RxCommandListener _listener1;
  ScrollController _scrollController = ScrollController();

  bool busy;
  bool hasMoreResults;

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

    _listener1 = RxCommandListener(
      myCoolRxCommandViaServiceLocator,
      onNotBusy: () => this.busy = false,
      onIsBusy: () => this.busy = false,
    );

    _scrollController.addListener(() {
      double maxScroll = _scrollController.position.maxScrollExtent;
      double currentScroll = _scrollController.position.pixels;
      double delta = MediaQuery.of(context).size.height * .3;

      if (maxScroll - currentScroll < delta) {
        if (hasMoreResults && busy == false) print("Do some pretty cool stuff");
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _listener1?.dispose();
    _scrollController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Is it possible to have something of this sort where you can pass functions into the listener after it has already been instantiated? If not, what would be recommended?

import 'package:flutter/material.dart';
import 'package:rx_command/rx_command_listener.dart';

class Home2 extends StatefulWidget {
  @override
  _Home2State createState() => _Home2State();
}

class _Home2State extends State<Home2> {
  RxCommandListener _listener1;
  ScrollController _scrollController = ScrollController();

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

    _listener1 = RxCommandListener(myCoolRxCommandViaServiceLocator);

    _scrollController.addListener(() {
      double maxScroll = _scrollController.position.maxScrollExtent;
      double currentScroll = _scrollController.position.pixels;
      double delta = MediaQuery.of(context).size.height * .3;

      if (maxScroll - currentScroll < delta) {
        _listener1.onNotBusy(() {
          print("Do some pretty cool stuff");
        });
        _listener1.onIsBusy(() {
          print("Hold up for cool stuff to end");
        });
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _listener1?.dispose();
    _scrollController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants