main.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import 'package:flutter/material.dart';
  2. import 'package:virtual_keyboard_multi_language/virtual_keyboard_multi_language.dart';
  3. import 'package:example/custom_layout.dart';
  4. void main() => runApp(MyApp());
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return MaterialApp(
  9. title: 'Virtual Keyboard Demo',
  10. theme: ThemeData(
  11. primarySwatch: Colors.blue,
  12. ),
  13. home: MyHomePage(title: 'Virtual Keyboard Demo'),
  14. );
  15. }
  16. }
  17. class MyHomePage extends StatefulWidget {
  18. MyHomePage({Key key, this.title}) : super(key: key);
  19. final String title;
  20. @override
  21. _MyHomePageState createState() => _MyHomePageState();
  22. }
  23. class _MyHomePageState extends State<MyHomePage> {
  24. // Holds the text that user typed.
  25. String text = '';
  26. CustomLayoutKeys _customLayoutKeys;
  27. // True if shift enabled.
  28. bool shiftEnabled = false;
  29. // is true will show the numeric keyboard.
  30. bool isNumericMode = false;
  31. TextEditingController _controllerText;
  32. @override
  33. void initState() {
  34. _customLayoutKeys = CustomLayoutKeys();
  35. _controllerText = TextEditingController();
  36. super.initState();
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return Scaffold(
  41. appBar: AppBar(
  42. title: Text(widget.title),
  43. ),
  44. body: Center(
  45. child: Column(
  46. children: <Widget>[
  47. Text(
  48. text,
  49. style: Theme.of(context).textTheme.display1,
  50. ),
  51. Text(
  52. _controllerText.text,
  53. style: TextStyle(color: Colors.red),
  54. ),
  55. SwitchListTile(
  56. title: Text(
  57. 'Keyboard Type = ' +
  58. (isNumericMode
  59. ? 'VirtualKeyboardType.Numeric'
  60. : 'VirtualKeyboardType.Alphanumeric'),
  61. ),
  62. value: isNumericMode,
  63. onChanged: (val) {
  64. setState(() {
  65. isNumericMode = val;
  66. });
  67. },
  68. ),
  69. Expanded(
  70. child: Container(),
  71. ),
  72. Container(
  73. color: Colors.deepPurple,
  74. child: VirtualKeyboard(
  75. height: 300,
  76. //width: 500,
  77. textColor: Colors.white,
  78. textController: _controllerText,
  79. //customLayoutKeys: _customLayoutKeys,
  80. defaultLayouts: [VirtualKeyboardDefaultLayouts.Arabic,VirtualKeyboardDefaultLayouts.English],
  81. //reverseLayout :true,
  82. type: isNumericMode
  83. ? VirtualKeyboardType.Numeric
  84. : VirtualKeyboardType.Alphanumeric,
  85. postKeyPress: _onKeyPress),
  86. )
  87. ],
  88. ),
  89. ),
  90. );
  91. }
  92. /// Fired when the virtual keyboard key is pressed.
  93. _onKeyPress(VirtualKeyboardKey key) {
  94. if (key.keyType == VirtualKeyboardKeyType.String) {
  95. text = text + (shiftEnabled ? key.capsText : key.text);
  96. } else if (key.keyType == VirtualKeyboardKeyType.Action) {
  97. switch (key.action) {
  98. case VirtualKeyboardKeyAction.Backspace:
  99. if (text.length == 0) return;
  100. text = text.substring(0, text.length - 1);
  101. break;
  102. case VirtualKeyboardKeyAction.Return:
  103. text = text + '\n';
  104. break;
  105. case VirtualKeyboardKeyAction.Space:
  106. text = text + key.text;
  107. break;
  108. case VirtualKeyboardKeyAction.Shift:
  109. shiftEnabled = !shiftEnabled;
  110. break;
  111. default:
  112. }
  113. }
  114. // Update the screen
  115. setState(() {});
  116. }
  117. }