saminsohag

textDirection is added to all

... ... @@ -60,6 +60,7 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
TextDirection _direction = TextDirection.ltr;
final TextEditingController _controller = TextEditingController(text: r'''
# hi how are you?$my is^2$
## hi how are you$(\frac ab)^2$?
... ... @@ -97,6 +98,14 @@ $hello$
title: Text(widget.title),
actions: [
IconButton(
onPressed: () {
setState(() {
_direction = TextDirection.values[(_direction.index + 1) % 2];
});
},
icon: const [Text("LTR"), Text("RTL")][_direction.index],
),
IconButton(
onPressed: widget.onPressed,
icon: const Icon(Icons.sunny),
),
... ... @@ -123,9 +132,12 @@ $hello$
LayoutBuilder(builder: (context, constraints) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
reverse: _direction == TextDirection.rtl,
child: SizedBox(
width: 400,
child: TexMarkdown(
_controller.text,
textDirection: TextDirection.rtl,
textDirection: _direction,
onLinkTab: (url, title) {
log(title, name: "title");
log(url, name: "url");
... ... @@ -134,6 +146,7 @@ $hello$
// color: Colors.green,
),
),
),
);
}),
);
... ...
... ... @@ -10,11 +10,17 @@ enum CustomRbSlot {
class CustomRb
extends SlottedMultiChildRenderObjectWidget<CustomRbSlot, RenderBox> {
const CustomRb(
{super.key, this.spacing = 5, required this.child, required this.value});
const CustomRb({
super.key,
this.spacing = 5,
required this.child,
this.textDirection = TextDirection.ltr,
required this.value,
});
final Widget child;
final bool value;
final double spacing;
final TextDirection textDirection;
@override
Widget? childForSlot(CustomRbSlot slot) {
... ... @@ -34,13 +40,14 @@ class CustomRb
@override
SlottedContainerRenderObjectMixin<CustomRbSlot, RenderBox> createRenderObject(
BuildContext context) {
return RenderCustomRb(spacing);
return RenderCustomRb(spacing, textDirection);
}
@override
void updateRenderObject(
BuildContext context, covariant RenderCustomRb renderObject) {
renderObject.spacing = spacing;
renderObject.textDirection = textDirection;
}
@override
... ... @@ -49,8 +56,17 @@ class CustomRb
class RenderCustomRb extends RenderBox
with SlottedContainerRenderObjectMixin<CustomRbSlot, RenderBox> {
RenderCustomRb(this._spacing);
RenderCustomRb(this._spacing, this._textDirection);
double _spacing;
TextDirection _textDirection;
set textDirection(TextDirection value) {
if (_textDirection == value) {
return;
}
_textDirection = value;
markNeedsLayout();
}
set spacing(double value) {
if (_spacing == value) {
return;
... ... @@ -80,6 +96,7 @@ class RenderCustomRb extends RenderBox
body!,
BoxConstraints(
maxWidth: constraints.maxWidth - rbSize.width - _spacing));
if (_textDirection == TextDirection.ltr) {
body!.parentData = BoxParentData()
..offset = Offset(rbSize.width + _spacing, 0);
rb!.parentData = BoxParentData()
... ... @@ -88,6 +105,15 @@ class RenderCustomRb extends RenderBox
body!.computeDistanceToActualBaseline(TextBaseline.alphabetic)! -
rb!.size.height / 1.5,
);
} else {
body!.parentData = BoxParentData()..offset = Offset(-_spacing, 0);
rb!.parentData = BoxParentData()
..offset = Offset(
bodySize.width,
body!.computeDistanceToActualBaseline(TextBaseline.alphabetic)! -
rb!.size.height / 1.5,
);
}
size = constraints.constrain(Size(bodySize.width + rbSize.width + _spacing,
max(rbSize.height, bodySize.height)));
}
... ... @@ -132,8 +158,13 @@ enum CustomCbSlot {
class CustomCb
extends SlottedMultiChildRenderObjectWidget<CustomCbSlot, RenderBox> {
const CustomCb(
{super.key, this.spacing = 5, required this.child, required this.value});
{super.key,
this.spacing = 5,
this.textDirection = TextDirection.ltr,
required this.child,
required this.value});
final Widget child;
final TextDirection textDirection;
final bool value;
final double spacing;
... ... @@ -150,13 +181,14 @@ class CustomCb
@override
SlottedContainerRenderObjectMixin<CustomCbSlot, RenderBox> createRenderObject(
BuildContext context) {
return RenderCustomCb(spacing);
return RenderCustomCb(spacing, textDirection);
}
@override
void updateRenderObject(
BuildContext context, covariant RenderCustomCb renderObject) {
renderObject.spacing = spacing;
renderObject.textDirection = textDirection;
}
@override
... ... @@ -165,8 +197,17 @@ class CustomCb
class RenderCustomCb extends RenderBox
with SlottedContainerRenderObjectMixin<CustomCbSlot, RenderBox> {
RenderCustomCb(this._spacing);
RenderCustomCb(this._spacing, this._textDirection);
double _spacing;
TextDirection _textDirection;
set textDirection(TextDirection value) {
if (_textDirection == value) {
return;
}
_textDirection = value;
markNeedsLayout();
}
set spacing(double value) {
if (_spacing == value) {
return;
... ... @@ -196,6 +237,7 @@ class RenderCustomCb extends RenderBox
body!,
BoxConstraints(
maxWidth: constraints.maxWidth - rbSize.width - _spacing));
if (_textDirection == TextDirection.ltr) {
body!.parentData = BoxParentData()
..offset = Offset(rbSize.width + _spacing, 0);
rb!.parentData = BoxParentData()
... ... @@ -203,6 +245,14 @@ class RenderCustomCb extends RenderBox
0,
body!.computeDistanceToActualBaseline(TextBaseline.alphabetic)! -
rb!.size.height / 1.5);
} else {
body!.parentData = BoxParentData()..offset = Offset(-_spacing, 0);
rb!.parentData = BoxParentData()
..offset = Offset(
bodySize.width,
body!.computeDistanceToActualBaseline(TextBaseline.alphabetic)! -
rb!.size.height / 1.5);
}
size = constraints.constrain(Size(bodySize.width + rbSize.width + _spacing,
max(rbSize.height, bodySize.height)));
}
... ...
... ... @@ -8,10 +8,12 @@ class UnorderedListView extends SingleChildRenderObjectWidget {
this.padding = 10,
this.bulletColor,
this.bulletSize = 4,
this.textDirection = TextDirection.ltr,
required super.child});
final double bulletSize;
final double spacing;
final double padding;
final TextDirection textDirection;
final Color? bulletColor;
@override
... ... @@ -20,6 +22,7 @@ class UnorderedListView extends SingleChildRenderObjectWidget {
spacing,
padding,
bulletColor ?? Theme.of(context).colorScheme.onSurface,
textDirection,
bulletSize,
);
}
... ... @@ -32,6 +35,7 @@ class UnorderedListView extends SingleChildRenderObjectWidget {
renderObject.bulletSize = bulletSize;
renderObject.spacing = spacing;
renderObject.padding = padding;
renderObject.textDirection = textDirection;
}
}
... ... @@ -40,15 +44,18 @@ class UnorderedListRenderObject extends RenderProxyBox {
double spacing,
double padding,
Color bulletColor,
TextDirection textDirection,
this._bulletSize, {
RenderBox? child,
}) : _bulletColor = bulletColor,
_spacing = spacing,
_padding = padding,
_textDirection = textDirection,
super(child);
double _spacing;
double _padding;
Offset _bulletOffset = Offset.zero;
TextDirection _textDirection;
set spacing(double value) {
if (_spacing == value) {
return;
... ... @@ -65,6 +72,15 @@ class UnorderedListRenderObject extends RenderProxyBox {
markNeedsLayout();
}
set textDirection(TextDirection value) {
if (_textDirection == value) {
return;
}
_textDirection = value;
markNeedsLayout();
markNeedsPaint();
}
Color _bulletColor;
double _bulletSize;
set bulletSize(double value) {
... ... @@ -139,10 +155,20 @@ class UnorderedListRenderObject extends RenderProxyBox {
constraints.maxWidth - _spacing - 6 - _bulletSize - _padding,
),
parentUsesSize: true);
if (_textDirection == TextDirection.ltr) {
child!.parentData = BoxParentData()
..offset = Offset(_spacing + _padding + 6 + _bulletSize, 0);
var value = child!.computeDistanceToActualBaseline(TextBaseline.alphabetic);
var value =
child!.computeDistanceToActualBaseline(TextBaseline.alphabetic);
_bulletOffset = Offset(4 + _padding, value! - _bulletSize);
} else {
child!.parentData = BoxParentData()
..offset = Offset(-_spacing - _padding + 6 + _bulletSize, 0);
var value =
child!.computeDistanceToActualBaseline(TextBaseline.alphabetic);
_bulletOffset =
Offset(child!.size.width - 4 + _padding, value! - _bulletSize);
}
size = constraints.constrain(Size(
child!.size.width + _spacing + _padding + 6 + _bulletSize,
child!.size.height));
... ... @@ -153,11 +179,18 @@ class UnorderedListRenderObject extends RenderProxyBox {
if (child == null) {
return;
}
if (_textDirection == TextDirection.ltr) {
context.paintChild(
child!, offset + (child!.parentData as BoxParentData).offset);
context.canvas.drawCircle(
offset + _bulletOffset, _bulletSize, Paint()..color = _bulletColor);
} else {
context.paintChild(
child!, offset + (child!.parentData as BoxParentData).offset);
context.canvas.drawCircle(
offset + _bulletOffset, _bulletSize, Paint()..color = _bulletColor);
}
}
}
class OrderedListView extends SingleChildRenderObjectWidget {
... ... @@ -170,9 +203,11 @@ class OrderedListView extends SingleChildRenderObjectWidget {
this.padding = 10,
TextStyle? style,
required super.child,
this.textDirection = TextDirection.ltr,
required this.no})
: _style = style;
final TextStyle? _style;
final TextDirection textDirection;
TextStyle getStyle(BuildContext context) {
if (_style == null || _style!.inherit) {
... ... @@ -187,6 +222,7 @@ class OrderedListView extends SingleChildRenderObjectWidget {
no,
spacing,
padding,
textDirection,
getStyle(context),
);
}
... ... @@ -198,6 +234,7 @@ class OrderedListView extends SingleChildRenderObjectWidget {
renderObject.spacing = spacing;
renderObject.padding = padding;
renderObject.style = getStyle(context);
renderObject.textDirection = textDirection;
}
}
... ... @@ -206,15 +243,18 @@ class OrderedListRenderObject extends RenderProxyBox {
String no,
double spacing,
double padding,
TextDirection textDirection,
TextStyle style, {
RenderBox? child,
}) : _no = no,
_style = style,
_spacing = spacing,
_padding = padding,
_textDirection = textDirection,
super(child);
double _spacing;
double _padding;
TextDirection _textDirection;
Offset _ptOffset = Offset.zero;
set spacing(double value) {
if (_spacing == value) {
... ... @@ -232,6 +272,15 @@ class OrderedListRenderObject extends RenderProxyBox {
markNeedsLayout();
}
set textDirection(TextDirection value) {
if (_textDirection == value) {
return;
}
_textDirection = value;
markNeedsLayout();
markNeedsPaint();
}
TextStyle _style;
set style(TextStyle value) {
_style = value;
... ... @@ -292,11 +341,21 @@ class OrderedListRenderObject extends RenderProxyBox {
maxWidth: constraints.maxWidth - pt.width - _spacing - _padding,
),
parentUsesSize: true);
if (_textDirection == TextDirection.ltr) {
child!.parentData = BoxParentData()
..offset = Offset(_spacing + _padding + pt.width, 0);
var value = child!.computeDistanceToActualBaseline(TextBaseline.alphabetic);
var value =
child!.computeDistanceToActualBaseline(TextBaseline.alphabetic);
_ptOffset = Offset(_padding,
value! - pt.computeDistanceToActualBaseline(TextBaseline.alphabetic));
} else {
child!.parentData = BoxParentData()
..offset = Offset(-_spacing - _padding + pt.width, 0);
var value =
child!.computeDistanceToActualBaseline(TextBaseline.alphabetic);
_ptOffset = Offset(child!.size.width + _padding - 4,
value! - pt.computeDistanceToActualBaseline(TextBaseline.alphabetic));
}
size = constraints.constrain(Size(
child!.size.width + _spacing + _padding + pt.width,
child!.size.height));
... ...
... ... @@ -275,6 +275,7 @@ class CheckBoxMd extends BlockMd {
var match = exp.firstMatch(text.trim());
return CustomCb(
value: ("${match?[1]}" == "x"),
textDirection: textDirection,
child: MdWidget(
"${match?[2]}",
onLinkTab: onLinkTab,
... ... @@ -309,6 +310,7 @@ class RadioButtonMd extends BlockMd {
var match = exp.firstMatch(text.trim());
return CustomRb(
value: ("${match?[1]}" == "x"),
textDirection: textDirection,
child: MdWidget(
"${match?[2]}",
onLinkTab: onLinkTab,
... ... @@ -343,6 +345,7 @@ class UnOrderedList extends BlockMd {
var match = exp.firstMatch(text.trim());
return UnorderedListView(
bulletColor: style?.color,
textDirection: textDirection,
child: MdWidget(
"${match?[2]}",
onLinkTab: onLinkTab,
... ... @@ -380,6 +383,7 @@ class OrderedList extends BlockMd {
var match = exp.firstMatch(text.trim());
return OrderedListView(
no: "${match?[1]}",
textDirection: textDirection,
style: (style ?? const TextStyle()).copyWith(fontWeight: FontWeight.bold),
child: MdWidget(
"${match?[2]}",
... ...