-
-
Save talamaska/35a1172f43496e835d273fd34e559f14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: Text('Sample App')), | |
body: CustomEditableText(), | |
), | |
); | |
} | |
} | |
class CustomEditableText extends StatefulWidget { | |
@override | |
_CustomEditableTextState createState() => _CustomEditableTextState(); | |
} | |
class _CustomEditableTextState extends State<CustomEditableText> { | |
final NoUnderlineTextEditingController _controller = | |
NoUnderlineTextEditingController(decorationColor: Colors.green); | |
@override | |
Widget build(BuildContext context) { | |
return Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: EditableText( | |
controller: _controller, | |
focusNode: FocusNode(), | |
style: TextStyle(fontSize: 18, color: Colors.black), | |
cursorColor: Colors.blue, | |
backgroundCursorColor: Colors.grey, | |
keyboardType: TextInputType.multiline, | |
minLines: 1, | |
maxLines: null, | |
autocorrect: true, | |
), | |
); | |
} | |
} | |
class NoUnderlineTextEditingController extends TextEditingController { | |
final Color decorationColor; | |
NoUnderlineTextEditingController({required this.decorationColor}); | |
TextSpan buildTextSpan( | |
{required BuildContext context, | |
TextStyle? style, | |
required bool withComposing}) { | |
assert(!value.composing.isValid || | |
!withComposing || | |
value.isComposingRangeValid); | |
final bool composingRegionOutOfRange = | |
!value.isComposingRangeValid || !withComposing; | |
if (composingRegionOutOfRange) { | |
return TextSpan(style: style, text: text); | |
} | |
final TextStyle composingStyle = style?.merge(TextStyle( | |
decoration: TextDecoration.underline, | |
decorationColor: decorationColor, | |
)) ?? | |
TextStyle( | |
decoration: TextDecoration.underline, | |
decorationColor: decorationColor); | |
return TextSpan( | |
style: style, | |
children: <TextSpan>[ | |
TextSpan(text: value.composing.textBefore(value.text)), | |
TextSpan( | |
style: composingStyle, | |
text: value.composing.textInside(value.text), | |
), | |
TextSpan(text: value.composing.textAfter(value.text)), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment