Home > Mobile >  Flutter bool is correct but dont change physics?
Flutter bool is correct but dont change physics?

Time:10-19

I want to change my physics of my ListView when the keyboard appears. I did this with the help of flutter_keyboard_visibility. Code for the bool :

class LoginSignUpScreen extends StatefulWidget {
  const LoginSignUpScreen({Key? key}) : super(key: key);

  @override
  State<LoginSignUpScreen> createState() => _LoginSignUpScreenState();
}

class _LoginSignUpScreenState extends State<LoginSignUpScreen> {

  late StreamSubscription<bool> keyboardSubscription;
  bool isKeyboardVisible = false;


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

    var keyboardVisibilityController = KeyboardVisibilityController();
    // Query
    print('Keyboard visibility direct query: ${keyboardVisibilityController.isVisible}');

    // Subscribe
    keyboardSubscription = keyboardVisibilityController.onChange.listen((bool visible) {
      print('Keyboard visibility update. Is visible: $visible');
      isKeyboardVisible = visible;
    });
  }

  @override
  void dispose() {
    keyboardSubscription.cancel();
    super.dispose();
  }

but the bool is correct. It says False when the keyboard is down, and true when it's up. Somewhere here seems to be a mistake, but where?!

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 0.1,
        backgroundColor: Colors.black,
      ),
      body: Stack(
        children: [
          Stack(children: [
            Container(
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  colors: [Color(0xfff68972), Color(0xfff67582)],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
              ),
            ),
            ScrollConfiguration(
              behavior: MyBehavior(),
              child: ListView(
                  controller: controllerV,
                  scrollDirection: Axis.vertical,
                  physics: isKeyboardVisible? const ClampingScrollPhysics(): const NeverScrollableScrollPhysics(),

                  children:const  [
                     LoginForm(),
                     RegisterForm(),


                  ]),
            ),
            ElevatedButton(onPressed: (){print(isKeyboardVisible);}, child: Text("TEST?"))
          ]),
        ],
      ),
    );
  }
}
class MyBehavior extends ScrollBehavior {
  @override
  Widget buildOverscrollIndicator(BuildContext context, Widget child, ScrollableDetails details) {
    return child;
  }
}

in this case the physics is alway Neverscrollable, so the init state, but than the state dont change anymore.

CodePudding user response:

// Subscribe
keyboardSubscription = keyboardVisibilityController.onChange.listen((bool visible) {
print('Keyboard visibility update. Is visible: $visible');
setState(() { isKeyboardVisible = visible; });
});

You need to update the state when it changes

CodePudding user response:

can you try with

setState((){
isKeyboardVisible = visible;
});
  • Related