0

我用 BlocProvider 包装了我的根小部件,并使用 BlocBuilder 根据 ScreensControllerBloc 的状态绘制我的小部件,并使用 BlocProvider.of(context).add(....) 来更改 bloc 的状态,第一个状态是 ScreenIndexState 工作正常在 BlocBuilder 中,但是 SectionIndexState 不起作用,当我尝试在 BlocBuilder 中使用 state.sectionIndex 时,它给了我空值!请帮忙,因为这是一个真正的项目,我无法解决这个问题

编辑:我添加了我使用 BlocBuilder 的代码

这是代码

screen_controller_event.dart

abstract class ScreensControllerEvent {}

class ChangeScreenIndexEvent extends ScreensControllerEvent {
  final int newScreenIndex;

  ChangeScreenIndexEvent({this.newScreenIndex});
}

class ChangeSectionIndexEvent extends ScreensControllerEvent {
  final int newSectionIndex;

  ChangeSectionIndexEvent({this.newSectionIndex});
}

screen_controller_state.dart

abstract class ScreensControllerState {
  int currentScreenIndex;
  int sectionsIndex;
}

class Uninitialized extends ScreensControllerState {
  Uninitialized._();

  factory Uninitialized.set() {
    return Uninitialized._()
      ..currentScreenIndex = 0
      ..sectionsIndex = 1;
  }
}

class ScreenIndexState extends ScreensControllerState {
  ScreenIndexState._();

  factory ScreenIndexState.change(int newIndex) {
    return ScreenIndexState._()..currentScreenIndex = newIndex;
  }
}

class SectionIndexState extends ScreensControllerState {
  SectionIndexState._();

  factory SectionIndexState.change(int newIndex) {
    return SectionIndexState._()..sectionsIndex = newIndex;
  }
}

screen_controller_bloc..dart

class ScreensControllerBloc
    extends Bloc<ScreensControllerEvent, ScreensControllerState> {
  @override
  ScreensControllerState get initialState => Uninitialized.set();

  @override
  Stream<ScreensControllerState> mapEventToState(
    ScreensControllerEvent event,
  ) async* {
    if (event is ChangeScreenIndexEvent) {
      yield* _mapScreenIndexChangeToState(event.newScreenIndex);
    } else if (event is ChangeSectionIndexEvent) {
      yield* _mapSectionIndexChangeToState(event.newSectionIndex);
    }
  }

  Stream<ScreensControllerState> _mapScreenIndexChangeToState(
    int newIndex,
  ) async* {
    yield ScreenIndexState.change(newIndex);
  }

  Stream<ScreensControllerState> _mapSectionIndexChangeToState(
    int newIndex,
  ) async* {
    yield SectionIndexState.change(newIndex);
  }
}

在 main_screen.dart 页面这有效:

class _MainScreenState extends State<MainScreen> {
  final userAccountAdmin = CustomerAccountAdmin.instance;
  ScreensControllerBloc screensControllerBloc = ScreensControllerBloc();

  @override
  Widget build(BuildContext context) {
    screensControllerBloc = BlocProvider.of<ScreensControllerBloc>(context);
    return BlocBuilder<ScreensControllerBloc, ScreensControllerState>(
      builder: (context, state) => SafeArea(
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: Scaffold(
            resizeToAvoidBottomPadding: false,
            appBar: buildAppBar(state.screenIndex),//state.screenIndex works here
            body: buildCurrentScreen(state.screenIndex),
            bottomNavigationBar: BottomNavigationBar(.....),
        ),
       ),
      ),
    );
  }
}

但是在此页面上 state.sectionIndex 不起作用

class _SectionsState extends State<Sections> {
  @override
  Widget build(BuildContext context) {
    final deviceWidth = MediaQuery.of(context).size.width;
    return BlocBuilder<ScreensControllerBloc, ScreensControllerState>(
      builder: (context, state) {
        return Row(
          children: <Widget>[
            Container(
              width: deviceWidth / 3,
              child: Column(
                  children: sections[state.sectionIndex].map((section) {
                return Container(...),
                    color: section.color,
                    onPressed: () {
                      setState(() {
                        for (int i = 0;
                            i < sections[state.sectionIndex].length;
                            i++) {
                          sections[state.sectionIndex][i].color =
                              Color(0xffdddddd);
                        }
                        section.color = Colors.white;
                      });
                    },
                  ),
                );
              }).toList()),
            ),
          ],
        );
      },
    );
  }
4

0 回答 0