extended_text
extended official text to build special text like inline image or @somebody quickly,it also support custom background,custom over flow and custom selection toolbar and handles.
speical text
create speical text
extended text helps to convert your text to speical textspan quickly.
for example, follwing code show how to create @xxxx speical textspan.
class attext extends specialtext {
attext(textstyle textstyle, specialtextgesturetapcallback ontap,
{this.showatbackground = false, this.start})
: super(flag, ' ', textstyle, ontap: ontap);
static const string flag = '@';
final int start;
/// whether show background for @somebody
final bool showatbackground;
@override
inlinespan finishtext() {
final textstyle textstyle =
this.textstyle?.copywith(color: colors.blue, fontsize: 16.0);
final string attext = tostring();
return showatbackground
? backgroundtextspan(
background: paint()..color = colors.blue.withopacity(0.15),
text: attext,
actualtext: attext,
start: start,
///caret can move into special text
deleteall: true,
style: textstyle,
recognizer: (tapgesturerecognizer()
..ontap = () {
if (ontap != null) {
ontap(attext);
}
}))
: specialtextspan(
text: attext,
actualtext: attext,
start: start,
style: textstyle,
recognizer: (tapgesturerecognizer()
..ontap = () {
if (ontap != null) {
ontap(attext);
}
}));
}
}
specialtextspanbuilder
create your specialtextspanbuilder
class myspecialtextspanbuilder extends specialtextspanbuilder {
myspecialtextspanbuilder({this.showatbackground = false});
/// whether show background for @somebody
final bool showatbackground;
@override
textspan build(string data,
{textstyle textstyle, specialtextgesturetapcallback ontap}) {
if (kisweb) {
return textspan(text: data, style: textstyle);
}
return super.build(data, textstyle: textstyle, ontap: ontap);
}
@override
specialtext createspecialtext(string flag,
{textstyle textstyle, specialtextgesturetapcallback ontap, int index}) {
if (flag == null || flag == '') {
return null;
}
///index is end index of start flag, so text start index should be index-(flag.length-1)
if (isstart(flag, attext.flag)) {
return attext(
textstyle,
ontap,
start: index - (attext.flag.length - 1),
showatbackground: showatbackground,
);
} else if (isstart(flag, emojitext.flag)) {
return emojitext(textstyle, start: index - (emojitext.flag.length - 1));
} else if (isstart(flag, dollartext.flag)) {
return dollartext(textstyle, ontap,
start: index - (dollartext.flag.length - 1));
}
return null;
}
}
image
imagespan
show inline image by using imagespan.
class imagespan extends extendedwidgetspan {
imagespan(
imageprovider image, {
key key,
@required double imagewidth,
@required double imageheight,
edgeinsets margin,
int start = 0,
ui.placeholderalignment alignment = ui.placeholderalignment.bottom,
string actualtext,
textbaseline baseline,
boxfit fit= boxfit.scaledown,
imageloadingbuilder loadingbuilder,
imageframebuilder framebuilder,
string semanticlabel,
bool excludefromsemantics = false,
color color,
blendmode colorblendmode,
alignmentgeometry imagealignment = alignment.center,
imagerepeat repeat = imagerepeat.norepeat,
rect centerslice,
bool matchtextdirection = false,
bool gaplessplayback = false,
filterquality filterquality = filterquality.low,
gesturetapcallback ontap,
hittestbehavior behavior = hittestbehavior.defertochild,
})
parameter | description | default |
---|---|---|
image | the image to display(imageprovider). | – |
imagewidth | the width of image(not include margin) | required |
imageheight | the height of image(not include margin) | required |
margin | the margin of image | – |
actualtext | actual text, take care of it when enable selection,something likes “[love]” | ‘ufffc’ |
start | start index of text,take care of it when enable selection. | 0 |
selection
parameter | description | default |
---|---|---|
selectionenabled | whether enable selection | false |
selectioncolor | color of selection | theme.of(context).textselectioncolor |
dragstartbehavior | dragstartbehavior for text selection | dragstartbehavior.start |
textselectioncontrols | an interface for building the selection ui, to be provided by the implementor of the toolbar widget or handle widget | extendedmaterialtextselectioncontrols/extendedcupertinotextselectioncontrols |
textselectioncontrols
default value of textselectioncontrols are materialextendedtextselectioncontrols/cupertinoextendedtextselectioncontrols
override buildtoolbar or buildhandle to custom your toolbar widget or handle widget
class myextendedmaterialtextselectioncontrols
extends extendedmaterialtextselectioncontrols {
myextendedmaterialtextselectioncontrols();
@override
widget buildtoolbar(
buildcontext context,
rect globaleditableregion,
double textlineheight,
offset selectionmidpoint,
list<textselectionpoint> endpoints,
textselectiondelegate delegate,
) {}
@override
widget buildhandle(
buildcontext context, textselectionhandletype type, double textheight) {
}
}
control toolbar handle
contain your page into extendedtextselectionpointerhandler, so you can control toolbar and handle.
default behavior
set your page as child of extendedtextselectionpointerhandler
return extendedtextselectionpointerhandler(
//default behavior
child: result,
);
- tap region outside of extended text, hide toolbar and handle
- scorll, hide toolbar and handle
custom behavior
get selectionstates(extendedtextselectionstate) by builder call back, and handle by your self.
return extendedtextselectionpointerhandler(
//default behavior
// child: result,
//custom your behavior
builder: (states) {
return listener(
child: result,
behavior: hittestbehavior.translucent,
onpointerdown: (value) {
for (var state in states) {
if (!state.containsposition(value.position)) {
//clear other selection
state.clearselection();
}
}
},
onpointermove: (value) {
//clear other selection
for (var state in states) {
state.clearselection();
}
},
);
},
);
custom background
refer to issues 24335/24337 about background
backgroundtextspan(
text:
"this text has nice background with borderradius,no mattter how many line,it likes nice",
background: paint()..color = colors.indigo,
clipborderradius: borderradius.all(radius.circular(3.0))),
parameter | description | default |
---|---|---|
background | background painter | – |
clipborderradius | clip borderradius | – |
paintbackground | paint background call back, you can paint background by self | – |
custom overflow
refer to issue 26748
parameter | description | default |
---|---|---|
child | the widget of textoverflow. | @required |
maxheight | the maxheight of [textoverflowwidget], default is preferredlineheight. | preferredlineheight |
align | the align of [textoverflowwidget], left/right. | right |
position | the position which textoverflowwidget should be shown. | textoverflowposition.end |
extendedtext(
overflowwidget: textoverflowwidget(
position: textoverflowposition.end,
align: textoverflowalign.center,
// just for debug
debugoverflowrectcolor: colors.red.withopacity(0.1),
child: container(
child: row(
mainaxissize: mainaxissize.min,
children: <widget>[
const text('u2026 '),
inkwell(
child: const text(
'more',
),
ontap: () {
launch(
'https://github.com/fluttercandies/extended_text');
},
)
],
),
),
),
)
join zero-width space
refer to issue 18761
if [extendedtext.joinzerowidthspace] is true, it will join ‘u{200b}’ into text, make line breaking and overflow style better.
extendedtext(
joinzerowidthspace: true,
)
or you can convert by following method:
- string
string input='abc'.joinchar();
- inlinespan
inlinespan innertextspan;
innertextspan = joinchar(
innertextspan,
accumulator(),
zerowidthspace,
);
take care of following things:
- the word is not a word, it will not working when you want to double tap to select a word.
- text is changed, if [extendedtext.selectionenabled] is true, you should override textselectioncontrols and remove zerowidthspace.
class mytextselectioncontrols extends textselectioncontrols {
@override
void handlecopy(textselectiondelegate delegate,
clipboardstatusnotifier? clipboardstatus) {
final texteditingvalue value = delegate.texteditingvalue;
string data = value.selection.textinside(value.text);
// remove zerowidthspace
data = data.replaceall(zerowidthspace, '');
clipboard.setdata(clipboarddata(
text: value.selection.textinside(value.text),
));
clipboardstatus?.update();
delegate.texteditingvalue = texteditingvalue(
text: value.text,
selection: textselection.collapsed(offset: value.selection.end),
);
delegate.bringintoview(delegate.texteditingvalue.selection.extent);
delegate.hidetoolbar();
}
}
Comments are closed.