所以我有這個獨特的要求,如下圖所示,其中藍色文本是用戶輸入。我一直在對此進行自己的研究,并找到了一些可行的解決方案,其中一個自定義TextEditingController似乎最有希望。我還沒有開始實施,但我想我以后可能會遇到觸摸和游標控制元件的一些問題。
現在我的問題是,有沒有更好的方法我可能忽略了?有沒有辦法優雅地處理哪些區域是可觸摸的,洗掉時處理游標,并移動到下一個焦點?

uj5u.com熱心網友回復:
正如@Andrey Gordeev 所建議的,我們可以使用Wrap Widgetachildren定義如下:
Wrap(
alignment: WrapAlignment.center,
runAlignment: WrapAlignment.center,
children: [
const Text('Hala, we\'re '),
SizedBox(
width: 50,
height: 20,
child: TextField(
controller: TextEditingController.fromValue(
const TextEditingValue(text: 'aryab'),
),
),
),
const Text(' You? I\'m '),
SizedBox(
width: 150,
height: 20,
child: TextField(
controller: TextEditingController.fromValue(
const TextEditingValue(text: 'Someone Awesome'),
),
),
),
],
)
這將呈現附加螢屏截圖的結果:

可以在下面找到完整的示例代碼段:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child:
Wrap(
alignment: WrapAlignment.center,
runAlignment: WrapAlignment.center,
children: [
const Text('Hala, we\'re '),
SizedBox(
width: 50,
height: 20,
child: TextField(
controller: TextEditingController.fromValue(
const TextEditingValue(text: 'aryab'),
),
),
),
const Text(' You? I\'m '),
SizedBox(
width: 150,
height: 20,
child: TextField(
controller: TextEditingController.fromValue(
const TextEditingValue(text: 'Someone Awesome'),
),
),
),
],
),
),
);
}
}
uj5u.com熱心網友回復:
雖然有兩種型別的widget,<plainText,inputText>可以放在這里,我們需要關心從表單/文本中檢索全文。
我認為為小部件創建地圖將是一個不錯的選擇。
final Map<String, String?> _textsInForm = {
"Hala, we're": null,
"aryab": "groupName",
". You? I'm": null,
"Someone Awysome": "myself",
",a": null,
"Driver": "job",
};
如您所見,我將key其用作主值和value可空字串。如果我們找到空字串,它將只是Text小部件,否則TextFiled。
我正在RichText用來處理這種情況。
我們將創建串列List<InlineSpan>供RichText和List<TextEditingController>處理輸入文本。
我正在使用StatelessWidget, 同時stateFullWidget處理initState.
有一個問題,它采用最小提示文本寬度。您可以嘗試改為傳遞文本TextEditingController。

import 'package:flutter/material.dart';
class SomeTextEditable extends StatelessWidget {
SomeTextEditable({Key? key}) : super(key: key);
final Map<String, String?> _textsInForm = {
"Hala, we're": null,
"aryab": "groupName",
". You? I'm": null,
"Someone Awysome": "myself",
",a": null,
"Driver": "job",
};
final TextStyle _hintTextStyle = const TextStyle(
color: Colors.grey,
);
final TextStyle _textFiledStyle = const TextStyle(
color: Colors.blue,
);
WidgetSpan _textFiled(TextEditingController controller, String hint) =>
WidgetSpan(
alignment: PlaceholderAlignment.middle, // set middle according to texts
child: IntrinsicWidth(
//flexiable size
child: TextField(
style: _textFiledStyle,
controller: controller,
decoration: InputDecoration(
hintStyle: _hintTextStyle,
hintText: hint,
border: InputBorder.none,
),
),
),
);
@override
Widget build(BuildContext context) {
List<TextEditingController> controllers = [];
List<InlineSpan> textSpans = [];
_textsInForm.forEach((key, value) {
if (value != null) {
TextEditingController controller = TextEditingController();
controllers.add(controller);
textSpans.add(_textFiled(controller, key));
} else {
textSpans.add(TextSpan(text: "$key "));
}
});
return Scaffold(
body: Column(
children: [
RichText(
text: TextSpan(children: textSpans),
),
ElevatedButton(
onPressed: () {
String result = "";
int i = 0;
_textsInForm.forEach((key, value) {
if (value != null) {
final textFromIcontroller = controllers[i ].text;
result = "$textFromIcontroller ";
} else {
result = "$key ";
}
});
print(result);
},
child: Text("Get Text"),
),
],
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/340049.html
