gpt4 book ai didi

flutter - 如何在我们输入 flutter 的 TextField 小部件时格式化文本?

转载 作者:行者123 更新时间:2023-12-03 15:04:35 25 4
gpt4 key购买 nike

当我们输入 TextField 时,我想格式化文本.例如,当我使用下划线时 _在字段中,它们之间的字符应为斜体。
我要的是类似 Markdown 文本格式发生在 Flutter 中的同一个小部件中。
RichText 可以提供帮助,但它们不可编辑。为此,我需要一个 RichTextField,但 Flutter 中不存在类似的东西。
我的结果应该类似于 WhatsApp 的消息字段,它应用了粗体、斜体、删除线等。

最佳答案

请参阅有关在现有“TextField”上实现答案的示例代码。
Screen Recording

import 'dart:ui';
import 'package:flutter/material.dart';

final Color darkBlue = const Color.fromARGB(255, 18, 32, 47);

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: const Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}

class TextFieldColorizer extends TextEditingController {
final Map<String, TextStyle> map;
final Pattern pattern;

TextFieldColorizer(this.map)
: pattern = RegExp(
map.keys.map((key) {
return key;
}).join('|'),
multiLine: true);

@override
set text(String newText) {
value = value.copyWith(
text: newText,
selection: TextSelection.collapsed(offset: newText.length),
composing: TextRange.empty,
);
}

@override
TextSpan buildTextSpan({TextStyle style, bool withComposing}) {
final List<InlineSpan> children = [];
String patternMatched;
String formatText;
TextStyle myStyle;
text.splitMapJoin(
pattern,
onMatch: (Match match) {
myStyle = map[match[0]] ??
map[map.keys.firstWhere(
(e) {
bool ret = false;
RegExp(e).allMatches(text)
..forEach((element) {
if (element.group(0) == match[0]) {
patternMatched = e;
ret = true;
return true;
}
});
return ret;
},
)];

if (patternMatched == r"_(.*?)\_") {
formatText = match[0].replaceAll("_", " ");
} else if (patternMatched == r'\*(.*?)\*') {
formatText = match[0].replaceAll("*", " ");
} else if (patternMatched == "~(.*?)~") {
formatText = match[0].replaceAll("~", " ");
} else if (patternMatched == r'```(.*?)```') {
formatText = match[0].replaceAll("```", " ");
} else {
formatText = match[0];
}
children.add(TextSpan(
text: formatText,
style: style.merge(myStyle),
));
return "";
},
onNonMatch: (String text) {
children.add(TextSpan(text: text, style: style));
return "";
},
);

return TextSpan(style: style, children: children);
}
}

class MyWidget extends StatefulWidget {
const MyWidget();
_MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
final TextEditingController _controller = TextFieldColorizer(
{
r"@.\w+": TextStyle(color: Colors.blue, shadows: kElevationToShadow[2]),
'red': const TextStyle(
color: Colors.red, decoration: TextDecoration.underline),
'green': TextStyle(color: Colors.green, shadows: kElevationToShadow[2]),
'purple': TextStyle(color: Colors.purple, shadows: kElevationToShadow[2]),
r'_(.*?)\_': TextStyle(
fontStyle: FontStyle.italic, shadows: kElevationToShadow[2]),
'~(.*?)~': TextStyle(
decoration: TextDecoration.lineThrough,
shadows: kElevationToShadow[2]),
r'\*(.*?)\*': TextStyle(
fontWeight: FontWeight.bold, shadows: kElevationToShadow[2]),
r'```(.*?)```': TextStyle(
color: Colors.yellow,
fontFeatures: [const FontFeature.tabularFigures()],
shadows: kElevationToShadow[2]),
},
);

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10),
child: TextField(
maxLines: 5,
onChanged: (text) {
final val = TextSelection.collapsed(offset: _controller.text.length);
_controller.selection = val;
},
style: const TextStyle(fontSize: 32),
controller: _controller,
),
);
}
}

关于flutter - 如何在我们输入 flutter 的 TextField 小部件时格式化文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55989844/

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