我做了一个关于我们的页面在我的Flutter 应用程序中的条款,政策等部分列出,并点击他们我存储在一些变量中的索引,我想打开它的描述下面的部分名称.

我通过使用赋值指示器成功地做到了这一点,但现在我想转换为BLOC.

以下是我的文件,

AboutUsState.dart

    import 'package:equatable/equatable.dart';
    import 'package:flutter/material.dart';
    import '../../../Model/ModelInformation.dart';
    
    @immutable
    abstract class AboutUsState extends Equatable {}
    
    class AboutUsLoadingState extends AboutUsState {
      @override
      List<Object?> get props => [];
    }
    
    class AboutUsLoadedState extends AboutUsState {
    
      final ModelInformation modelInformation;
        AboutUsLoadedState(this.modelInformation);
      @override
      List<Object?> get props => [modelInformation];
    }
    
    class AboutUsErrorState extends AboutUsState {
    
      final String error;
      AboutUsErrorState(this.error);
      @override
      List<Object?> get props => [error];
    
    }

AboutUsEvent.dart

import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';

@immutable
abstract class AboutUsEvent extends Equatable {
  const AboutUsEvent();
}

class LoadAboutUsEvent extends AboutUsEvent {
  @override
  List<Object?> get props => [];
}

class UpdateSelectedViewEvent extends AboutUsEvent {

  @override
  List<Object?> get props => [];
}

AboutUsBloc.dart

import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../Model/ModelInformation.dart';
import 'AboutUsEvent.dart';
import 'AboutUsState.dart';
import 'Repository.dart';

class AboutUsBloc extends Bloc<AboutUsEvent,AboutUsState> {

  final Repository repository;
    int selectedView = -1;

  AboutUsBloc(this.repository,this.selectedView) : super(AboutUsLoadingState()) {

    on<LoadAboutUsEvent>((event, emit) async {

        emit(AboutUsLoadingState());

        try {
          var dict = await repository.getAboutUs();
          final ModelInformation modelInformation = ModelInformation.fromJson(dict["mainResponse"]);

          emit(AboutUsLoadedState(modelInformation));
        } catch (e) {
          emit(AboutUsErrorState(e.toString()));
        }

    }
    );

  }
}

Repository.dart

import '../../../Services/ServiceManager.dart';
import '../../../Util/Constant.dart';

class Repository {

  Future getAboutUs() async {

   return await ServiceManager.callWebservice(
        varMethodType: enumMethodType.POST,
        strSuffixPath: ConstantApi.keyGetAboutUs,
        dictParameters: {},
        dictHeaders: {},
        arrayUploadFile: []
    );
  }

}

AboutUs.dart

import 'package:flutter_bloc/flutter_bloc.dart';
import '../../Model/ModelInformation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../Util/Constant.dart';
import 'AboutUs/AboutUsBloc.dart';
import 'AboutUs/AboutUsEvent.dart';
import 'AboutUs/AboutUsState.dart';
import 'AboutUs/Repository.dart';

class ClassAboutUs extends StatefulWidget {
  @override
  stateClassAboutUs createState() => stateClassAboutUs();
}

class stateClassAboutUs extends State<ClassAboutUs> {

  final AboutUsBloc aboutUsBloc = AboutUsBloc(Repository(),-1);

  @override
  void initState() {
    super.initState();
    aboutUsBloc.add(LoadAboutUsEvent());
  }

  @override
  Widget build(BuildContext context) {

    return

      WillPopScope(
        onWillPop: ()  async{
          Navigator.pop(context);
          return true;
        },

        child: new Scaffold(
          backgroundColor: Theme.of(context).colorScheme.background,

          appBar: AppBar(
            elevation: 0.5,
            backgroundColor: Theme.of(context).colorScheme.background,
            titleSpacing: 0,
            centerTitle: false,
            leading: BackButton(
              color: Theme.of(context).colorScheme.onBackground.withOpacity(0.87),
              onPressed: (){
                Navigator.pop(context);
              },
            ),
            title: Text("About us",

              style: TextStyle(
                color: Theme.of(context).colorScheme.onBackground.withOpacity(0.87),
                fontFamily: ConstantFont.keyPrimaryFont,
                fontSize: 15,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          body:
          BlocProvider(
              create: (_) => aboutUsBloc,

            child: BlocBuilder<AboutUsBloc, AboutUsState>(

              builder: (context, state) {
                if (state is AboutUsLoadingState) {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                }
                if (state is AboutUsErrorState) {
                  return const Center(child:  Text("Error"));
                }
                if (state is AboutUsLoadedState) {

                  ModelInformation modelInformation = state.modelInformation;
                  return ListView.builder(
                    padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                    itemCount: modelInformation.mainmenu!.informationPages!.length,
                    itemBuilder: (context, index) {

                        return

                        aboutUsBloc.selectedView == index ?
                        Container(
                            color: Theme.of(context).colorScheme.primary.withOpacity(0.10),
                            child: Column(

                              crossAxisAlignment: CrossAxisAlignment.start,

                              children: [

                                InkWell(
                                  onTap: (){
                                    aboutUsBloc.selectedView = aboutUsBloc.selectedView== index ? -1 : index;

                                  },
                                  child:
                                  Padding(
                                      padding: const EdgeInsets.all(10),
                                      child: Row(
                                        children: [

                                          Expanded(
                                            child: Text("${modelInformation.mainmenu!.informationPages![index].headingTitle}",style: ConstantTextStyle.h2.copyWith(fontWeight: FontWeight.w600,color: Theme.of(context).colorScheme.primary)),
                                          ),
                                          Padding(
                                            padding: EdgeInsets.only(left: 5,top: 5,bottom: 5),
                                            child: Icon(Icons.keyboard_arrow_up_rounded,size: 15,color: Theme.of(context).colorScheme.onBackground.withOpacity(0.60),),
                                          ),

                                        ],
                                      )
                                  ),),
                                Padding(
                                  padding: const EdgeInsets.only(left: 10,right: 10,),
                                  child: Text("${modelInformation.mainmenu!.informationPages![index].description}",style: ConstantTextStyle.h3.copyWith(color: Theme.of(context).colorScheme.onBackground.withOpacity(0.87)),
                                    textAlign: TextAlign.justify,
                                  ),
                                ),


                              ],
                            )
                        ) :

                         Column(

                          crossAxisAlignment: CrossAxisAlignment.start,

                          children: [

                            InkWell(
                              onTap: (){
                                  aboutUsBloc.selectedView = aboutUsBloc.selectedView == index ? -1 : index;
                                aboutUsBloc.add(UpdateSelectedViewEvent());
                              },
                              child:
                              Padding(
                                  padding: const EdgeInsets.all(10),
                                  child: Row(
                                    children: [

                                      Expanded(
                                        child: Text("${modelInformation.mainmenu!.informationPages![index].headingTitle}",style: ConstantTextStyle.h2.copyWith(fontWeight: FontWeight.w600,color: Theme.of(context).colorScheme.primary)),
                                      ),
                                      Padding(
                                        padding: EdgeInsets.only(left: 5,top: 5,bottom: 5),
                                        child: Icon(Icons.keyboard_arrow_down_rounded,size: 15,color: Theme.of(context).colorScheme.onBackground.withOpacity(0.60),),
                                      ),

                                    ],
                                  )
                              ),),
                            Divider(height: 1,color: Theme.of(context).colorScheme.onBackground.withOpacity(0.37),),

                          ],
                        );
                    },
                  );
                }

                return Container();
              },
            ),
          )

        ),
      );
  }

}

我想根据选定的视图值更改更新列表视图单个数据块

推荐答案

再创建一个用于更新 Select 的事件:

class UpdateSelectedEvent extends AboutUsEvent {
  final int selectedView;

  UpdateSelectedEvent(this.selectedView);

  @override
  List<Object?> get props => [selectedView];
}

将此活动添加到您的区块:

on<UpdateSelectedEvent>((event, emit) {
    selectedView = event.selectedView;
    if(state is AboutUsLoadedState){
       emit(AboutUsLoadedState(state.modelInformation));
    }
  });

并在onTap上添加更新事件:

onTap: () => aboutUsBloc.add(UpdateSelectedEvent(index)),

Flutter相关问答推荐

想更新ListView上的索引点击块Flutter

DART 3.3:为什么扩展类型不起作用?

如何在Firebase项目中更改包名称和包ID

无法让Riverpod Stream发出新值

Fighter:基于ChangeNotifier状态动态更新AppBar中的图标

Flutter -如何停止块监听程序在后台堆栈中运行

如何将Will POP示波器转换为POP数据抖动的POP示波器

如何实现Flutter 梳理机图像的小量移动

BaseInputfield和Textfield有什么不同?

Flutter中不使用Container是否可以做margin

文本溢出文本框 - Flutter

Flutter Firebase 升级后出现问题? ARC 语义问题 (Xcode): Select 器appleCredentialWithIDToken:rawNonce:fullName:没有已知的类方法

PreferredSizeWidget类不能用作混合,因为它既不是混合类也不是混合

如何正确地将 PageView 设置为 AppBar 的标题?

这是使用导航功能处理从一页到另一页的移动的最佳方式

有什么办法可以消除Flutter 中的传递依赖?

将 String 与 List 元素进行比较时出现问题

Flutter 中出现错误空判断运算符用于空值

Firebase Cloud Messaging (Flutter):订阅主题时出现错误消息

更新未知文档 ID firebase\flutter 中的字段