如何使用Flutter编写带有子弹点的段落?

les*_*ger 9 text bulletedlist flutter

使用HTML我可以将一个项目符号添加到这样的段落:

<ul>
   <li> example </li>
   <li> example </li>
   <li> example </li>
<ul>
Run Code Online (Sandbox Code Playgroud)

如何在Flutter中编写项目符号表格?

new Text(''),
Run Code Online (Sandbox Code Playgroud)

Mja*_*tro 16

如果您不想下载另一个库(例如 flutter_markdown),并且您的一个或多个列表项具有跨越多行的冗长文本,我会选择 Raegtime 的回答。但是,由于它假定一个带有换行符的字符串,我想为带有字符串的列表创建一个版本,这是一种更常见的情况。在下面的代码中,Column使列表项出现在不同的行上,Row并使项目符号点在其下方具有空白空间。

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),
        ),
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我们可以这样使用它:

UnorderedList([
    "What conclusions can we draw from the implementation?",
    "Are there any changes that need to be introduced permanently?"
])
Run Code Online (Sandbox Code Playgroud)

并得到结果: 示例结果图片


Rém*_*let 15

Using markdown for this is overkill. Using character is by far easier.

If you're too lazy to copy paste the character, here's a custom Text that does it for you:

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,
        );
}
Run Code Online (Sandbox Code Playgroud)

  • 它不会将长文本消息与项目符号图标的右侧对齐。对于长文本有什么建议吗?谢谢。 (2认同)

And*_*sky 9

我最好使用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,);
}
Run Code Online (Sandbox Code Playgroud)


stt*_*106 5

我尝试使用flutter_markdown,它似乎可以工作。当然,您可以根据需要将其更改为编号/有序或无序列表。

在此处输入图片说明

import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter/material.dart';
void main() => runApp(Demo());


class Demo extends StatelessWidget {
  final testData = ["Example1", "Example2", "Example3", "Example100"];



  @override
  Widget build(BuildContext context) {
    final _markDownData = testData.map((x) => "- $x\n").reduce((x, y) => "$x$y");

    return MaterialApp(
        home: Scaffold(
      body: Container(
        margin: EdgeInsets.all(40.0),
        child: Markdown(data: _markDownData)),
    ));
  }
}
Run Code Online (Sandbox Code Playgroud)