Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
897 views
in Technique[技术] by (71.8m points)

flutter - TextField stays behind Keyboard when in Focus at BottomSheet on a real device but works on Emulator

I saw some solutions and implemented them. Look Ok on Emulator but doesn't work on Real Device (See screens). So when I click on a textField, the keyboard moves up as per in-focus Textfield and I am able to scroll, however does not happen in real device. Thanks in advance for your time.

BottomSheet launched at FloatinActionButton

showModalBottomSheet(
                isScrollControlled: true,
                backgroundColor: Colors.transparent,
                context: context,
                builder: (context) => SingleChildScrollView(
                  child: Container(
                      child: PostAd(),
                      padding: EdgeInsets.only(
                        bottom: MediaQuery.of(context).viewInsets.bottom,
                      )),
                ),
              );

The method PostAD shows several pages in Container (depending on Index chosen by user)

return Center(
      child: Container(
        height: MediaQuery.of(context).size.height / 1.5,
       
        child: Center(
          child: Container(
           child: Column(
              children: [
                Center(child: radioButtonCreate()), // Radio Buttons to select Page Index
                Expanded(child: kadFormList[formIndex]), //Pages i.e forms that has text field
              ],
            ),
          ),
        ),

enter image description here

question from:https://stackoverflow.com/questions/65874792/textfield-stays-behind-keyboard-when-in-focus-at-bottomsheet-on-a-real-device-bu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import 'package:flutter/material.dart';
import 'package:keyboard_visibility/keyboard_visibility.dart';

main() => runApp(MaterialApp(
  home: Scaffold(
    body: MyForm(),
  ),
));

class MyForm extends StatefulWidget {
  MyForm() : super();

  @override
  State<StatefulWidget> createState() => MyFormState();
}

class MyFormState extends State<MyForm> {
  bool isKeyboardVisible = false;

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

    KeyboardVisibilityNotification().addNewListener(
      onChange: (bool visible) {
        if (!visible) {
          setState(() {
            FocusScope.of(context).unfocus();
          });
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(20.0, 20, 20, 0),
      child: SingleChildScrollView(
        reverse: isKeyboardVisible ? true : false,
        child: Padding(
          padding: const EdgeInsets.fromLTRB(0, 0, 0, 100),
          child: Column(
            children: <Widget>[
              for (int i=0; i<100; i++) TextField()
            ],
          ),
        ),
      ),
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...