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

Cant trigger listener in my own code #1

Closed
Rockvole opened this issue Dec 7, 2018 · 13 comments
Closed

Cant trigger listener in my own code #1

Rockvole opened this issue Dec 7, 2018 · 13 comments
Assignees

Comments

@Rockvole
Copy link

Rockvole commented Dec 7, 2018

Hi,
Thanks for the plugin.

I can see that your example works. When I try to copy / paste the example code into my own code and include your library it doesn't seem to ever call the listener.
In the listener, I just put a print statement to show the $visible value but it never seems to be called.
Is there anything else I need to do to enable it ?

@Rockvole
Copy link
Author

Rockvole commented Dec 7, 2018

I made a new project just to try your keyboard :

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

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
	return MaterialApp(
	  title: 'Flutter Demo',
	  theme: ThemeData(

		primarySwatch: Colors.blue,
	  ),
	  home: MyHomePage(title: 'Flutter Demo Home Page'),
	);
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  KeyboardVisibilityNotification _keyboardVisibility = new KeyboardVisibilityNotification();
  int _keyboardVisibilitySubscriberId;
  bool _keyboardState;

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

	_keyboardState = _keyboardVisibility.isKeyboardVisible;

	_keyboardVisibilitySubscriberId = _keyboardVisibility.addNewListener(
	  onChange: (bool visible) {
		print("Keyboard visible=$visible");
		setState(() {
		  _keyboardState = visible;
		});
	  },
	);
  }

  @override
  void dispose() {
	_keyboardVisibility.removeListener(_keyboardVisibilitySubscriberId);
  }

  @override
  Widget build(BuildContext context) {
	return Scaffold(
	  appBar: AppBar(
		title: Text(widget.title),
	  ),
	  body: Center(
		child: Column(
		  mainAxisAlignment: MainAxisAlignment.center,
		  children: <Widget>[
			Text(
			  'You have pushed the button this many times:',
			),
			TextField(
			  keyboardType: TextInputType.text,
			  decoration: InputDecoration(
				labelText: 'Input box for keyboard test',
			  ),
			),
		  ],
		),
	  ),
	);
  }
}

@Rockvole
Copy link
Author

Rockvole commented Dec 7, 2018

In my pubspec.yaml I have :

dev_dependencies:
  flutter_test:
    sdk: flutter
  keyboard_visibility:
      path: ../flutter_keyboard_visibility

It still doesnt work even with that basic example. Is there an android setting in the Manifest or something which is also needed ?
Thanks.

@CosmicPangolin
Copy link

@Rockvole @adee42 Did you ever make any progress on this? I'm having a similar problem - the listener seems to work in all of my app pages except one...and I have noooo idea why that could be.

@adee42
Copy link
Owner

adee42 commented Jan 6, 2019

Hi man, sorry for the late reply,

@Rockvole: no, there are no further settings needed. I'll check your example

@CosmicPangolin: do you have any further information on what happens on this specific page? Can you provide any example? How are you testing? Android or iOS?

@adee42 adee42 self-assigned this Jan 6, 2019
@adee42
Copy link
Owner

adee42 commented Jan 6, 2019

@Rockvole: I checked your example on Android emulator, it works. Can you provide more information on your set up?

@CosmicPangolin
Copy link

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

void main() => runApp(KeyboardApp());

class KeyboardApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Keyboard failz example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: KeyboardFailzExample(),
    );
  }
}

class KeyboardFailzExample extends StatefulWidget {
  KeyboardFailzExampleState createState() => KeyboardFailzExampleState();
}

class KeyboardFailzExampleState extends State<KeyboardFailzExample> {

  KeyboardVisibilityNotification _keyboardVisibility =
  KeyboardVisibilityNotification();
  int _keyboardVisibilitySubscriberId;
  bool _keyboardState;

  @override
  void initState() {
    super.initState();
    print('Scaffold init');


    _keyboardState = _keyboardVisibility.isKeyboardVisible;
    print(_keyboardVisibility.isKeyboardVisible);

    _keyboardVisibilitySubscriberId = _keyboardVisibility.addNewListener(
      onChange: (bool visible) {
        setState(() {
          _keyboardState = visible;
          print('SCAFFOLD');
          print(_keyboardState);
        });
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
    _keyboardVisibility.removeListener(_keyboardVisibilitySubscriberId);
  }

  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        bottomNavigationBar: _keyboardState == false ? TestBar()
            : Transform.translate(
          offset: Offset(
              0.0, -1 * MediaQuery.of(context).viewInsets.bottom),
          child: TestBar(),
        ),
        body: CustomScrollView(
          slivers: <Widget>[
            SliverFillRemaining(),
          ],
        ),
      ),
    );
  }
}

class TestBar extends StatefulWidget {

  TestBarState createState() => TestBarState();
}

class TestBarState extends State<TestBar> {

  KeyboardVisibilityNotification _keyboardVisibility =
  KeyboardVisibilityNotification();
  int _keyboardVisibilitySubscriberId;
  bool _keyboardState;

  @override
  void initState() {
    super.initState();
    print('Bar Init');

    _keyboardState = _keyboardVisibility.isKeyboardVisible;
    print(_keyboardVisibility.isKeyboardVisible);

    _keyboardVisibilitySubscriberId = _keyboardVisibility.addNewListener(
      onChange: (bool visible) {
        setState(() {
          _keyboardState = visible;
          print('BAR');
          print(_keyboardState);
        });
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
    _keyboardVisibility.removeListener(_keyboardVisibilitySubscriberId);
  }


  Widget build(BuildContext context) {
    return BottomAppBar(
      child: TextField(),
    );
  }
}

@adee42
Copy link
Owner

adee42 commented Jan 6, 2019

OK, I will have a look

@CosmicPangolin
Copy link

CosmicPangolin commented Jan 6, 2019

@adee42 Check out that example. 'SCAFFOLD' never gets printed. From my testing so far, both KeyboardVisibilityNotification constructors run just fine, with _keyboardVisibilitySubscription assigned as 'Instance of _BroadcastSubscription'. However, onChange is never called for the Scaffold class.

Interestingly, this issue seems somehow linked to composition...two distinct classes within scaffold (BottomAppBar and some sliver) can have working KeyboardVisibilityNotifications.

@adee42
Copy link
Owner

adee42 commented Jan 6, 2019

If I see correctly, you have two listeners running at the same time. Only one of them (the latter) is triggered

@adee42
Copy link
Owner

adee42 commented Jan 6, 2019

OK, I found the bug and fixed it. The new version 0.5.1 is uploaded and ready to use.

@CosmicPangolin: You have a bug in your code, though. When you update the page, the TextField loses focus which closes the keyboard But given this is only an example, I believe you will get it fixed in the final application

@adee42 adee42 closed this as completed Jan 6, 2019
@adee42 adee42 reopened this Jan 6, 2019
@CosmicPangolin
Copy link

Yeaa see the fix, that makes sense. I was over here digging into Broadcast streams being like 'but whyyy wont it take multiple listeners'... :)

No doubt on the bug; the actual codebase is full of components and state management...hence the multiple listeners; I really didn't want to have to dispatch actions and juggle a state object for keyboard stuffs.

Thanks for the quick work, this was blocking me all morning!

@adee42
Copy link
Owner

adee42 commented Jan 6, 2019

@CosmicPangolin: Yes, in Flutter only one EventChannel receives the actual message from Android or iOS. That is why I had to use the internal list of listeners to allow multiple listeners. In my own code I used a 'global' variable, that is why I didn't encounter the same problem as you did.

I am happy I could help. Enjoy!

@adee42
Copy link
Owner

adee42 commented Feb 4, 2019

This issue has been open for some time without any updates, please re-open if you still experience it

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

3 participants