Created
August 8, 2024 22:50
-
-
Save justinmc/8d9798202ad3cae20f0408c59b95abc9 to your computer and use it in GitHub Desktop.
Simple example of rich text in a TextField by overriding TextEditingController
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'; | |
// This example shows how to format input text in a TextField using TextSpans. | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Rich TextEditingController Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Rich TextEditingController Demo'), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
final _BigBoldTextEditingController _bigBoldTextEditingController = _BigBoldTextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
TextField( | |
decoration: const InputDecoration( | |
hintText: 'Type something with the word "big"', | |
), | |
controller: _bigBoldTextEditingController, | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
/// A [TextEditingController] that makes the string "big" appear bold whenever | |
// it appears. | |
/// | |
/// Doesn't bother with the default behavior of | |
/// [TextEditingController.buildTextSpan], which styles composing regions. | |
class _BigBoldTextEditingController extends TextEditingController { | |
static const String _stringToEmbolden = 'big'; | |
static const TextStyle _emboldenedTextStyle = TextStyle( | |
fontWeight: FontWeight.w900, | |
); | |
@override | |
TextSpan buildTextSpan({required BuildContext context, TextStyle? style, required bool withComposing}) { | |
return _embolden(text, style); | |
} | |
static TextSpan _embolden(String text, TextStyle? style) { | |
final int index = text.indexOf(_stringToEmbolden); | |
if (index < 0) { | |
return TextSpan( | |
text: text, | |
style: style, | |
); | |
} | |
final String before = text.substring(0, index); | |
final String after = text.substring(index + _stringToEmbolden.length); | |
final TextStyle emboldenedTextStyle = style == null | |
? _emboldenedTextStyle | |
: style.copyWith(fontWeight: FontWeight.bold); | |
return TextSpan( | |
text: before, | |
style: style, | |
children: <TextSpan>[ | |
TextSpan( | |
text: _stringToEmbolden, | |
style: emboldenedTextStyle, | |
), | |
_embolden(after, style), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment