gpt4 book ai didi

flutter - 使用流/ block 从列表中的文本字段中设置值

转载 作者:行者123 更新时间:2023-12-04 10:48:19 25 4
gpt4 key购买 nike

使用流/块时,我在为文本字段设置值时遇到问题。基本上,我有一个文本字段,单击时我想要一个弹出对话框来提供要选择的值列表。当用户选择一个值时,应在文本字段中设置该值。当使用常规文本字段时,我知道我可以使用 textfield.text = value 但是当使用流/块时是不同的。我不知道如何使用它。

这是我的流代码

import 'dart:async';
import 'validators.dart';
import 'package:rxdart/rxdart.dart';


class Bloc extends Object with Validators {

final amountController = BehaviorSubject<String>();
final frequencyController = BehaviorSubject<String>();
final datePaidController = BehaviorSubject<String>();
final categoryController = BehaviorSubject<String>();
final depositToController = BehaviorSubject<String>();
final descriptionController = BehaviorSubject<String>();

// Add data to stream
Stream<String> get amount => amountController.stream.transform(validateAmount);
Stream<String> get frequency =>
frequencyController.stream.transform(validateEmpty);
Stream<String> get datePaid =>
datePaidController.stream.transform(validateEmpty);
Stream<String> get category =>
categoryController.stream.transform(validateEmpty);
Stream<String> get deposit =>
depositToController.stream.transform(validateEmpty);
Stream<String> get description =>
descriptionController.stream.transform(validateEmpty);
/*
Stream<bool> get submitValid =>
Observable.combineLatest6(amount, frequency, datePaid, category, deposit,
description, (a,b,c,d,e,f) => true);
*/

// change data
Function(String) get changeAmount => amountController.sink.add;
Function(String) get changeFrequency => frequencyController.sink.add;
Function(String) get changeDatePaid => datePaidController.sink.add;
Function(String) get changeCategory => categoryController.sink.add;
Function(String) get changeDepositTo => depositToController.sink.add;
Function(String) get changeDescription => descriptionController.sink.add;

submit() {
final validAmount = amountController.value;
final validFrequency = frequencyController.value;
final validDatePaid = datePaidController.value;
final validCategory = categoryController.value;
final validDepositTo = depositToController.value;
final validDescription = descriptionController.value;

// print('Email is $validEmail, and password is $validPassword');
}

dispose() {
amountController.close();
frequencyController.close();
datePaidController.close();
categoryController.close();
depositToController.close();
descriptionController.close();
}
}



我的文本字段代码(与上面的不同)
 final bloc = Provider.of(context);

return StreamBuilder(
stream: bloc.amount,
builder: (context, snapshot) {
return TextField(
onChanged: bloc.changeAmount,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Income Amount',
// labelText: 'Email Address',
errorText: snapshot.error,
prefixIcon: Icon(Icons.attach_money),
suffix: IconButton(
icon: Icon(Icons.arrow_right),
onPressed: () => {

showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually list of items to choose from

),
],
);
},
)
},

),
),
);
},
);

如果您查看文本字段代码,则文本字段上的图标有 onPressed 函数。按下时,应出现一个对话框供用户选择一个值。一旦选择了一个值,我想用该值设置文本字段。我正在使用流/块,但我不知道该怎么做,因为文本 Controller 在 Bloc 类中,而文本字段代码是另一个类。当用户从弹出列表中选择时,有人可以帮助如何设置文本字段值吗?提前致谢

最佳答案

使用 TextEditingController .
它将允许您在文本字段中处理和设置文本值。
你可以这样使用它:

final _controller = TextEditingController();
在文本字段中,分配 _controller :
TextField(
controller: _controller,
//everything else should be same as before
),
当需要使用新值更新文本字段时,您可以执行以下操作:
_controller.text = newAmountFromStream;

关于flutter - 使用流/ block 从列表中的文本字段中设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59596203/

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