繁体   English   中英

将底部模态表提取到单独的小部件中会使 BlocProvider 颤动

[英]Extracting bottom modal sheet into separate widget looses BlocProvider flutter

首先,我希望你们在这个全球非常艰难的时期一切顺利。 我正在重构我的屏幕代码的一部分,但我坚持这一点。 我有一个底部模式表,我提取到一个单独的文件中,以保持我的MapScreen UI 代码简短明了,但出了点问题。 我得到的错误是BlocProvider.of() called with a context that does not contain a Bloc of type TrackingBloc. 我必须在单独的文件中声明 BlocProvider 的那个人吗? 它不是通过context:context参数传递给小部件吗? 然后我尝试添加它,但仍然出现错误。 你能看出我做错了什么吗? 一如既往,非常感谢您的时间和帮助,尤其是在这个非常困难的时期。

UI 模态底页:

showModalBottomSheet(
                              isScrollControlled: true,
                              context: context,
                              builder: (modal) {
//                                return AndroidTrackingSheet(routeName,
//                               isTracking, _textEditingController);
                                return Container(
                                  color: Color(0xff757575),
                                  child: SingleChildScrollView(
                                    child: Container(
                                      padding: EdgeInsets.only(
                                          left: 20,
                                          right: 20,
                                          bottom: MediaQuery.of(modal)
                                              .viewInsets
                                              .bottom),
                                      decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(20)),
                                      ),
                                      child: Center(
                                        child: Column(
//                                        mainAxisAlignment:
//                                            MainAxisAlignment.center,
                                          crossAxisAlignment:
                                              CrossAxisAlignment.stretch,
                                          children: <Widget>[
                                            SizedBox(
                                              height: 10,
                                            ),
                                            Text(
                                              'Nuovo percorso',
                                              textAlign: TextAlign.center,
                                              style: TextStyle(
                                                fontSize: 25,
                                                color: Colors.orangeAccent,
                                                fontWeight: FontWeight.w400,
                                              ),
                                            ),
                                            SizedBox(
                                              height: 10,
                                            ),
                                            Text(
                                              'Inserisci un nome per il tuo nuovo percorso, e scegli Inizia tracking. Quando sarai arrivato a destinazione premi di nuovo il bottone Tracking e scegli Fine tracking.',
                                              textAlign: TextAlign.center,
                                              style: TextStyle(
                                                  fontSize: 18,
                                                  color: Colors.black,
                                                  fontWeight: FontWeight.w400),
                                            ),
                                            SizedBox(
                                              height: 10,
                                            ),
                                            TextField(
                                              controller:
                                                  _textEditingController,
                                              autofocus: true,
                                              textAlign: TextAlign.center,
                                              showCursor: true,
                                              decoration: InputDecoration(
                                                hintText: isTracking
                                                    ? routeName
                                                    : 'nome percorso',
                                                labelStyle: TextStyle(
                                                    fontSize: 18,
                                                    color: Colors.black,
                                                    fontWeight:
                                                        FontWeight.w100),
                                                border: OutlineInputBorder(),
//                                              focusColor:
//                                                  Colors.lightGreenAccent,
                                                focusedBorder:
                                                    OutlineInputBorder(
                                                  borderSide: BorderSide(
                                                    color: Colors.orange,
                                                    width: 1,
                                                  ),
                                                ),
                                              ),
                                            ),
                                            SizedBox(
                                              height: 10,
                                            ),
                                            FlatButton(
                                              color: Colors.orangeAccent,
                                              child: Text(
                                                isTracking
                                                    ? "Fine tracking"
                                                    : 'Inizia tracking',
                                                style: TextStyle(
                                                    fontSize: 18,
                                                    color: Colors.white),
                                              ),
                                              onPressed: () {
                                                print(
                                                    "Action 2 is been clicked");
                                                routeName =
                                                    _textEditingController.text;
                                                Navigator.pop(context);
                                                isTracking = !isTracking;
                                                BlocProvider.of<TrackingBloc>(
                                                        context)
                                                    .add(StartStopTracking());
                                              },
                                            ),
                                            SizedBox(
                                              height: 10,
                                            ),
                                            FlatButton(
                                              color: Colors.redAccent,
                                              child: Text(
                                                'Cancella',
                                                style: TextStyle(
                                                    fontSize: 18,
                                                    color: Colors.white),
                                              ),
                                              onPressed: () {
                                                Navigator.pop(context);
                                              },
                                            ),
                                            SizedBox(
                                              height: 10,
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                                );
                              });

单独的小部件模式表:

    class AndroidTrackingSheet extends StatelessWidget {
  TextEditingController _textEditingController;
  bool isTracking;
  String routeName;
  AndroidTrackingSheet(
      this.routeName, this.isTracking, this._textEditingController);
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xff757575),
      child: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.only(
              left: 20,
              right: 20,
              bottom: MediaQuery.of(context).viewInsets.bottom),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(20)),
          ),
          child: Center(
            child: Column(
//                                        mainAxisAlignment:
//                                            MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                SizedBox(
                  height: 10,
                ),
                Text(
                  'Nuovo percorso',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 25,
                    color: Colors.orangeAccent,
                    fontWeight: FontWeight.w400,
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                Text(
                  'Inserisci un nome per il tuo nuovo percorso, e scegli Inizia tracking. Quando sarai arrivato a destinazione premi di nuovo il bottone Tracking e scegli Fine tracking.',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      fontSize: 18,
                      color: Colors.black,
                      fontWeight: FontWeight.w400),
                ),
                SizedBox(
                  height: 10,
                ),
                TextField(
                  controller: _textEditingController,
                  autofocus: true,
                  textAlign: TextAlign.center,
                  showCursor: true,
                  decoration: InputDecoration(
                    hintText: isTracking ? routeName : 'nome percorso',
                    labelStyle: TextStyle(
                        fontSize: 18,
                        color: Colors.black,
                        fontWeight: FontWeight.w100),
                    border: OutlineInputBorder(),
//                                              focusColor:
//                                                  Colors.lightGreenAccent,
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.orange,
                        width: 1,
                      ),
                    ),
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                FlatButton(
                  color: Colors.orangeAccent,
                  child: Text(
                    isTracking ? "Fine tracking" : 'Inizia tracking',
                    style: TextStyle(fontSize: 18, color: Colors.white),
                  ),
                  onPressed: () {
                    print("Action 2 is been clicked");
                    routeName = _textEditingController.text;
                    Navigator.pop(context);
                    isTracking = !isTracking;
                    BlocProvider.of<TrackingBloc>(context)
                        .add(StartStopTracking());
                  },
                ),
                SizedBox(
                  height: 10,
                ),
                FlatButton(
                  color: Colors.orangeAccent,
                  child: Text(
                    'Cancella',
                    style: TextStyle(fontSize: 18, color: Colors.white),
                  ),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

与 bloc 提供程序分开的底部工作表:

class AndroidTrackingBottomSheet extends StatelessWidget {
  TextEditingController _textEditingController;
  bool isTracking;
  String routeName;
  AndroidTrackingBottomSheet(
      this.routeName, this.isTracking, this._textEditingController);
  @override
  Widget build(BuildContext context) {
    return BlocProvider<TrackingBloc>(
      create: (context) => TrackingBloc(),
      child: Container(
        color: Color(0xff757575),
        child: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.only(
                left: 20,
                right: 20,
                bottom: MediaQuery.of(context).viewInsets.bottom),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(20)),
            ),
            child: Center(
              child: Column(
//                                        mainAxisAlignment:
//                                            MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  SizedBox(
                    height: 10,
                  ),
                  Text(
                    'Nuovo percorso',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 25,
                      color: Colors.orangeAccent,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Text(
                    'Inserisci un nome per il tuo nuovo percorso, e scegli Inizia tracking. Quando sarai arrivato a destinazione premi di nuovo il bottone Tracking e scegli Fine tracking.',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 18,
                        color: Colors.black,
                        fontWeight: FontWeight.w400),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  TextField(
                    controller: _textEditingController,
                    autofocus: true,
                    textAlign: TextAlign.center,
                    showCursor: true,
                    decoration: InputDecoration(
                      hintText: isTracking ? routeName : 'nome percorso',
                      labelStyle: TextStyle(
                          fontSize: 18,
                          color: Colors.black,
                          fontWeight: FontWeight.w100),
                      border: OutlineInputBorder(),
//                                              focusColor:
//                                                  Colors.lightGreenAccent,
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: Colors.orange,
                          width: 1,
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  FlatButton(
                    color: Colors.orangeAccent,
                    child: Text(
                      isTracking ? "Fine tracking" : 'Inizia tracking',
                      style: TextStyle(fontSize: 18, color: Colors.white),
                    ),
                    onPressed: () {
                      print("Action 2 is been clicked");
                      routeName = _textEditingController.text;
                      Navigator.pop(context);
                      isTracking = !isTracking;
                      BlocProvider.of<TrackingBloc>(context)
                          .add(StartStopTracking());
                    },
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  FlatButton(
                    color: Colors.orangeAccent,
                    child: Text(
                      'Cancella',
                      style: TextStyle(fontSize: 18, color: Colors.white),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                  SizedBox(
                    height: 10,
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

我终于发现 Bloc 必须通过BlocProvider.value提供给底部工作表,而不是在小部件文件中,所以工作代码是:

showModalBottomSheet(
                              isScrollControlled: true,
                              context: context,
                              builder: (modal) {
                                return BlocProvider.value(
                                  value: BlocProvider.of<TrackingBloc>(context),
                                  child: AndroidTrackingBottomSheet(
                                      widget.key,
                                      routeName,
                                      isTracking,
                                      _textEditingController),
                                );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM