keyboard_controller.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. deleteOne(){
  57. if(selection.baseOffset == 0)
  58. return;
  59. String newText = '';
  60. if(selection.baseOffset != selection.extentOffset)
  61. {
  62. newText = selection.textBefore(text) + selection.textAfter(text);
  63. value = TextEditingValue(
  64. text: newText,
  65. selection: selection.copyWith(
  66. baseOffset:selection.baseOffset,
  67. extentOffset: selection.baseOffset)
  68. );
  69. }else{
  70. newText = text.substring(0,selection.baseOffset - 1) + selection.textAfter(text);
  71. value = TextEditingValue(
  72. text: newText,
  73. selection: selection.copyWith(
  74. baseOffset:selection.baseOffset - 1,
  75. extentOffset: selection.baseOffset - 1)
  76. );
  77. }
  78. }
  79. /// 在光标位置添加文字,一般用于键盘输入
  80. addText(String insertText){
  81. String newText = selection.textBefore(text) + insertText + selection.textAfter(text);
  82. value = TextEditingValue(
  83. text: newText,
  84. selection: selection.copyWith(
  85. baseOffset:selection.baseOffset + insertText.length,
  86. extentOffset: selection.baseOffset + insertText.length)
  87. );
  88. }
  89. /// 完成
  90. doneAction(){
  91. CoolKeyboard.sendPerformAction(TextInputAction.done);
  92. }
  93. }