Flutter块状态未更新,我找不到任何错误

cpjpxq1n  于 2023-05-18  发布在  Flutter
关注(0)|答案(1)|浏览(121)

区块文件

import 'package:bloc/bloc.dart';

part 'address_event.dart';
part 'address_state.dart';

class AddressBloc extends Bloc<AddressEvent, AddressState> {
  AddressBloc() : super(AddressInitial());

  @override
  Stream<AddressState> mapEventToState(AddressEvent event) async* {
    if (event is GetInput) {
      yield* _mapGetInputEventToState(event);
    }
  }

  @override
  Stream<AddressState> _mapGetInputEventToState(GetInput event) async* {
    yield AddressInitial(inputText: event.inputText);
  }
}

事件文件

part of 'address_bloc.dart';

@override
abstract class AddressEvent {}

class GetInput extends AddressEvent {
  String inputText;

  GetInput(this.inputText);
}

状态文件

part of 'address_bloc.dart';

@override
abstract class AddressState {}

class AddressInitial extends AddressState {
  String? inputText;

  AddressInitial({this.inputText});
}

UI文件块部件

BlocBuilder<AddressBloc, AddressState>(
                              bloc: addressBloc,
                              builder: (context, state) {
                                if(state is AddressInitial){
                                  return Row(
                                    children: [
                                      Icon(Icons.location_on, size: 20),
                                      Space(24),
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              "Address",
                                              textAlign: TextAlign.start,
                                              style: TextStyle(
                                                  fontWeight: FontWeight.w900,
                                                  fontSize: 21),
                                            ),
                                            Space(4),
                                            if(textEditingController.text.isNotEmpty) 
                                              Text(
                                                **"${state.inputText}",**
                                                textAlign: TextAlign.start,
                                                style: TextStyle(
                                                  color: Colors.grey,
                                                  fontSize: 16,
                                                ),
                                              ),
                                            if(textEditingController.text.isEmpty) 
                                              Text(
                                                "2911, sec 9/11, Hisar",
                                                textAlign: TextAlign.start,
                                                style: TextStyle(
                                                  color: Colors.grey,
                                                  fontSize: 16,
                                                ),
                                              ),
                                          ],
                                        ),
                                      ),
                                      Space(8),
                                      IconButton(
                                          onPressed: () {
                                            showDialog(
                                                context: context,
                                                builder: (BuildContext context) {
                                                  return Dialog(
                                                    child: Container(
                                                      height: 250,
                                                      width: 500,
                                                      padding:
                                                          const EdgeInsets.all(
                                                              20),
                                                      child:
                                                          SingleChildScrollView(
                                                        child: Column(
                                                          children: [
                                                            CustomTextField(
                                                                controller: textEditingController,
                                                                maxLines: 3,
                                                                title: 'Address',
                                                                hasTitle: true,
                                                                initialValue: '',
                                                                **onChanged:
                                                                    (value) {
                                                                      addressBloc.add(GetInput(textEditingController.text));
                                                                    }),**
                                                            CustomTextField(
                                                                maxLines: 1,
                                                                title: 'LandMark',
                                                                hasTitle: true,
                                                                initialValue: '',
                                                                onChanged:
                                                                    (value) {}),
                                                            ElevatedButton(
                                                              style: ElevatedButton
                                                                  .styleFrom(
                                                                      backgroundColor:
                                                                          Colors
                                                                              .white),
                                                              onPressed: () {
                                                                Navigator.pop(
                                                                    context);
                                                              },
                                                              child: Text(
                                                                'Save',
                                                                style: Theme.of(
                                                                        context)
                                                                    .textTheme
                                                                    .headline5!
                                                                    .copyWith(
                                                                        color: Colors
                                                                            .black),
                                                              ),
                                                            )
                                                          ],
                                                        ),
                                                      ),
                                                    ),
                                                  );
                                                });
                                          },
                                          icon: Icon(Icons.edit)),
                                    ],
                                  );
                                
                                }
                                else{
                                  return SizedBox(height: 10,);
                                }
                              },
                            ),

我已经把块的部分在UI在**请,所以它是为什么它是不更新的,即使我添加一些值的地址对话框。对话框弹出编辑地址,但在我点击保存按钮后,地址输入部分的值没有改变。我也尝试在保存的onPressed中添加相同的块代码,但它似乎不工作无论如何。

oiopk7p5

oiopk7p51#

我会尝试摆脱textEditingController,在您的CustomTextField上的on Changed属性上尝试以下操作:

onChanged: (value)=>  addressBloc.add(GetInput(value))

还建议在BlocBuilder之前初始化TextEditingController,并将值赋给BlocBuilder的builder方法上的控制器。
我还建议在state类上扩展Equatable,这样你就可以声明什么使一个事件与另一个事件不同,BlocBuilder可以响应它。

相关问题