1. Positioning Widget (Application scenario: The login button is blocked by the pop-up keyboard, and the whole page moves up when you want to pop-up the keyboard)

Initialize a GlobalKey

GlobalKey _globalKey = GlobalKey();
Copy the code

2. Control to listen on (login button)

Container(key:_globalKey, child: Text(" Login button "),Copy the code

3. Get the properties of the control (login button)

/ / listen for widget final RenderBox box. = _globalKey currentContext. FindRenderObject (); Final topLeftPosition = box. LocalToGlobal (offset.zero); Margin-top - own height final bottom = SS().screenheight - (topleftPosition.dy + SS().h(80));Copy the code

Second, obtain the keyboard height, and position the control position, through scrollView sliding to avoid the keyboard block control

class _RegisterTelPageState extends State<RegisterTelPage> with WidgetsBindingObserver { ScrollController _scrollController = ScrollController(); GlobalKey _globalKey = GlobalKey(); @override void didChangeMetrics() { // TODO: implement didChangeMetrics super.didChangeMetrics(); WidgetsBinding. Instance. AddPostFrameCallback ((timeStamp) {/ / pop-up keyboard height double height = MediaQuery.of(context).viewInsets.bottom; / / listen for widget final RenderBox box. = _globalKey currentContext. FindRenderObject (); Final topLeftPosition = box. LocalToGlobal (offset.zero); Final bottom = SS().screenheight - (topleftPosition.dy + SS().h(80)); If (height == 0){_scrollController.jumpto (0); } else {// If (height-bottom > 0) {// Let scrollView slide up, Sliding distance to control the keyboard _scrollController. AnimateTo (height - bottom, duration: the duration (milliseconds: 100), the curve: Curves.easeInSine); }}}); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); }}Copy the code

The page is wrapped in SingleChildScrollview.

SingleChildScrollView(
   controller: _scrollController,
   scrollDirection: Axis.vertical,
   physics: ClampingScrollPhysics(),
   child:Container(),
}
   
   
   
Copy the code