0

I am facing this exception when i run my app in debug mode, in normal mode it is not giving me the error. it throws the exception when i click on my textfield.

Execption:

exception = {FlutterError} _MultiChildContainerRenderObject does not meet its 
  constraints.\nConstraints:\n  BoxConstraints(0.0<=w<=1440.0, 0.0<=h<=Infinity)\n
  _stackTrace = null
  diagnostics = {_GrowableList} size = 4
  this = {_MultiChildContainerRenderObject} _MultiChildContainerRenderObject#8e952 
  relayoutBoundary=up7 NEEDS-LAYOUT NEEDS-PAINT
  _depth = 107
  _owner = {PipelineOwner} 
  _parent = {RenderRepaintBoundary} RenderRepaintBoundary#fed68 relayoutBoundary=up6 
NEEDS-LAYOUT NEEDS-PAINT
 _debugDisposed = false
 parentData = {ParentData} <none>
 debugCreator = {DebugCreator} _CalendarMultiChildContainer ← RepaintBoundary ← Stack ← 
 RepaintBoundary ← IndexedSemantics ← _SelectionKeepAlive ← Notification
 _debugDoingThisResize = false
 _debugDoingThisLayout = true
 _debugCanParentUseSize = true
 _debugMutationsLocked = true
 _needsLayout = true
 _relayoutBoundary = {RenderViewport} RenderViewport#2ead6 NEEDS-LAYOUT NEEDS-PAINT
 _doingThisLayoutWithCallback = false
 _constraints = {BoxConstraints} BoxConstraints(0.0<=w<=1440.0, 0.0<=h<=Infinity)
 _debugDoingThisPaint = false
 _wasRepaintBoundary = false
 _layerHandle = {LayerHandle} LayerHandle(DISPOSED)
 _needsCompositingBitsUpdate = false
 _needsCompositing = true
 _needsPaint = true
 _needsCompositedLayerUpdate = false
 _cachedSemanticsConfiguration = null
 _needsSemanticsUpdate = false
 _semantics = null
 _cachedIntrinsicDimensions = null
 _cachedDryLayoutSizes = null
 _computingThisDryLayout = false
 _size = {_DebugSize} Size(1440.0, -30.0)
 _cachedBaselines = null
 _debugActivePointers = 0
 _childCount = 2
 _firstChild = {RenderRepaintBoundary} RenderRepaintBoundary#33f67 NEEDS-LAYOUT NEEDS- 
 PAINT
_lastChild = {RenderRepaintBoundary} RenderRepaintBoundary#3c5b1 NEEDS-LAYOUT NEEDS- 
 PAINT
_hasVisualOverflow = false
_resolvedAlignment = null
_alignment = {AlignmentDirectional} AlignmentDirectional.topStart
_textDirection = {TextDirection} TextDirection.ltr
_fit = {StackFit} StackFit.loose
_clipRectLayer = {LayerHandle} LayerHandle(DISPOSED)
_painter = null
_width = 1440.0
_height = -30.0
_cacheNodes = null

Code:

   class NewReservationScreen extends StatefulWidget {
   const NewReservationScreen({Key? key}) : super(key: key);

   @override
   State<NewReservationScreen> createState() => _NewReservationScreenState();
  }

   class _NewReservationScreenState extends State<NewReservationScreen> {
     late final GlobalKey<FormState> formKey;
     late final TextEditingController driverNameController;
     late final TextEditingController dateController;
     late final TextEditingController fromTimeController;
     late final TextEditingController toTimeController;

           @override
           void initState() {
           super.initState();
           BlocProvider.of<OverviewBloc>(context).add(const OverviewLoadEvent());
           formKey = GlobalKey<FormState>();
           driverNameController = TextEditingController();
           dateController = TextEditingController();
           fromTimeController = TextEditingController();
           toTimeController = TextEditingController();
          }

          @override
          Widget build(BuildContext context) {
          bool isSwitched = false;
         return Container(
         decoration: const BoxDecoration(
         image: DecorationImage(
         image: AssetImage("assets/images/main_background.png"),
         fit: BoxFit.cover,
        ),
       ),
       child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
        title: Container(
        height: MediaQuery.of(context).size.height * 0.04,
        width: MediaQuery.of(context).size.width * 0.5,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/images/vlcone-lila.png"),
          ),
        ),
      ),
      elevation: 0,
      backgroundColor: Colors.transparent,
      centerTitle: true,
      leading: IconButton(
        onPressed: () {
          Navigator.pop(context);
        },
        icon: const Icon(Icons.arrow_back_ios),
      ),
    ),
    body: SingleChildScrollView(
      physics: const BouncingScrollPhysics(),
      child: Container(
        padding: const EdgeInsets.symmetric(
          vertical: 16,
        ),
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.vertical(top: Radius.circular(32)),
          color: Colors.white,
        ),
        height: MediaQuery.of(context).size.height - kToolbarHeight,
        child: SafeArea(
            child: Form(
              key: formKey,
              child: BlocConsumer<OverviewBloc, OverviewState>(
          listener: (context, overviewState) {
              if (overviewState is OverviewFailureState &&
                  overviewState.errorMessage == errorUnauthorizedString) {
                AwesomeDialog(
                    context: context,
                    isDense: false,
                    animType: AnimType.topSlide,
                    dialogType: DialogType.error,
                    body: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 8),
                      child: Text(AppLocalization.of(context)
                          .getTranslatedValues("loginAgain")
                          .toUpperCase()),
                    ),
                    btnOkOnPress: () {
                      BlocProvider.of<LogoutBloc>(context).add(const LogoutUserEvent());
                      Navigator.pushNamedAndRemoveUntil(
                        context,
                        "/login",
                        (route) => false,
                      );
                    }).show();
              }
          },
          builder: (context, overviewState) {
              return BlocListener<PostReservationBloc, PostReservationState>(
                listener: (context, postReservationState) {
                  if (postReservationState is PostReservationSuccessState) {
                    BlocProvider.of<GetReservationBloc> 
                   (context).add(GetReservationLoadEvent(
                        leaseId: BlocProvider.of<OverviewBloc>(context)
                                .state
                                .model
                                ?.data
                                ?.leases
                                ?.firstWhere((element) => element.status == "Active")
                                .id ??
                            ""));
                    Navigator.of(context).pop();
                    Navigator.of(context).pop();
                  }
                  if (postReservationState is PostReservationIsLoadingState) {
                    AwesomeDialog(
                      context: context,
                      isDense: false,
                      animType: AnimType.topSlide,
                      dialogType: DialogType.noHeader,
                      body: const Padding(
                        padding: EdgeInsets.symmetric(horizontal: 8),
                        child: LoadingWidget(),
                      ),
                    ).show();
                  }
                  if (postReservationState is PostReservationFailureState) {
                    AwesomeDialog(
                        context: context,
                        isDense: false,
                        animType: AnimType.topSlide,
                        dialogType: DialogType.error,
                        body: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 8),
                          child: Text(postReservationState.errorMessage),
                        ),
                        btnOkOnPress: () {
                          Navigator.of(context).pop();
                        }).show();
                  }
                },
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 12.0,
                        vertical: 12.0,
                      ),
                      child: Text(
                          
                   AppLocalization.of(context).getTranslatedValues("newReservation"),
                          style: const TextStyle(
                              fontFamily: "Montserrat",
                              color: kViveLaCarBlack,
                              fontSize: 20,
                              fontWeight: FontWeight.w700)),
                    ),
                    const SizedBox(height: 24),
                    TextFieldLabelWidget(
                      label: AppLocalization.of(context).getTranslatedValues("driver"),
                      padding: const EdgeInsets.symmetric(horizontal: 20),
                    ),
                    TextFieldWidget(
                      //textFieldHeight: 42,
                      readOnly: false,
                      padding: const EdgeInsets.symmetric(horizontal: 20),
                      margin: const EdgeInsets.only(bottom: 20),
                      keyboardType: TextInputType.text,
                      controller: driverNameController,
                      validator: (name) {
                        if (name == null || name.isEmpty) {
                          return AppLocalization.of(context)
                              .getTranslatedValues("requiredField");
                        }
                        return null;
                      },
                      onChanged: (value) {},
                    ),
                    // TextFieldWidget(
                    //   controller: driverNameController,
                    //   readOnly: false,
                    //   padding: const EdgeInsets.symmetric(horizontal: 20),
                    //   keyboardType: TextInputType.text,
                    //   onChanged: (value) {},
                    // ),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 20),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            AppLocalization.of(context).getTranslatedValues("allDay"),

                            style: const TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.w600,
                                fontFamily: "Montserrat",
                                color: kViveLaCarBlack),
                          ),
                          Switch.adaptive(
                            onChanged: (bool newValue) {
                              setState(() {
                                isSwitched = !isSwitched;
                              });
                            },
                            value: isSwitched,
                            activeColor: kViveLaCarWhite,
                            inactiveTrackColor: kViveLaCarLightGrey,
                            activeTrackColor: kViveLaCarGreen,
                            inactiveThumbColor: kViveLaCarWhite,
                          ),
                        ],
                      ),
                    ),
                    const SizedBox(height: 10),
                    TextFieldLabelWidget(
                      label: AppLocalization.of(context).getTranslatedValues("date"),
                      padding: const EdgeInsets.symmetric(horizontal: 20),
                    ),
                    TextFieldWidget(
                      validator: (date) {
                        if (date == null || date.isEmpty) {
                          return 
               AppLocalization.of(context).getTranslatedValues("requiredField");
                        }
                        return null;
                      },
                      controller: dateController,
                      keyboardType: TextInputType.text,
                      readOnly: true,
                      suffixIcon: const Icon(
                        Icons.calendar_today,
                        color: kViveLaCarViolet,
                      ),
                      padding: const EdgeInsets.symmetric(horizontal: 20),
                      onTap: () async {
                        DateTime? pickedDate = await showDatePicker(
                          context: context,
                          initialDate: DateTime.now(),
                          firstDate: DateTime.now(),
                          lastDate: DateTime(2101),
                          initialEntryMode: DatePickerEntryMode.input,
                          builder: (context, child) {
                            return Theme(
                              data: Theme.of(context).copyWith(
                                colorScheme: const ColorScheme.light(
                                  primary: kViveLaCarViolet,
                                  onPrimary: Colors.white,
                                  onSurface: Colors.black,
                                ),
                                textButtonTheme: TextButtonThemeData(
                                  style: TextButton.styleFrom(foregroundColor: 
                                  kViveLaCarViolet),
                                ),
                              ),
                              child: child!,
                            );
                          },
                        );
                        if (pickedDate != null) {
                          String formattedDate = DateFormat("yyyy-MM- 
                          dd").format(pickedDate);
                          setState(() {
                            dateController.text = formattedDate;
                          });
                        }
                      },
                    ),
                    const SizedBox(height: 20),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 20),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          TextFieldLabelWidget(
                              label: 
                       AppLocalization.of(context).getTranslatedValues("from")),
                          SizedBox(
                            width: 125,
                            child: TextFieldWidget(
                              validator: (time) {
                                if (time == null || time.isEmpty) {
                                  return AppLocalization.of(context)
                                      .getTranslatedValues("requiredField");
                                }
                                return null;
                              },
                              readOnly: true,
                              suffixIcon: const Icon(
                                Icons.access_time,
                                color: kViveLaCarViolet,
                              ),
                              controller: fromTimeController,
                              onTap: () async {
                                showTimePicker(
                                  context: context,
                                  initialTime: TimeOfDay.now(),
                                  initialEntryMode: TimePickerEntryMode.input,
                                  builder: (context, child) {
                                    return Theme(
                                      data: Theme.of(context).copyWith(
                                        colorScheme: const ColorScheme.light(
                                          primary: kViveLaCarViolet,
                                          onPrimary: Colors.white,
                                          onSurface: Colors.black,
                                        ),
                                        textButtonTheme: TextButtonThemeData(
                                          style: TextButton.styleFrom(
                                              foregroundColor: kViveLaCarViolet),
                                        ),
                                      ),
                                      child: child!,
                                    );
                                  },
                                ).then((dynamic value) {
                                  if (value != null) {
                                    fromTimeController.text = 
                             value.toString().substring(10, 15);
                                  }
                                });
                              },
                            ),
                          ),
                          TextFieldLabelWidget(
                              label: 
                       AppLocalization.of(context).getTranslatedValues("to")),
                          SizedBox(
                            width: 125,
                            child: TextFieldWidget(
                              validator: (time) {
                                if (time == null || time.isEmpty) {
                                  return AppLocalization.of(context)
                                      .getTranslatedValues("requiredField");
                                }
                                return null;
                              },
                              readOnly: true,
                              suffixIcon: const Icon(
                                Icons.access_time,
                                color: kViveLaCarViolet,
                              ),
                              controller: toTimeController,
                              onTap: () async {
                                showTimePicker(
                                  context: context,
                                  initialTime: TimeOfDay.now(),
                                  initialEntryMode: TimePickerEntryMode.input,
                                  builder: (context, child) {
                                    return Theme(
                                      data: Theme.of(context).copyWith(
                                        colorScheme: const ColorScheme.light(
                                          primary: kViveLaCarViolet,
                                          onPrimary: Colors.white,
                                          onSurface: Colors.black,
                                        ),
                                        textButtonTheme: TextButtonThemeData(
                                          style: TextButton.styleFrom(
                                              foregroundColor: kViveLaCarViolet),
                                        ),
                                      ),
                                      child: child!,
                                    );
                                  },
                                ).then((dynamic value) {
                                  if (value != null) {
                                    toTimeController.text = 
                            value.toString().substring(10, 15);
                                  }
                                });
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
                    const SizedBox(height: 40),
                    CustomButtonWidget(
                      color: kViveLaCarViolet,
                      onPress: () {
                        if (formKey.currentState!.validate()) {
                          String fromHour = fromTimeController.text.substring(0, 2);
                          String fromMin = fromTimeController.text.substring(3, 4);
                          String toHour = toTimeController.text.substring(0, 2);
                          String toMin = toTimeController.text.substring(3, 4);
                          TimeOfDay fromTime =
                              TimeOfDay(hour: int.parse(fromHour), minute: 
                             int.parse(fromMin));
                          TimeOfDay toTime =
                              TimeOfDay(hour: int.parse(toHour), minute: 
                          int.parse(toMin));
                          double fromDouble = fromTime.hour + fromTime.minute / 60;
                          double toDouble = toTime.hour + toTime.minute / 60;
                          if (toDouble > fromDouble) {
                            BlocProvider.of<PostReservationBloc>(context)
                                .add(PostReservationLoadEvent(
                              leaseId: overviewState.model?.data?.leases
                                      ?.firstWhere((element) => element.status == 
                                   "Active")
                                      .id ??
                                  "",
                              startTime: "${dateController.text} 
                              ${fromTimeController.text}:00",
                              endTime: "${dateController.text} 
                            ${toTimeController.text}:00",
                            ));
                          } else {
                            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                              content: Text(
                                AppLocalization.of(context)
                                    
                        .getTranslatedValues("yourEndTimeMustBeGreaterThanStartTime"),
                                style: TextStyle(
                                  fontSize: MediaQuery.of(context).size.width < 768 ? 12 
                                    : 22,
                                  fontFamily: "Montserrat",
                                  fontWeight: FontWeight.bold,
                                  color: Colors.white,
                                ),
                              ),
                              backgroundColor: kViveLaCarViolet,
                              duration: const Duration(seconds: 4),
                              action: SnackBarAction(
                                label: "OK",
                                onPressed: () {},
                                textColor: Colors.white,
                              ),
                            ));
                          }
                        }
                      },
                      label: Text(
                        AppLocalization.of(context)
                            .getTranslatedValues("reservation")
                            .toUpperCase(),
                        style: TextStyle(
                          color: kViveLaCarWhite,
                          fontSize: MediaQuery.of(context).size.width < 768 ? 15 : 25,
                          fontWeight: FontWeight.bold,
                          fontFamily: 'Montserrat',
                        ),
                      ),
                    ),
                  ],
                ),
              );
          },
        ),
            )),
      ),
    ),
  ),
);
}

             @override
             void dispose() {
             dateController.dispose();
             fromTimeController.dispose();
             toTimeController.dispose();
             super.dispose();
   }
}

Code of TextField widget:

            class TextFieldWidget extends StatelessWidget {
            const TextFieldWidget(
            {Key? key,
            this.controller,
            this.onTap,
            this.onChanged,
            this.keyboardType,
            required this.readOnly,
            this.suffixIcon,
            this.padding,
            this.validator,
            this.margin,
            this.maxLines,
            this.hintText,
            this.obscureText = false})
            : assert(obscureText !=null),
             super(key: key);

          final TextEditingController? controller;
          final bool readOnly;
          final TextInputType? keyboardType;
          final void Function(String)? onChanged;
          final void Function()? onTap;
          final Widget? suffixIcon;
          final EdgeInsetsGeometry? padding;
          final String? Function(String?)? validator;
          final EdgeInsetsGeometry? margin;
          final int? maxLines;
          final String? hintText;
          final bool obscureText;

          @override
          Widget build(BuildContext context) {
          return Container(
          padding: padding,
          margin: margin,
          //height: 42,
          child: TextFormField(
           style: const TextStyle(fontFamily: "Montserrat"),
           cursorColor: kViveLaCarViolet,
           controller: controller,
           obscureText: obscureText,
           keyboardType: keyboardType,
           onChanged: onChanged,
           readOnly: readOnly,
           onTap: onTap,
           validator: validator,
           maxLines: maxLines,
           decoration: InputDecoration(
           hintText: hintText,
           suffixIcon: suffixIcon,
           suffixIconColor: kViveLaCarViolet,
           focusedBorder: OutlineInputBorder(
           borderRadius: BorderRadius.circular(13),
           borderSide: const BorderSide(color: kViveLaCarViolet),
         ),
           disabledBorder: const OutlineInputBorder(
           borderRadius: BorderRadius.all(Radius.circular(13)),
           borderSide: BorderSide(
          width: 1,
          color: kViveLaCarDarkGrey,
        ),
      ),
      border: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(13)),
        borderSide: BorderSide(
          width: 1,
        ),
      ),
      errorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(13),
        borderSide: const BorderSide(
          color: Colors.redAccent,
          width: 1.0,
        ),
      ),
      focusedErrorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(13),
          borderSide: const BorderSide(
            color: Colors.redAccent,
          )),
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(13),
        borderSide: const BorderSide(
          color: kViveLaCarDarkGrey,
          width: 1.0,
        ),
      ),
      contentPadding: const EdgeInsets.all(10),
      errorMaxLines: 2,
      errorStyle: const TextStyle(fontFamily: "Montserrat")
    ),
  ),
);
}
}

        
eamirho3ein
  • 16,619
  • 2
  • 12
  • 23
shah Jahan
  • 23
  • 3

0 Answers0