-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cat-voices): PlaceholderRichText and TokenValue helper handles n…
…ullable range (#1489) * feat: PlaceholderRichText and TokenValue helper handles nullable range * chore: clean up * chore: fix import * chore: add or equal to loc file * chore: adjust strings
- Loading branch information
1 parent
4908eeb
commit 771ee64
Showing
6 changed files
with
137 additions
and
31 deletions.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
catalyst_voices/apps/voices/lib/widgets/rich_text/placeholder_rich_text.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
final _kPlaceholderRegExp = RegExp(r'{(\w*)}'); | ||
|
||
typedef PlaceholderSpanBuilder = InlineSpan Function( | ||
BuildContext context, | ||
String placeholder, | ||
); | ||
|
||
class PlaceholderRichText extends StatefulWidget { | ||
final String text; | ||
final PlaceholderSpanBuilder placeholderSpanBuilder; | ||
final TextStyle? style; | ||
final TextAlign textAlign; | ||
|
||
const PlaceholderRichText( | ||
this.text, { | ||
super.key, | ||
required this.placeholderSpanBuilder, | ||
this.style, | ||
this.textAlign = TextAlign.start, | ||
}); | ||
|
||
@override | ||
State<PlaceholderRichText> createState() => _PlaceholderRichTextState(); | ||
} | ||
|
||
class _PlaceholderRichTextState extends State<PlaceholderRichText> { | ||
List<InlineSpan> _spans = []; | ||
|
||
@override | ||
void didChangeDependencies() { | ||
super.didChangeDependencies(); | ||
|
||
// Context colors may have changed, rebuild it. | ||
_spans = _calculateSpans(); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(covariant PlaceholderRichText oldWidget) { | ||
super.didUpdateWidget(oldWidget); | ||
|
||
if (widget.text != oldWidget.text) { | ||
_spans = _calculateSpans(); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Text.rich( | ||
TextSpan(children: _spans), | ||
textAlign: widget.textAlign, | ||
style: widget.style, | ||
); | ||
} | ||
|
||
List<InlineSpan> _calculateSpans() { | ||
final text = widget.text; | ||
final spans = <InlineSpan>[]; | ||
|
||
text.splitMapJoin( | ||
_kPlaceholderRegExp, | ||
onMatch: (match) { | ||
final placeholder = match.group(0)!; | ||
// removes "{" and "}" | ||
final key = placeholder.substring(1, placeholder.length - 1); | ||
|
||
final span = widget.placeholderSpanBuilder(context, key); | ||
|
||
spans.add(span); | ||
|
||
return ''; | ||
}, | ||
onNonMatch: (match) { | ||
spans.add(TextSpan(text: match)); | ||
return ''; | ||
}, | ||
); | ||
|
||
return spans; | ||
} | ||
} |
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
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
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
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