当前位置:网站首页>The textfield is encapsulated by the flutter itself, which causes the data display to be disordered when the data in the list is updated.
The textfield is encapsulated by the flutter itself, which causes the data display to be disordered when the data in the list is updated.
2022-06-25 16:00:00 【Favorite grapes】
demand
There is a list to enter , The input list will vary according to the selected options , And enter different content .
such as :
choice Vip When the customer , Need to enter : full name , cell-phone number , address ,Vip Duration , payment method
When selecting normal users , Need to enter : full name , cell-phone number , address .
Realization
Use ListView Show all input items , Because the input box is universal . So I made a simple package .
On the left is the title , On the right is the input , When empty content , Exhibition hint Tips Please enter .
It's probably like this
-----------------------------
Customer type ⊙Vip ⊙ Ordinary
-----------------------------
full name Please enter
-----------------------------
cell-phone number Please enter
-----------------------------
address Please enter
-----------------------------
When the customer type changes , The following data needs to be changed by linkage .
problem
When switching customer types , There will be data confusion ( For example, the mobile phone number is displayed to the name ).
Trace the problem :
Because data sharing uses Provider+ChangeNotifier In the form of . therefore , When the data changes , actually , The quotation will be updated if it is worth mentioning .
So the first step is to judge , Whether the value is passed to the component encapsulated by itself .
Own encapsulated input box code ( Simple version , Mainly to illustrate the problem ):
// CustomInputTextField
class CustomInputTextField extends StatefulWidget {
// Tips
final String hint;
// Input change monitoring
final ValueChanged<String>? onValueChanged;
// Initial value
final String? value;
const CustomInputTextField(
{Key? key,
required this.hint,
this.onValueChanged,
this.value,})
: super(key: key);
@override
State<StatefulWidget> createState() {
return CustomInputTextFieldState();
}
}
class CustomInputTextFieldState extends State<CustomInputTextField> {
late TextEditingController _controller;
@override
void initState() {
_controller = TextEditingController(text : widget.value??"");
}
@override
Widget build(BuildContext context) {
return TextField(
controller: _controller,
onChanged: onInputChanged,
);
}
}
Find the cause of the problem
Actually , When the data is updated , Every time Build Is the correct value , What then? , The displayed values are indeed out of order ?
because ,TextField The value in , What is shown is _controller Medium Text, And because of the use of State, and initState Method only executes once , So when the component value Time of change , actually ,_controller The data in does not change . So the display will be disordered .
Solution
Rough version
Directly create a new one each time controller, Because of this controller In the whole assembly , Didn't take the initiative to use .
controller: TextEditingController.fromValue(
TextEditingValue(
text: widget.value??'',
selection: TextSelection.fromPosition(
TextPosition(offset: widget.value?.length ?? 0),
),
),
),
Kind version
Every time a component calls didChangeDependence when , Take again widget The value of _controller On assignment .
Or in didUpdateWidget when , Take the new value to , Compare with the old value , And replace .
边栏推荐
- QC, QA, IPQC, JQE, DQA, SQE, DQC, MQC, IQC, FQC, OQC
- Free books! AI across the Internet paints old photos. Here is a detailed tutorial!
- Principle analysis of ThreadLocal source code
- Don't underestimate the integral mall, its role can be great!
- 什么是NFT数字藏品?
- Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
- How to reload the win10 app store?
- Sword finger offer 04 Find in 2D array
- Deep learning pytorch cifar10 dataset training "suggestions collection"
- Continuously improve the overall performance of adaoracle Oracle Oracle
猜你喜欢

Don't underestimate the integral mall, its role can be great!

appium服务的启动与关闭踩坑记录

Sword finger offer 03 Duplicate number in array

Asynchronous processing of error prone points
Cloning and importing DOM nodes
Super comprehensive custom deep copy function
Consumer and producer cases of inter thread synchronization (condition variable)

异步处理容易出错的点

通俗讲跨域

The release of autok3s v0.5.0 continues to be simple and friendly
随机推荐
读配置、讲原理、看面试真题,我只能帮你到这了。。。
元宇宙系统的概念解析
TFIDF and BM25
不要小看了积分商城,它的作用可以很大!
Message format of Modbus (PLC)
Free books! AI across the Internet paints old photos. Here is a detailed tutorial!
What are the reasons why the game industry needs high defense servers?
Optimization of lazyagg query rewriting in parsing data warehouse
加载本地cifar10 数据集
JS中的==和===的区别(详解)
Uniapp converts graphic verification codes in the form of file streams into images
Introduction to database transactions
[problem solving] dialogfragment can not be attached to a container view
Deep learning pytorch cifar10 dataset training "suggestions collection"
Sword finger offer 06 Print linked list from end to end
MySQL modifier l'instruction de champ
普通人的2022春招总结(阿里、腾讯offer)
Yadali brick playing game based on deep Q-learning
免费送书啦!火遍全网的AI给老照片上色,这里有一份详细教程!
Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way