1

when I am using this inherited widget sometimes I had getting the issue , multiple widgets using same global key , I don't know why it shows the issue , some times in the release build app shows a full black screen , is this black screen shown because of this issue?, anyone know please help I'm new to flutter

class StateWidget extends StatefulWidget {
  final Widget child;

  const StateWidget({
    Key? key,
    required this.child,
  }) : super(key: key);

  @override
  _StateWidgetState createState() => _StateWidgetState();
}

class _StateWidgetState extends State<StateWidget> {
  CoreState state = CoreState();


  void strengthCompleted(bool strengthCompleted) {
    final newState = state.copy(strengthCompleted: strengthCompleted);
    setState(() => state = newState);
  }

  void startExerciseClicked(bool startExerciseClicked) {
    final newState = state.copy(startExerciseClicked: startExerciseClicked);
    setState(() => state = newState);
  }

  void sessionCompleted(bool sessionCompleted) {
    final newState = state.copy(sessionCompleted: sessionCompleted);
    setState(() => state = newState);
  }

  void sessionValue(String sessionValue) {
    final newState = state.copy(sessionValue: sessionValue);
    setState(() => state = newState);
  }

  void recoveryDay(String recoveryDay)
  {
    final newState = state.copy(recoveryDay: recoveryDay);
    setState(() => state = newState);
  }

  @override
  Widget build(BuildContext context) {
    return StateInheritedWidget(
      child: widget.child,
      state: state,
      stateWidget: this,
    );
  }
}

class StateInheritedWidget extends InheritedWidget {
  final CoreState state;
  final _StateWidgetState stateWidget;

  const StateInheritedWidget({
    Key? key,
    required Widget child,
    required this.state,
    required this.stateWidget,
  }) : super(key: key, child: child);

  static _StateWidgetState of(BuildContext context) => context
      .dependOnInheritedWidgetOfExactType<StateInheritedWidget>()!
      .stateWidget;

  @override
  bool updateShouldNotify(StateInheritedWidget oldWidget) =>
      oldWidget.state != state;
}

here below I am showing the error log after getting this issue

enter image description here

here I am showing the mainScreen code
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

class MainScreen extends StatefulWidget {

  MainScreen(
      {Key? key})
      : super(key: key);

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
    

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose

    WidgetsBinding.instance.removeObserver(this);
    _controller?.dispose();
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);

    // if (state == AppLifecycleState.inactive ||
    //     state == AppLifecycleState.detached) return;
    // final appInactive = state == AppLifecycleState.inactive;
    final appDetached = state == AppLifecycleState.detached;
    final isBackground = state == AppLifecycleState.paused;
    if (appDetached) {
   
    }

    if (isBackground) {
      print('app is in background');
    }


  }

  @override
  Widget build(BuildContext context) {
   
    final sessionCompleted =
        StateInheritedWidget
            .of(context)
            .state
            .sessionCompleted;
 
    final exerciseCompleted =
        StateInheritedWidget
            .of(context)
            .state
            .exerciseCompleted;
  
    final sessionValue = StateInheritedWidget
        .of(context)
        .state
        .sessionValue;
    
    final provider = StateInheritedWidget.of(context);
    SizeConfig().init(context);

    print('SizeConfig.blockSizeVertical');
    print(SizeConfig.blockSizeVertical);
    print('SizeConfig.blockSizeHorizontal');
    print(SizeConfig.blockSizeHorizontal);

    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: Colors.white,
      drawer: NavigationDrawer(),
      body: SafeArea(
        child: SizedBox(
          height: MediaQuery
              .of(context)
              .size
              .height,
          width: MediaQuery
              .of(context)
              .size
              .width,
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
               Positioned(
                            left: 0,
                            top: SizeConfig.blockSizeHorizontal * .75,
                            bottom: SizeConfig.blockSizeHorizontal * .75,
                            child: InkWell(
                              onTap: () {
                                _scaffoldKey.currentState?.openDrawer();
                              },
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.grey[100], // border color
                                  shape: BoxShape.circle,
                                ),
                                child: Image.asset(
                                  'assets/images/navdrawerimage.png',
                                  width: SizeConfig.blockSizeHorizontal * 5.25,
                                  height: SizeConfig.blockSizeHorizontal * 5.25,
                                ),
                              ),
                            ),
                          ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Nns_ninteyFIve
  • 439
  • 4
  • 12

0 Answers0