是否可以將 a 中的最后String一個list自動輸入到 a 中TextField?
該串列在前幾秒鐘是空的,并且正在更改。它也在構建背景關系后初始化。
如果是,請提供代碼,因為我是新來的。
我知道如何使用TextEditingController.
final TextEditingController _controller = TextEditingController();
TextField(controller: _controller)
ElevatedButton(
onPressed: () {
const newText = 'Hello World';
final updatedText = _controller.text newText;
_controller.value = _controller.value.copyWith(
text: updatedText,
selection: TextSelection.collapsed(offset: updatedText.length),
);
},
)
但它有一個按鈕,我怎樣才能自動化呢?
uj5u.com熱心網友回復:
是的,你可以這樣做TextEditingController......讓我們有一個代碼示例,你有一個字串串列......例如
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late TextEditingController _textEditingController;
final List<String> _exampleList = [];
@override
void initState() {
Future.delayed(
const Duration(seconds: 2),
() {
if (mounted) {
setState(() {
_exampleList.add("apple");
_textEditingController = TextEditingController(text: _exampleList.last);
});
}
},
);
_textEditingController = TextEditingController(text: "Loading ...");
super.initState();
}
@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: TextFormField(
enabled: _exampleList.isNotEmpty,
controller: _textEditingController,
),
),
),
);
}
}
這是結果...這只是對如何在文本欄位中顯示串列的最后一項的簡單說明

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426590.html
上一篇:在顫動中啟動鏈接
