Skip to content

Instantly share code, notes, and snippets.

@rekire
Created September 8, 2023 16:52
Show Gist options
  • Save rekire/0ab11b3c770fcba750db66183bd18aff to your computer and use it in GitHub Desktop.
Save rekire/0ab11b3c770fcba750db66183bd18aff to your computer and use it in GitHub Desktop.
Demonstration of "my" keyboard bug
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _focus = FocusNode();
final _controller = TextEditingController();
bool _allowLetters = true;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Keyboard bug',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Keyboard bug'),
backgroundColor: Colors.blue,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
style: Theme.of(context).textTheme.bodySmall,
'Please tap into the input field below, hide the keyboard '
'and press any key on your keyboard. When writing this app '
'the on Screen keyboard opens which is not an expected '
'behavior.\n\nAttentional this does not happen when the '
'keyboardType is number.\n\nThis is a relevant case for '
'devices with physical scanners.'),
TextField(
focusNode: _focus,
controller: _controller,
keyboardType:
_allowLetters ? TextInputType.text : TextInputType.number,
autofocus: true,
decoration: InputDecoration(
hintText: 'Room for your input',
suffixIcon: IconButton(
icon: Icon(
_allowLetters
? Icons.font_download_outlined
: Icons.font_download_off_outlined,
),
onPressed: () {
_focus.unfocus();
setState(() {
_allowLetters = !_allowLetters;
WidgetsBinding.instance
.addPostFrameCallback((_) => _focus.requestFocus());
});
},
),
),
),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment