mind_map_input.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import 'package:flutter/material.dart';
  2. import 'package:o2oa_all_platform/common/index.dart';
  3. typedef MindMapTextEditDone = Function(String text);
  4. class MindMapTextEdit extends StatefulWidget {
  5. final double inputLeft;
  6. final double inputTop;
  7. final double inputWidth;
  8. final double inputHeight;
  9. final Color inputFillColor;
  10. final bool autoFocus;
  11. final TextSpan? textSpan;
  12. final MindMapTextEditDone textEditDone;
  13. const MindMapTextEdit(
  14. {Key? key,
  15. required this.inputLeft,
  16. required this.inputTop,
  17. required this.inputWidth,
  18. required this.inputHeight,
  19. required this.inputFillColor,
  20. required this.autoFocus,
  21. required this.textSpan,
  22. required this.textEditDone})
  23. : super(key: key);
  24. @override
  25. MindMapState createState() {
  26. return MindMapState();
  27. }
  28. }
  29. class MindMapState extends State<MindMapTextEdit> {
  30. late TextEditingController _controller;
  31. late FocusNode _textNode;
  32. @override
  33. void initState() {
  34. super.initState();
  35. _controller = TextEditingController();
  36. _textNode = FocusNode();
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. final text = widget.textSpan?.text ?? ' ';
  41. _controller.text = text;
  42. _controller.selection = TextSelection(baseOffset: text.length, extentOffset: text.length);
  43. if (widget.autoFocus) {
  44. FocusScope.of(context).requestFocus(_textNode);
  45. }else {
  46. FocusScope.of(context).requestFocus(FocusNode());
  47. }
  48. return Positioned(
  49. left: widget.inputLeft,
  50. top: widget.inputTop,
  51. width: widget.inputWidth,
  52. height: widget.inputHeight,
  53. child: widget.autoFocus ? TextField(
  54. focusNode: _textNode,
  55. style: widget.textSpan?.style,
  56. decoration: InputDecoration.collapsed(
  57. hintText: '',
  58. filled: true,
  59. fillColor: widget.inputFillColor), //去除边框
  60. controller: _controller,
  61. textInputAction: TextInputAction.done,
  62. onEditingComplete: () {
  63. OLogger.d('onEditing complete.....${_controller.text}');
  64. done();
  65. },
  66. ) : Container()
  67. ,
  68. );
  69. }
  70. @override
  71. void dispose() {
  72. _controller.dispose();
  73. _textNode.dispose();
  74. super.dispose();
  75. }
  76. void done(){
  77. widget.textEditDone(_controller.text);
  78. }
  79. }