gpt4 book ai didi

flutter - 如何创建扩展方法以对字符串进行颜色加粗,删除内容

转载 作者:行者123 更新时间:2023-12-03 04:21:36 24 4
gpt4 key购买 nike

我有一个像这样的JSON:

[
{
"country": "UNITED STATED",
"gini": 39,
"note": "a country mostly located in central North America, between CANADA and MEXICO, It consists of 50 states. At 3.8 million square miles (9.8 million km2)"
},
{
"country": "GERMANY",
"gini": 33.1,
"note": "Covering an area of 357,022 square kilometres (137,847 sq mi), it borders DENMARK to the north, POLAND to the east. "
}
]

我要创建以下2个扩展方法:
  • 对于“Gini”数据:删除“.0”(例如:“39.0” =>“39”)
  • 对于“注释”数据:
  • 粗体和颜色精确的文本“CANADA”; “墨西哥”; “丹麦”; “POLAND”
  • 颜色所有数字
  • 颜色“。”和“,”(如果介于数字
  • 之间)

    enter image description here

    所以请帮助我,这是主要文件
    import 'package:ask/services/us_services.dart';
    import 'package:flutter/material.dart';
    import 'model/us_model.dart';

    class Demo2 extends StatefulWidget {
    Demo2() : super();

    @override
    _ContinentPageState createState() => _ContinentPageState();
    }

    class _ContinentPageState extends State<Demo2> {
    List<Zone> _zone = [];

    void initState() {
    super.initState();
    ContinentServices2.getContinent().then((zones) {
    setState(() {
    _zone = zones;
    });
    });
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(title: Text('')),
    body: Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
    for (Zone zone in _zone)
    Padding(
    padding: const EdgeInsets.all(8.0),
    child: Table(columnWidths: {
    0: FlexColumnWidth(0.2)
    },
    children: [
    TableRow(children: [TableCell(child: Text('Country ')), TableCell(child: Text(zone.country))]),
    TableRow(children: [TableCell(child: Text('Gini')), TableCell(child: Text('${zone.gini}'))]),
    TableRow(children: [TableCell(child: Text('Note')), TableCell(child: Text(zone.note))]),
    ],
    ))
    ]));
    }
    }

    extension BoldColored on String {
    String get boldColored {}
    }

    extension Numbers on String {
    String get numbers {}
    }

    最佳答案

    1。

    这是您可以使用的扩展。它只会在小数点后修剪零。

    extension DoubleExtension on double {
    String get asReadableString =>
    this % toInt() == 0 ? toInt().toString() : toString();
    }

    用法:

    final value = 20.0;
    print(value.asReadableString); // prints 20
    final value2 = 20.5;
    print(value2.asReadableString); // prints 20.5

    2。

    这是一个实用程序类,可用于格式化文本。

    class RichTextFormatter {
    final Map<RegExp, TextStyle> patternMap;
    final TextStyle normalStyle;

    RichTextFormatter({
    @required this.patternMap,
    @required this.normalStyle,
    });

    TextSpan buildFormattedTextSpan(String text) {
    final children = <TextSpan>[];
    RegExp allRegex;
    allRegex = RegExp(patternMap.keys.map((e) => e.pattern).join('|'));
    text.splitMapJoin(
    allRegex,
    onMatch: (match) {
    final key = patternMap.entries
    .singleWhere(
    (element) => element.key.allMatches(match[0]).isNotEmpty)
    .key;
    children.add(TextSpan(text: match[0], style: patternMap[key]));
    },
    onNonMatch: (span) {
    children.add(TextSpan(text: span, style: normalStyle));
    },
    );
    return TextSpan(style: normalStyle, children: children);
    }
    }

    用法:

    final formatter = RichTextFormatter(
    normalStyle: TextStyle(color: Colors.black),
    patternMap: {
    RegExp(r'CANADA|MEXICO|DENMARK|POLAND'):
    TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
    RegExp(r'\d+(.\d+)?'):
    TextStyle(color: Colors.orange),
    },
    );
    final textSpan = formatter.buildFormattedTextSpan(text);

    用户界面:

    Text.rich(textSpan)

    关于flutter - 如何创建扩展方法以对字符串进行颜色加粗,删除内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62255384/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com