Inside the TextFormField I am using the following to display a numeric keypad and only allow digits without decimals:
keyboardType: TextInputType.numberWithOptions(decimal:false),
inputFormatters: [
FilteredTextInputFormatter.digitsOnly,
FilteredTextInputFormatter.singleLineFormatter,
]
On mobile phones this works very well, however I am testing on a 10-inch Android tablet and I see the numeric keypad on the right side and an arithmetic operations keyboard on the left side that becomes useless and inconvenient.
How can I make only the numeric keypad show?
In Flutter, the display of the keyboard, including its layout and additional panels like the arithmetic operations keyboard you’re seeing, is largely controlled by the operating system and can vary based on the device. Specifically for Android, the appearance of the numeric keypad can differ between devices and Android versions, and unfortunately, Flutter doesn’t provide direct control over these aspects of the keyboard UI.
However, there are a couple of approaches you could consider to try to manage or mitigate this issue:
Custom Keyboard: Implement a custom numeric keyboard within your app. This gives you complete control over the keyboard layout and functionality. This can be more complex to implement but ensures a consistent user experience across all devices.
Third-Party Packages: Explore third-party packages that might offer more refined control over the keyboard appearance or provide a custom keyboard solution. For example, packages like flutter_keyboard_visibility or keyboard_actions could be used to enhance the keyboard interaction, though they might not directly solve the specific issue of hiding the operations panel.
Feedback to Users: If a custom keyboard solution isn’t suitable, and no third-party options suffice, consider providing user guidance or feedback. For example, if the operations panel is unavoidable due to the device’s default behavior, you might instruct users on how to best use the available interface or provide an app setting to toggle between different input methods if feasible.
Here’s a basic example of how you might start implementing a simple custom numeric keypad in Flutter, which you can expand upon based on your specific needs:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: NumericKeyboardExample()));
}
class NumericKeyboardExample extends StatefulWidget {
@override
_NumericKeyboardExampleState createState() => _NumericKeyboardExampleState();
}
class _NumericKeyboardExampleState extends State<NumericKeyboardExample> {
String input = '';
void _handleKeyPress(String key) {
setState(() {
input += key; // Append key to the current input string
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Custom Numeric Keyboard"),
),
body: Column(
children: [
Expanded(
child: Container(
alignment: Alignment.centerRight,
padding: EdgeInsets.all(20),
child: Text(input, style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
),
),
GridView.count(
shrinkWrap: true,
crossAxisCount: 3,
childAspectRatio: 2,
padding: EdgeInsets.all(20),
children: List.generate(9, (index) {
return ElevatedButton(
onPressed: () => _handleKeyPress('${index + 1}'),
child: Text('${index + 1}', style: TextStyle(fontSize: 20)),
);
})
..add(
ElevatedButton(
onPressed: () => setState(() {
input = input.substring(0, input.length - 1); // Implement backspace
}),
child: Icon(Icons.backspace),
),
)
..add(
ElevatedButton(
onPressed: () => _handleKeyPress('0'),
child: Text('0', style: TextStyle(fontSize: 20)),
),
)
..add(
ElevatedButton(
onPressed: () => setState(() {
input = ''; // Clear input
}),
child: Icon(Icons.clear),
),
),
),
],
),
);
}
}
Bernard Aybout is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.