我正在嘗試創建一個基本計算器,其中我有一個輸入字串..
比如用戶輸入了 79 93-27,
在這里,我想為數字設定黑色,為操作員設定紅色......
這是我解釋我想要什么的代碼
這是我代碼的一部分,我不想更改任何型別的變數型別..
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
String inputstring = '79 93-27';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text(
inputstring,
style: TextStyle(fontSize: 30),
)),
),
);
}
}
uj5u.com熱心網友回復:
你可以像這樣使用RegExp和RichText:
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
String inputstring = '79 93-27';
@override
Widget build(BuildContext context) {
RegExp regExp = new RegExp(r'(\ |-|\*|\/|=|>|<|>=|<=|&|\||%|!|\^|\(|\))');
List inputs = inputstring.replaceAll(regExp, '').split('').toList();
return MaterialApp(
home: Scaffold(
body: Center(
child: RichText(
text: TextSpan(
children: inputstring.characters
.map((e) => inputs.contains(e)
? TextSpan(
text: e,
style: TextStyle(color: Colors.black),
)
: TextSpan(
text: e,
style: TextStyle(color: Colors.red),
))
.toList()),
),
),
),
);
}
}

uj5u.com熱心網友回復:
您可以將 RichText 小部件與多個 TextSpan 一起使用。請參閱示例:
https://api.flutter.dev/flutter/widgets/RichText-class.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529317.html
標籤:扑镖颤振布局
上一篇:如何在FutureBuilder中呼叫2個回傳Future值的Future函式?
下一篇:我需要在串列中添加多個圖示和按鈕
