Skip to content

Instantly share code, notes, and snippets.

@craiglabenz
Created January 11, 2020 16:53
Show Gist options
  • Save craiglabenz/4ac72794729bf4e676f763d8a32f4511 to your computer and use it in GitHub Desktop.
Save craiglabenz/4ac72794729bf4e676f763d8a32f4511 to your computer and use it in GitHub Desktop.
Flutter fails to report keyboard size via `MediaQuery.of(context).size`
import 'package:flutter/material.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: Scaffold(
appBar: AppBar(
title: Text('ViewInsets Demo'),
),
body: InputsPage(),
),
);
}
}
class InputsPage extends StatefulWidget {
InputsPage({Key key}) : super(key: key);
@override
_InputsPageState createState() => _InputsPageState();
}
class _InputsPageState extends State<InputsPage> {
FocusNode usernameFocusNode;
FocusNode passwordFocusNode;
@override
void initState() {
super.initState();
usernameFocusNode = FocusNode();
passwordFocusNode = FocusNode();
usernameFocusNode.addListener(() {
print(MediaQuery.of(context).size);
print(MediaQuery.of(context).viewInsets);
print(MediaQuery.of(context).viewInsets.bottom);
});
passwordFocusNode.addListener(() {
print(MediaQuery.of(context).size);
print(MediaQuery.of(context).viewInsets);
print(MediaQuery.of(context).viewInsets.bottom);
});
}
@override
void dispose() {
usernameFocusNode.dispose();
passwordFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
hintText: 'Username',
),
focusNode: usernameFocusNode,
),
SizedBox(height: 100),
TextFormField(
decoration: InputDecoration(
hintText: 'Password',
),
focusNode: passwordFocusNode,
obscureText: true,
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment