Created
January 15, 2021 17:26
-
-
Save unacorbatanegra/06ff44eec14b6365551572e32f7743d1 to your computer and use it in GitHub Desktop.
Above the keyboard widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
void main() { | |
runApp( | |
GetMaterialApp( | |
initialRoute: '/', | |
initialBinding: BindingsBuilder.put(() => HomeController()), | |
getPages: [ | |
GetPage( | |
name: '/', | |
page: () => Home(), | |
) | |
], | |
), | |
); | |
} | |
class Home extends GetView<HomeController> { | |
const Home({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('keyboard'), | |
), | |
resizeToAvoidBottomInset: false, | |
body: Obx( | |
() => Stack( | |
children: [ | |
Container( | |
child: Center( | |
child: TextFormField( | |
decoration: InputDecoration( | |
border: OutlineInputBorder( | |
borderRadius: BorderRadius.circular(4.0), | |
borderSide: BorderSide( | |
color: Colors.grey, | |
), | |
), | |
contentPadding: const EdgeInsets.all(18.0), | |
hintText: 'text form', | |
hintStyle: const TextStyle(color: Colors.grey), | |
)), | |
), | |
), | |
Visibility( | |
visible: controller.isKeyboardOpen, | |
child: Positioned( | |
bottom: MediaQuery.of(context).viewInsets.bottom, | |
left: 0, | |
right: 0, | |
child: Container( | |
height: 50, | |
child: Center( | |
child: Text( | |
'above keyboard', | |
style: TextStyle(color: Colors.white), | |
)), | |
decoration: BoxDecoration(color: Colors.black), | |
), | |
), | |
) | |
], | |
), | |
), | |
); | |
} | |
} | |
class HomeController extends GetxController with WidgetsBindingObserver { | |
final _isKeyboardOpen = true.obs; | |
@override | |
void onInit() { | |
super.onInit(); | |
WidgetsBinding.instance.addObserver(this); | |
} | |
bool get isKeyboardOpen => _isKeyboardOpen.value; | |
@override | |
void onClose() { | |
WidgetsBinding.instance.removeObserver(this); | |
super.onClose(); | |
} | |
@override | |
void didChangeMetrics() { | |
super.didChangeMetrics(); | |
_isKeyboardOpen.value = | |
WidgetsBinding.instance.window.viewInsets.bottom > 0.0; | |
print(isKeyboardOpen); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment