再利用可能な複数行テキスト コンポーネントを 1 つ作成しました。TextSpan ウィジェットを使用しています。
コード:
/// Creates multi line styled widget which displays list of items with bullet
class AppMultiLineText extends StatelessWidget {
final List<CustomStyledText> items;
final String semanticsText;
const AppMultiLineText({@required this.items, this.semanticsText}) : assert(items != null);
@override
Widget build(BuildContext context) {
final style = context.appThemeData.bodyCopyDefault.appTextStyle;
final styleBold = context.appThemeData.bodyCopyBold.appTextStyle;
return MergeSemantics(
child: Padding(
padding: context.appThemeData.multilineDisplay.padding,
child: Column(
children: [
_buildRow(context, items, style, styleBold, semanticsText),
],
),
),
);
}
}
Widget _buildRow(BuildContext context, List<CustomStyledText> styledText, AppTextStyle style, AppTextStyle styleBold,
String semanticsText) {
final _padding = EdgeInsets.symmetric(vertical: context.appThemeData.bulletStyle.spaceBetweenLines);
return Semantics(
child: Padding(
padding: _padding,
child: Row(
children: [
_buildBulletSection(context),
_styledWidget(context, styledText, style, styleBold, semanticsText),
],
),
),
);
}
Widget _buildBulletSection(BuildContext context) {
// final textScaleFactor = _textScaleFactor(context);
final _bulletPadding = EdgeInsets.only(
top: context.appThemeData.bulletStyle.bulletOffset, right: context.appThemeData.bulletStyle.spaceAfterBullet);
return Text.rich(
TextSpan(
children: [
WidgetSpan(
child: Padding(
padding: _bulletPadding,
child: _drawBullet(context),
),
),
],
),
//textScaleFactor: textScaleFactor,
);
}
Widget _drawBullet(BuildContext context) {
final _bulletDiameter = context.appThemeData.bulletStyle.bulletDiameter;
return Container(
width: _bulletDiameter,
height: _bulletDiameter,
decoration: BoxDecoration(
color: context.appThemeData.bodyCopyDefault.appTextStyle.textStyle.color,
shape: BoxShape.circle,
),
);
}
Widget _styledWidget(BuildContext context, List<CustomStyledText> styledText, AppTextStyle style,
AppTextStyle styleBold, String semanticsText) {
final scaleFactor = context.calculateScaleFactor(
style.textStyle.fontSize,
style.maximumFontSize,
style.minimumScalingFactor,
);
return Expanded(
child: Text.rich(
TextSpan(
style: style.textStyle,
children: styledText.map((e) {
return TextSpan(
text: e.text,
style: e.isBold
? styleBold.textStyle.copyWith(
letterSpacing: e.isNumeric ? PresentationConstants.numericLetterSpacing : null,
)
: style.textStyle.copyWith(
letterSpacing: e.isNumeric ? PresentationConstants.numericLetterSpacing : null,
),
);
}).toList(),
),
style: style.textStyle,
textScaleFactor: scaleFactor,
textAlign: style.textAlign,
semanticsLabel: semanticsText,
),
);
}
class CustomStyledText {
final String text;
final String styleText;
final bool isBold;
final bool isNumeric;
CustomStyledText(
this.text, {
this.styleText,
this.isBold = false,
this.isNumeric = false,
});
}
class:
AppMultiLineText(
items: [
CustomStyledText(context.appLocalization.listOfRequirementsBodyCopy1),
CustomStyledText(context.appLocalization.listOfRequirementsBodyCopy2),
],
),
],
),
[![ここに画像の説明を入力][1]][1]
これは予想されるデザインです [1]: https://i.stack.imgur.com/AZF3w.png
しかし、私はすべての項目を段落として取得しています。