flutter 如何修复此UI?

sqyvllje  于 2023-03-03  发布在  Flutter
关注(0)|答案(1)|浏览(91)

我正试着做一个表单页面,在那里我添加了一堆文本字段。
我已经添加了下面的视频.. https://drive.google.com/file/d/1WUqBAg6GnyE0g6rW0xFFKgIgO1AK0iAc/view?usp=sharing
正如您所看到的,当我添加一个新表时,它会创建一个表,但仅在特定的空间中创建,我希望它与上面的表一样正常,按钮向下。
下面是代码:

class AddVenueScreen extends StatefulWidget {
  const AddVenueScreen({Key? key, required this.hasBackButton})
      : super(key: key);
  final bool hasBackButton;

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

class _AddVenueScreenState extends State<AddVenueScreen> {
  @override
  void initState() {
    super.initState();
  }

  final TextEditingController _restaurantNameController = TextEditingController();
  final TextEditingController _fromTimeController = TextEditingController();
  final TextEditingController _toTimeController = TextEditingController();
  final TextEditingController _totalCapacityController = TextEditingController();
  final TextEditingController _malePriceController = TextEditingController();
  final TextEditingController _femalePriceController = TextEditingController();
  final TextEditingController _couplePriceController = TextEditingController();
  bool submitting = false;
  bool canSubmit = false;
  File? restaurantImage;
  List<_TableGroupController> _tableGroupControllers = [];
  List<TextField> _tableNameFields = [];
  List<TextField> _tablePaxFields = [];
  List<TextField> _tablePriceFields = [];
  var ruid = Uuid();
  
  void _canSubmit() {
    if (restaurantImage != null && _restaurantNameController.text.isNotEmpty &&
        _fromTimeController.text.isNotEmpty &&
        _toTimeController.text.isNotEmpty &&
        _totalCapacityController.text.isNotEmpty) {
      setState(() {
        canSubmit = true;
      });
    } else {
      setState(() {
        canSubmit = false;
      });
    }
  }

  void storeData() async {
    final ap = Provider.of<AuthProvider>(context, listen: false);
    RestaurantModel restaurantModel = RestaurantModel(
      restaurantName: _restaurantNameController.text.trim(),
      restaurantUserName: /*ap.userModel.name*/ "",
      restaurantLocation: "",
      rid: ruid.v1(),
      restaurantImage: restaurantImage.toString(),
      restaurantPhoneNumber: "",
      openingTime: _fromTimeController.text.trim(),
      closingTime: _toTimeController.text.trim(),
      // reviews: "",
      // ratings: "",
      salt: "",
      approvalStatus: "",
      restaurantCreatedAt: DateTime.now().toIso8601String(),
      updatedAt: "",
    );
    if (restaurantImage != null) {
      ap.saveRestaurantDataToFirebase(
        context: context,
        restaurantModel: restaurantModel,
        restaurantPic: restaurantImage!,
        onSuccess: () {
          /*ap.saveUserDataToSP().then(
                (value) => ap.setSignIn().then(
                    (value) {
                  Navigator.pushNamedAndRemoveUntil(context, RouteGenerator.appNavigation, (_) => false);
                  setState(() {
                    DataConstants.onboardingNotDoneFlag = true;
                    _saveOnBoarding();
                  });
                }
            ),
          );*/
          NavigationHelper().pushAndRemoveU(context, const AppNavigation());
        },
      );
    } else {
      SnackbarAlert().basicSnackbar(
          context: context, message: 'Please upload a restaurant image!');
    }
  }

  void selectImage() async {
    restaurantImage = await pickImage(context);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      appBar: AppBar(
        toolbarHeight: 72.0,
        automaticallyImplyLeading: false,
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        centerTitle: false,
        title: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 8.0),
          child: Row(
            children: [
              widget.hasBackButton
                  ? GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                      },
                      child: const Icon(Icons.arrow_back))
                  : const SizedBox.shrink(),
              const Text(' Add New Venue', style: TextStyles.h2),
            ],
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: SizedBox(
              height: MediaQuery.of(context).size.height,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.max,
                children: [
                  InkWell(
                      onTap: () => selectImage(),
                      child: restaurantImage == null
                          ? Container(
                              height: 150,
                              width: MediaQuery.of(context).size.width - 30,
                              decoration: BoxDecoration(
                                  color: Colors.white12,
                                  border: Border(),
                                  borderRadius: BorderRadius.circular(10)),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment:
                                    CrossAxisAlignment.center,
                                children: const [
                                  Icon(
                                    Icons.add,
                                    size: 20,
                                  ),
                                  Text("Add Image here"),
                                ],
                              ),
                            )
                          : Container(
                              height: 150,
                              width: MediaQuery.of(context).size.width - 30,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(10),
                                image: DecorationImage(
                                    image: FileImage(restaurantImage!),
                                    fit: BoxFit.cover),
                              ),
                            ) /*CircleAvatar(
                      backgroundImage: FileImage(restaurantImage!),
                      radius: 50,
                    ),*/
                      ),
                  const SizedBox(
                    height: 10,
                  ),
                  Text('Name', style: TextStyles.bodyText),
                  const SizedBox(height: 8.0),
                  EntryField(
                    controller: _restaurantNameController,
                    hint: 'Enter Club Name',
                    textInputAction: TextInputAction.send,
                    textInputType: TextInputType.text,
                    onChanged: (_) {
                      _canSubmit();
                    },
                  ),
                  const SizedBox(height: 12.0),
                  Row(
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('Timings', style: TextStyles.bodyText),
                            const SizedBox(height: 8.0),
                            Row(
                              children: [
                                Expanded(
                                  child: EntryField(
                                    onChanged: (_) {
                                      _canSubmit();
                                    },
                                    controller: _fromTimeController,
                                    hint: 'From',
                                    textInputAction: TextInputAction.send,
                                    textInputType: TextInputType.text,
                                  ),
                                ),
                                const SizedBox(
                                  width: 5,
                                ),
                                Expanded(
                                  child: EntryField(
                                    onChanged: (_) {
                                      _canSubmit();
                                    },
                                    controller: _toTimeController,
                                    hint: 'To',
                                    textInputAction: TextInputAction.send,
                                    textInputType: TextInputType.text,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(width: 8.0),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('Total Capacity',
                                style: TextStyles.bodyText),
                            const SizedBox(height: 8.0),
                            EntryField(
                                onChanged: (_) {
                                  _canSubmit();
                                },
                                controller: _totalCapacityController,
                                hint: 'Ex 20',
                                textInputAction: TextInputAction.send,
                                textInputType: TextInputType.number,
                                textInputFormatters: <TextInputFormatter>[
                                  FilteringTextInputFormatter.digitsOnly
                                ]),
                          ],
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 12.0),
                  Text('Guest Pricing', style: TextStyles.bodyText),
                  const SizedBox(height: 8.0),
                  Column(
                    children: [
                      Row(
                        children: [
                          const Expanded(
                              child: Text(
                            "Male",
                          )),
                          Expanded(
                            flex: 1,
                            child: EntryField(
                              controller: _malePriceController,
                              hint: '₹ 999',
                              textInputAction: TextInputAction.send,
                              textInputType: TextInputType.text,
                              onChanged: (_) {
                                _canSubmit();
                              },
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 5.0),
                      Row(
                        children: [
                          const Expanded(
                              child: Text(
                            "Female",
                          )),
                          Expanded(
                            flex: 1,
                            child: EntryField(
                              controller: _femalePriceController,
                              hint: '₹ 999',
                              textInputAction: TextInputAction.send,
                              textInputType: TextInputType.text,
                              onChanged: (_) {
                                _canSubmit();
                              },
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 5.0),
                      Row(
                        children: [
                          const Expanded(
                              child: Text(
                            "Couple",
                          )),
                          Expanded(
                            flex: 1,
                            child: EntryField(
                              controller: _couplePriceController,
                              hint: '₹ 999',
                              textInputAction: TextInputAction.send,
                              textInputType: TextInputType.text,
                              onChanged: (_) {
                                _canSubmit();
                              },
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                  const SizedBox(height: 12.0),
                  Text('Table Details', style: TextStyles.bodyText),
                  const SizedBox(height: 8.0),
                  Expanded(child: SizedBox(height: 400, child: Expanded(child: _listView()))),
                  InkWell(
                    onTap: () {
                      final group = _TableGroupController();

                      final nameField = _generateTextField(group.tableName, "Table Name");
                      final telField = _generateTextField(group.tablePax, "PaX");
                      final addressField = _generateTextField(group.tablePrice, "₹ Price");

                      setState(() {
                        _tableGroupControllers.add(group);
                        _tableNameFields.add(nameField);
                        _tablePaxFields.add(telField);
                        _tablePriceFields.add(addressField);
                      });
                    },
                    child: Container(
                      height: 40,
                      decoration: BoxDecoration(
                        border: Border.all(
                          width: 0.5,
                          color: Colors.grey
                        ),
                        borderRadius: BorderRadius.circular(10)
                      ),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: const [
                          Icon(Icons.add),
                          Text("Add New Table"),
                        ],
                      ),
                    ),
                  ),
                  const SizedBox(height: 10,),
                  CustomButton(
                    onTap: () {
                       if (canSubmit) {
                        if (!submitting) {
                          _canSubmit();
                        }
                      } else {
                        SnackbarAlert().basicSnackbar(
                            context: context,
                            message: 'Please fill all the fields correctly.');
                      }
                      storeData();
                    },
                    label: 'Submit',
                    loading: submitting,
                    color: Colors.transparent,
                    borderColor: canSubmit
                        ? ApplicationColors.primaryColor
                        : ApplicationColors.offWhite,
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  TextField _generateTextField(TextEditingController controller, String hint) {
    final theme = Theme.of(context);
    return TextField(
      controller: controller,
      decoration: InputDecoration(
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12.0),
          borderSide: BorderSide.none,
        ),
        filled: true,
        fillColor: ApplicationColors.buttonFillColor,
        hintText: hint,
        // prefix: prefix,
        hintStyle: theme.textTheme.bodyText2!
            .copyWith(color: theme.hintColor, fontSize: 15),
      ),
    );/*EntryField(
      controller: controller,
      hint: hint,
      textInputAction: TextInputAction.send,
      textInputType: TextInputType.text,
      onChanged: (_) {
        _canSubmit();
      },
    );*/
  }

  Widget _listView() {
    final children = [
      for (var i = 0; i < _tableGroupControllers.length; i++)
        Container(
          margin: EdgeInsets.all(5),
          child: InputDecorator(
            child: Row(
              children: [
                Expanded(flex: 2,child: _tableNameFields[i]),
                const SizedBox(width: 5,),
                Expanded(flex: 1,child: _tablePaxFields[i]),
                const SizedBox(width: 5,),
                Expanded(flex: 1,child: _tablePriceFields[i]),
              ],
            ),
            decoration: InputDecoration(
              labelText: i.toString(),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
            ),
          ),
        )
    ];
    return SingleChildScrollView(
      child: Column(
        children: children,
      ),
    );
  }
}

class _TableGroupController {
  TextEditingController tableName = TextEditingController();
  TextEditingController tablePax = TextEditingController();
  TextEditingController tablePrice = TextEditingController();
  void dispose() {
    tableName.dispose();
    tablePax.dispose();
    tablePrice.dispose();
  }
}

请让我知道我哪里做错了,我能做些什么来解决这个问题。谢谢!

osh3o9ms

osh3o9ms1#

您可以删除此SingleChildScrollView,因为您正在父级中使用相同的小部件。
请用这个
'窗口小部件列表视图(){

return Column(
    mainAxisSize: MainAxisSize.min,
    children: children,
  
);

}`
而不是小部件列表视图(){

return SingleChildScrollView(
  child: Column(
    children: children,
  ),
);

}
同时删除这些展开的小部件和展开的高度(子级:大小框(高度:400,儿童:扩展(子级:_列表视图()))),

相关问题