keyboard_controller.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. part of cool_ui;
  2. class KeyboardController extends ValueNotifier<TextEditingValue>{
  3. final InputClient client;
  4. KeyboardController({TextEditingValue value,this.client})
  5. : super(value == null ? TextEditingValue.empty : value);
  6. /// The current string the user is editing.
  7. String get text => value.text;
  8. /// Setting this will notify all the listeners of this [TextEditingController]
  9. /// that they need to update (it calls [notifyListeners]). For this reason,
  10. /// this value should only be set between frames, e.g. in response to user
  11. /// actions, not during the build, layout, or paint phases.
  12. set text(String newText) {
  13. value = value.copyWith(text: newText,
  14. selection: const TextSelection.collapsed(offset: -1),
  15. composing: TextRange.empty);
  16. }
  17. /// The currently selected [text].
  18. ///
  19. /// If the selection is collapsed, then this property gives the offset of the
  20. /// cursor within the text.
  21. TextSelection get selection => value.selection;
  22. /// Setting this will notify all the listeners of this [TextEditingController]
  23. /// that they need to update (it calls [notifyListeners]). For this reason,
  24. /// this value should only be set between frames, e.g. in response to user
  25. /// actions, not during the build, layout, or paint phases.
  26. set selection(TextSelection newSelection) {
  27. if (newSelection.start > text.length || newSelection.end > text.length)
  28. throw FlutterError('invalid text selection: $newSelection');
  29. value = value.copyWith(selection: newSelection, composing: TextRange.empty);
  30. }
  31. /// Set the [value] to empty.
  32. ///
  33. /// After calling this function, [text] will be the empty string and the
  34. /// selection will be invalid.
  35. ///
  36. /// Calling this will notify all the listeners of this [TextEditingController]
  37. /// that they need to update (it calls [notifyListeners]). For this reason,
  38. /// this method should only be called between frames, e.g. in response to user
  39. /// actions, not during the build, layout, or paint phases.
  40. void clear() {
  41. value = TextEditingValue.empty;
  42. }
  43. /// Set the composing region to an empty range.
  44. ///
  45. /// The composing region is the range of text that is still being composed.
  46. /// Calling this function indicates that the user is done composing that
  47. /// region.
  48. ///
  49. /// Calling this will notify all the listeners of this [TextEditingController]
  50. /// that they need to update (it calls [notifyListeners]). For this reason,
  51. /// this method should only be called between frames, e.g. in response to user
  52. /// actions, not during the build, layout, or paint phases.
  53. clearComposing() {
  54. value = value.copyWith(composing: TextRange.empty);
  55. }
  56. ///删除一个字符,一般用于键盘的删除键
  57. deleteOne(){
  58. if(selection.baseOffset == 0)
  59. return;
  60. String newText = '';
  61. if(selection.baseOffset != selection.extentOffset)
  62. {
  63. newText = selection.textBefore(text) + selection.textAfter(text);
  64. value = TextEditingValue(
  65. text: newText,
  66. selection: selection.copyWith(
  67. baseOffset:selection.baseOffset,
  68. extentOffset: selection.baseOffset)
  69. );
  70. }else{
  71. newText = text.substring(0,selection.baseOffset - 1) + selection.textAfter(text);
  72. value = TextEditingValue(
  73. text: newText,
  74. selection: selection.copyWith(
  75. baseOffset:selection.baseOffset - 1,
  76. extentOffset: selection.baseOffset - 1)
  77. );
  78. }
  79. }
  80. /// 在光标位置添加文字,一般用于键盘输入
  81. addText(String insertText){
  82. String newText = selection.textBefore(text) + insertText + selection.textAfter(text);
  83. value = TextEditingValue(
  84. text: newText,
  85. selection: selection.copyWith(
  86. baseOffset:selection.baseOffset + insertText.length,
  87. extentOffset: selection.baseOffset + insertText.length)
  88. );
  89. }
  90. /// 完成
  91. doneAction(){
  92. CoolKeyboard.sendPerformAction(TextInputAction.done);
  93. }
  94. /// 下一个
  95. nextAction(){
  96. CoolKeyboard.sendPerformAction(TextInputAction.next);
  97. }
  98. /// 换行
  99. newLineAction(){
  100. CoolKeyboard.sendPerformAction(TextInputAction.newline);
  101. }
  102. ///发送其他Action
  103. sendPerformAction(TextInputAction action){
  104. CoolKeyboard.sendPerformAction(action);
  105. }
  106. }