Line data Source code
1 : // Copyright 2014 The Flutter Authors.
2 : // Copyright 2021 Suragch.
3 : // All rights reserved.
4 : // Use of this source code is governed by a BSD-style license that can be
5 : // found in the LICENSE file.
6 :
7 : import 'package:flutter/foundation.dart';
8 : import 'package:flutter/widgets.dart';
9 :
10 : import 'mongol_render_paragraph.dart';
11 : import '../base/mongol_text_align.dart';
12 :
13 : /// A string of rich text in vertical Mongolian layout.
14 : ///
15 : /// Based on RichText of Flutter version 1.5. After that RichText became a
16 : /// MultiChildRenderObjectWidget in order to support InlineSpans.
17 : ///
18 : /// The [MongolRichText] widget displays text that uses multiple different styles. The
19 : /// text to display is described using a tree of [TextSpan] objects, each of
20 : /// which has an associated style that is used for that subtree. The text might
21 : /// break across multiple lines or might all be displayed on the same line
22 : /// depending on the layout constraints.
23 : ///
24 : /// Text displayed in a [MongolRichText] widget must be explicitly styled. When
25 : /// picking which style to use, consider using [DefaultTextStyle.of] the current
26 : /// [BuildContext] to provide defaults. For more details on how to style text in
27 : /// a [MongolRichText] widget, see the documentation for [TextStyle].
28 : ///
29 : /// Consider using the [MongolText] widget to integrate with the [DefaultTextStyle]
30 : /// automatically. When all the text uses the same style, the default constructor
31 : /// is less verbose. The [MongolText.rich] constructor allows you to style multiple
32 : /// spans with the default text style while still allowing specified styles per
33 : /// span.
34 : ///
35 : /// {@tool snippet}
36 : ///
37 : /// This sample demonstrates how to mix and match text with different text
38 : /// styles using the [MongolRichText] Widget. It displays the text "Hello bold world,"
39 : /// emphasizing the word "bold" using a bold font weight.
40 : ///
41 : /// ```dart
42 : /// MongolRichText(
43 : /// text: TextSpan(
44 : /// text: 'Hello ',
45 : /// style: DefaultTextStyle.of(context).style,
46 : /// children: <TextSpan>[
47 : /// TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
48 : /// TextSpan(text: ' world!'),
49 : /// ],
50 : /// ),
51 : /// )
52 : /// ```
53 : /// {@end-tool}
54 : ///
55 : /// See also:
56 : ///
57 : /// * [TextStyle], which discusses how to style text.
58 : /// * [TextSpan], which is used to describe the text in a paragraph.
59 : /// * [MongolText], which automatically applies the ambient styles described by a
60 : /// [DefaultTextStyle] to a single string.
61 : /// * [MongolText.rich], a const text widget that provides similar functionality
62 : /// as [MongolRichText]. [MongokText.rich] will inherit [TextStyle] from [DefaultTextStyle].
63 : class MongolRichText extends LeafRenderObjectWidget {
64 : /// Creates a paragraph of rich text in vertical orientation for traditional
65 : /// Mongolian.
66 : ///
67 : /// The [maxLines] property may be null (and indeed defaults to null), but if
68 : /// it is not null, it must be greater than zero.
69 : ///
70 : /// The [text] argument must not be null.
71 8 : const MongolRichText({
72 : Key? key,
73 : required this.text,
74 : this.textAlign = MongolTextAlign.top,
75 : this.softWrap = true,
76 : this.overflow = TextOverflow.clip,
77 : this.textScaleFactor = 1.0,
78 : this.maxLines,
79 2 : }) : assert(maxLines == null || maxLines > 0),
80 7 : super(key: key);
81 :
82 : /// The text to display in this widget.
83 : final TextSpan text;
84 :
85 : /// How the text should be aligned vertically.
86 : final MongolTextAlign textAlign;
87 :
88 : /// Whether the text should break at soft line breaks.
89 : ///
90 : /// If false, the glyphs in the text will be positioned as if there was
91 : /// unlimited vertical space.
92 : final bool softWrap;
93 :
94 : /// How visual overflow should be handled.
95 : final TextOverflow overflow;
96 :
97 : /// The number of font pixels for each logical pixel.
98 : ///
99 : /// For example, if the text scale factor is 1.5, text will be 50% larger than
100 : /// the specified font size.
101 : final double textScaleFactor;
102 :
103 : /// An optional maximum number of lines for the text to span, wrapping if
104 : /// necessary. If the text exceeds the given number of lines, it will be
105 : /// truncated according to [overflow].
106 : ///
107 : /// If this is 1, text will not wrap. Otherwise, text will be wrapped at the
108 : /// edge of the box.
109 : final int? maxLines;
110 :
111 7 : @override
112 : MongolRenderParagraph createRenderObject(BuildContext context) {
113 7 : return MongolRenderParagraph(
114 7 : text,
115 7 : textAlign: textAlign,
116 7 : softWrap: softWrap,
117 7 : overflow: overflow,
118 7 : textScaleFactor: textScaleFactor,
119 7 : maxLines: maxLines,
120 : );
121 : }
122 :
123 5 : @override
124 : void updateRenderObject(
125 : BuildContext context, MongolRenderParagraph renderObject) {
126 : renderObject
127 10 : ..text = text
128 10 : ..textAlign = textAlign
129 10 : ..softWrap = softWrap
130 10 : ..overflow = overflow
131 10 : ..textScaleFactor = textScaleFactor
132 10 : ..maxLines = maxLines;
133 : }
134 :
135 0 : @override
136 : void debugFillProperties(DiagnosticPropertiesBuilder properties) {
137 0 : super.debugFillProperties(properties);
138 0 : properties.add(StringProperty('text', text.toPlainText()));
139 0 : properties.add(EnumProperty<MongolTextAlign>('textAlign', textAlign,
140 : defaultValue: MongolTextAlign.top));
141 0 : properties.add(FlagProperty('softWrap', value: softWrap, ifTrue: 'wrapping at box height', ifFalse: 'no wrapping except at line break characters', showName: true));
142 0 : properties.add(EnumProperty<TextOverflow>('overflow', overflow,
143 : defaultValue: TextOverflow.clip));
144 0 : properties.add(
145 0 : DoubleProperty('textScaleFactor', textScaleFactor, defaultValue: 1.0));
146 0 : properties.add(IntProperty('maxLines', maxLines, ifNull: 'unlimited'));
147 : }
148 : }
149 :
150 :
|