Text 如何使用flifter编写带有要点的段落?

Text 如何使用flifter编写带有要点的段落?,text,flutter,bulletedlist,Text,Flutter,Bulletedlist,使用HTML,我可以向以下段落添加要点: 范例 范例 范例 我怎样才能在flifter中编写项目符号表单 new Text(''), 我试过使用它,它似乎有效。当然,您可以根据需要将其更改为编号/有序或无序列表 用降价来做这件事太过分了。使用•字符要容易得多 如果您懒得复制粘贴字符,这里有一个自定义的文本,可以为您执行此操作: class Bullet extends Text { const Bullet( String data, { Key key,

使用HTML,我可以向以下段落添加要点:

  • 范例
  • 范例
  • 范例
我怎样才能在flifter中编写项目符号表单

new Text(''),
我试过使用它,它似乎有效。当然,您可以根据需要将其更改为编号/有序或无序列表


用降价来做这件事太过分了。使用
字符要容易得多

如果您懒得复制粘贴字符,这里有一个自定义的
文本
,可以为您执行此操作:

class Bullet extends Text {
  const Bullet(
    String data, {
    Key key,
    TextStyle style,
    TextAlign textAlign,
    TextDirection textDirection,
    Locale locale,
    bool softWrap,
    TextOverflow overflow,
    double textScaleFactor,
    int maxLines,
    String semanticsLabel,
  }) : super(
          '• ${data}',
          key: key,
          style: style,
          textAlign: textAlign,
          textDirection: textDirection,
          locale: locale,
          softWrap: softWrap,
          overflow: overflow,
          textScaleFactor: textScaleFactor,
          maxLines: maxLines,
          semanticsLabel: semanticsLabel,
        );
}

我最好使用utf代码。对于列表,我认为更合适的是:

class DottedText extends Text {
  const DottedText(String data, {
    Key key,
    TextStyle style,
    TextAlign textAlign,
    TextDirection textDirection,
    Locale locale,
    bool softWrap,
    TextOverflow overflow,
    double textScaleFactor,
    int maxLines,
    String semanticsLabel,
  }) : super(
    '\u2022 $data',
    key: key,
    style: style,
    textAlign: textAlign,
    textDirection: textDirection,
    locale: locale,
    softWrap: softWrap,
    overflow: overflow,
    textScaleFactor: textScaleFactor,
    maxLines: maxLines,
    semanticsLabel: semanticsLabel,);
}

您可以使用
行拆分器
,以及ASCII项目符号。你所需要的只是一个带换行符的字符串

String myStringWithLinebreaks = "Line 1\nLine 2\nLine 3";
列表互动程序中的示例

 ListTile(
                  title: Text("Title Text"),
                  subtitle: 
                    Column(
                      children: LineSplitter.split(myStringWithLinebreaks).map((o) {
                    return Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text("• "),
                        Expanded(
                          child: Text(o),
                        )
                      ],
                    );
                  }).toList())),
listile(
标题:文本(“标题文本”),
字幕:
纵队(
子级:LineSplitter.split(myStringWithLinebreaks).map((o){
返回行(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
文本(“•”),
扩大(
儿童:文本(o),
)
],
);
}).toList()),

如果您不想下载另一个库(例如,Flatter_markdown),并且您的一个或多个列表项具有跨多行的长文本,我将使用Raegtime的答案。然而,由于它假设一个带有换行符的字符串,我想为一个带有字符串的列表创建一个版本,这是一个更常见的场景。在下面的代码中,
使列表项出现在不同的行上,
使项目符号在其下方有空白

import 'package:flutter/material.dart';

class UnorderedList extends StatelessWidget {
  UnorderedList(this.texts);
  final List<String> texts;

  @override
  Widget build(BuildContext context) {
    var widgetList = <Widget>[];
    for (var text in texts) {
      // Add list item
      widgetList.add(UnorderedListItem(text));
      // Add space between items
      widgetList.add(SizedBox(height: 5.0));
    }

    return Column(children: widgetList);
  }
}

class UnorderedListItem extends StatelessWidget {
  UnorderedListItem(this.text);
  final String text;

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text("• "),
        Expanded(
          child: Text(text),
        ),
      ],
    );
  }
}
并得到结果:

@Snurrig-非常好的答案。很好!非常感谢

修改它以创建一个有序/编号的列表。 见下文:

class OrderedList extends StatelessWidget {
  OrderedList(this.texts);
  final List<dynamic> texts;

  @override
  Widget build(BuildContext context) {
    var widgetList = <Widget>[];
    int counter = 0;
    for (var text in texts) {
      // Add list item
      counter++;
      widgetList.add(OrderedListItem(counter, text));
      // Add space between items
      widgetList.add(SizedBox(height: 5.0));
    }

    return Column(children: widgetList);
  }
}

class OrderedListItem extends StatelessWidget {
  OrderedListItem(this.counter, this.text);
  final int counter;
  final String text;

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text("$counter. "),
        Expanded(
          child: Text(text),
        ),
      ],
    );
  }
}
类OrderedList扩展了无状态小部件{
OrderedList(this.text);
最后清单文本;
@凌驾
小部件构建(构建上下文){
var widgetList=[];
int计数器=0;
for(文本中的变量文本){
//添加列表项
计数器++;
add(OrderedListItem(计数器,文本));
//在项目之间添加空格
添加(SizedBox(高度:5.0));
}
返回列(子项:widgetList);
}
}
类OrderedListItem扩展了无状态小部件{
OrderedListItem(this.counter,this.text);
最终整数计数器;
最终字符串文本;
@凌驾
小部件构建(构建上下文){
返回行(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
文本(“$计数器”),
扩大(
子:文本(Text),
),
],
);
}
}

是一个字符。为什么不在你的
文本中使用它呢?哦,然后使用一个列小部件来对齐它们。
flatter\u markdown
包可能会这样做。它不会将长文本消息对齐到项目符号图标的右侧。对长文本有什么建议吗?谢谢。它不会将长文本消息和项目符号图标的右侧对齐。对长文本有什么建议吗?谢谢。它不会将长文本消息和项目符号图标的右侧对齐。对长文本有什么建议吗?谢谢。@Kamlesh如果您的文本包含多行-我想,您最好使用Row小部件,它包含点作为第一个子项,文本作为第二个子项是的,亲爱的,我实现了相同的功能,因为没有其他内置解决方案。
UnorderedList([
    "What conclusions can we draw from the implementation?",
    "Are there any changes that need to be introduced permanently?"
])
class OrderedList extends StatelessWidget {
  OrderedList(this.texts);
  final List<dynamic> texts;

  @override
  Widget build(BuildContext context) {
    var widgetList = <Widget>[];
    int counter = 0;
    for (var text in texts) {
      // Add list item
      counter++;
      widgetList.add(OrderedListItem(counter, text));
      // Add space between items
      widgetList.add(SizedBox(height: 5.0));
    }

    return Column(children: widgetList);
  }
}

class OrderedListItem extends StatelessWidget {
  OrderedListItem(this.counter, this.text);
  final int counter;
  final String text;

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text("$counter. "),
        Expanded(
          child: Text(text),
        ),
      ],
    );
  }
}