Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
22 kB
1
Indexable
Never
import 'package:Seekqua/util/missingInfo.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_masked_text2/flutter_masked_text2.dart';
import 'package:Seekqua/components/CloseIcon.dart';
import 'package:Seekqua/enums/colors.dart';
import 'package:Seekqua/models/account.model.dart';
import 'package:Seekqua/pages/createListing/RentalPhotos.dart';
import 'package:Seekqua/providers/RentalItemProvider.dart';
import 'package:Seekqua/services/database.dart';
import 'package:Seekqua/util/CategoryInfo.dart';
import 'package:Seekqua/util/toast.dart';
// import 'package:platform_alert_dialog/platform_alert_dialog.dart';
import 'package:provider/provider.dart';

class RentalInfo extends StatefulWidget {
  const RentalInfo({
    Key key,
    @required this.bottomNavTapped,
  }) : super(key: key);

  final Function bottomNavTapped;

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

class _RentalInfoState extends State<RentalInfo> {
  final descriptionController =
      TextEditingController.fromValue(TextEditingValue.empty);
  final titleController =
      TextEditingController.fromValue(TextEditingValue.empty);
  final categoryController =
      TextEditingController.fromValue(TextEditingValue.empty);

  int descriptionCount = 0;
  int titleCount = 0;

  int titleLimit = 60;
  int descriptionLimit = 500;

  final focusNode = FocusNode();
  MoneyMaskedTextController priceController = MoneyMaskedTextController(
      decimalSeparator: '.', thousandSeparator: ',', leftSymbol: ' \$');

  List<CategoryInfo> categories = [];

  @override
  void initState() {
    final AccountModel account =
        Provider.of<AccountModel>(context, listen: false);
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (account.displayName == null || account.displayName.isEmpty) {
        showMissingInfoModal(context, () => Navigator.of(context).pop());
      }
    });
    descriptionCount = 0;
    descriptionController.addListener(() {
      setState(() => descriptionCount = descriptionController.text.length);
    });
    titleController.addListener(() {
      setState(() => titleCount = titleController.text.length);
    });
    final RentalItemProvider rentalItem =
        Provider.of<RentalItemProvider>(context, listen: false);
    rentalItem.clearRentalItem();
    if (rentalItem.price != null) {
      priceController.updateValue(rentalItem.price);
    }
    priceController.addListener(() {
      rentalItem.setPrice(priceController.numberValue);
    });
    focusNode.addListener(() {
      if (!focusNode.hasFocus && priceController.numberValue < 1) {
        showToast(msg: 'Price must be at least \$1');
      }
    });
    fetchCategories(account.uid);
    super.initState();
  }

  @override
  void dispose() {
    descriptionController.removeListener(() {});
    descriptionController.dispose();
    titleController.removeListener(() {});
    titleController.dispose();
    categoryController.removeListener(() {});
    categoryController.dispose();
    priceController.dispose();
    focusNode.dispose();
    super.dispose();
  }

  fetchCategories(uid) async {
    categories =
        await DatabaseService(uid: uid).getCategories(includeAll: false);
    setState(() {
      categories = categories;
    });
  }

  storeCategories(String category) {
    final RentalItemProvider rentalItem =
        Provider.of<RentalItemProvider>(context, listen: false);
    if (category != null) {
      rentalItem.setTaggedCategory(category);
    }
  }

  bool isFormValid(RentalItemProvider rentalItem) {
    bool valid = rentalItem.taggedCategory != null &&
        descriptionCount > 1 &&
        titleCount > 1 &&
        rentalItem.price != null &&
        rentalItem.price > 0.99;
    return valid;
  }

  Future<String> showCategories(
      RentalItemProvider rentalItem, AccountModel account) async {
    Size size = MediaQuery.of(context).copyWith().size;
    updateCategory(String _category) {
      rentalItem.setTaggedCategory(_category);
    }

    String category = rentalItem.taggedCategory;

    await showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return StatefulBuilder(
          builder: (context, setState) {
            close() {
              if (Navigator.canPop(context)) {
                Navigator.of(context).pop();
              }
            }

            return AlertDialog(
              title: const Text('Select a category'),
              content: SingleChildScrollView(
                child: SizedBox(
                  height: size.height * 0.5,
                  child: Column(
                    children: <Widget>[
                      for (CategoryInfo _category in categories)
                        GestureDetector(
                          behavior: HitTestBehavior.translucent,
                          onTap: () {
                            setState(() => {category = _category.title});
                            updateCategory(_category.title);
                            close();
                            // Future.delayed(Duration(milliseconds: 500))
                            //     .then(close()),
                          },
                          child: SizedBox(
                            height: (size.height * .5) / categories.length,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  _category.title,
                                  style: TextStyle(
                                    color: KalanaColors.text,
                                    fontSize: 16,
                                  ),
                                ),
                                if (category == _category.title)
                                  Icon(
                                    Icons.check,
                                    color: KalanaColors.secondary,
                                  ),
                              ],
                            ),
                          ),
                        ),
                    ],
                  ),
                ),
              ),
              actions: <Widget>[],
            );
          },
        );
      },
    );
    return category;
  }

  updateCategory(RentalItemProvider rentalItem, AccountModel account) async {
    String category = await showCategories(rentalItem, account);
    if (category != null) {
      setState(() => {
            categoryController.text = category,
          });
    }
  }

  @override
  Widget build(BuildContext context) {
    final RentalItemProvider rentalItem =
        Provider.of<RentalItemProvider>(context);
    final AccountModel account = Provider.of<AccountModel>(context);

    Size size = MediaQuery.of(context).copyWith().size;
    double padding = size.width * .025;
    double safePadding = MediaQuery.of(context).copyWith().padding.bottom +
        MediaQuery.of(context).copyWith().padding.top;
    AppBar appBar = AppBar(
      leading: GestureDetector(
        child: const CloseIcon(isBack: true),
        onTap: () => {
          Navigator.of(context).pop(),
        },
      ),
      backgroundColor: Colors.transparent,
      elevation: 0,
    );
    return 
    // GestureDetector(
    //   onTap: () {
    //     FocusScope.of(context).requestFocus(FocusNode());
    //   },
      // child: 
      // Stack(
      //   children: [
      //     Container(
      //       width: size.width,
      //       height: size.height,
      //       decoration: const BoxDecoration(
      //         image: DecorationImage(
      //           fit: BoxFit.cover,
      //           image: AssetImage(
      //             'assets/images/fog_mountains.jpg',
      //           ),
      //         ),
      //       ),
      //     ),
          Scaffold(
            backgroundColor: Colors.transparent,
            body: Container(
              width: size.width,
              height: size.height,
              decoration: BoxDecoration(color: Colors.transparent),
              child: 
              // SafeArea(
                // child: 
                SingleChildScrollView(
                  
                  scrollDirection: Axis.vertical,
                  child: Column(
                    children: [
                      appBar,
                      Center(
                        child: Container(
                          width: size.width * .90,
                          height: size.height -
                              appBar.preferredSize.height -
                              safePadding,
                          decoration: BoxDecoration(
                            color: KalanaColors.light,
                            borderRadius: BorderRadius.circular(10),
                            boxShadow: const [
                              BoxShadow(
                                color: Color.fromRGBO(0, 0, 0, .5),
                                blurRadius: 10,
                                spreadRadius: 0,
                                offset: Offset(
                                  4,
                                  4,
                                ),
                              ),
                            ],
                          ),
                          padding: EdgeInsets.all(size.width * 0.085),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              // FittedBox(
                              //   child: 
                                Text(
                                  'What are you renting?',
                                  style: TextStyle(
                                    color: KalanaColors.text,
                                    fontSize: 30,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              // ),
                              Text(
                                'Write a detailed description of what you are renting.',
                                style: TextStyle(
                                  color: KalanaColors.label,
                                  fontSize: 14,
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: padding),
                                child: Align(
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    'Give a title to your rental ($titleCount/60)',
                                    style: TextStyle(
                                      color: KalanaColors.text,
                                      fontSize: 15,
                                    ),
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(bottom: padding),
                                child: SizedBox(
                                  width: size.width * 0.9,
                                  child: TextField(
                                    maxLengthEnforcement:
                                        MaxLengthEnforcement.enforced,
                                    controller: titleController,
                                    maxLength: titleLimit,
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.all(
                                        size.width * .025,
                                      ),
                                      counterText: '',
                                    ),
                                    style: TextStyle(
                                      color: KalanaColors.text,
                                      fontSize: 15,
                                    ),
                                    keyboardType: TextInputType.text,
                                    maxLines: 2,
                                    minLines: 1,
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: padding),
                                child: Align(
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    'What category is your rental in?',
                                    style: TextStyle(
                                      color: KalanaColors.text,
                                      fontSize: 15,
                                    ),
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(bottom: padding),
                                child: SizedBox(
                                  width: size.width * 0.9,
                                  child: GestureDetector(
                                    onTap: () => {
                                      updateCategory(rentalItem, account),
                                    },
                                    behavior: HitTestBehavior.opaque,
                                    child: AbsorbPointer(
                                      child: TextField(
                                        controller: categoryController,
                                        decoration: InputDecoration(
                                          contentPadding: EdgeInsets.all(
                                            size.width * .025,
                                          ),
                                          counterText: '',
                                        ),
                                        style: TextStyle(
                                          color: KalanaColors.text,
                                          fontSize: 15,
                                        ),
                                        keyboardType: TextInputType.text,
                                        maxLines: 1,
                                        minLines: 1,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: padding),
                                child: Align(
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    'What price will you charge per day?',
                                    style: TextStyle(
                                      color: KalanaColors.text,
                                      fontSize: 15,
                                    ),
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(bottom: padding),
                                child: SizedBox(
                                  width: size.width * 0.9,
                                  child: TextField(
                                    controller: priceController,
                                    focusNode: focusNode,
                                    maxLines: 1,
                                    keyboardType: TextInputType.number,
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.all(
                                        size.width * .025,
                                      ),
                                    ),
                                    style: TextStyle(
                                      color: KalanaColors.text,
                                      fontSize: 15,
                                    ),
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: padding),
                                child: Align(
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    'Describe your rental ($descriptionCount/500)',
                                    style: TextStyle(
                                      color: KalanaColors.text,
                                      fontSize: 15,
                                    ),
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(bottom: padding),
                                child: SizedBox(
                                  width: size.width * 0.9,
                                  child: TextField(
                                    maxLengthEnforcement:
                                        MaxLengthEnforcement.enforced,
                                    controller: descriptionController,
                                    maxLength: descriptionLimit,
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.all(
                                        size.width * .025,
                                      ),
                                      counterText: '',
                                    ),
                                    style: TextStyle(
                                      color: KalanaColors.text,
                                      fontSize: 15,
                                    ),
                                    keyboardType: TextInputType.text,
                                    maxLines: null,
                                    minLines: 1,
                                  ),
                                ),
                              ),
                              Expanded(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  children: [
                                    InkWell(
                                      onTap: isFormValid(rentalItem)
                                          ? () {
                                              rentalItem.setItemTitle(
                                                  titleController.text);
                                              rentalItem.setItemDescription(
                                                  descriptionController.text);
                                              Navigator.of(context).push(
                                                MaterialPageRoute(
                                                  builder: (context) =>
                                                      RentalPhotos(
                                                          bottomNavTapped: widget
                                                              .bottomNavTapped),
                                                ),
                                              );
                                            }
                                          : null,
                                      child: Container(
                                        decoration: BoxDecoration(
                                          color: isFormValid(rentalItem)
                                              ? KalanaColors.secondary
                                              : KalanaColors.disabled,
                                          borderRadius: BorderRadius.circular(
                                            50,
                                          ),
                                        ),
                                        child: SizedBox(
                                          width: size.width * .65,
                                          height: size.height * .085,
                                          child: Align(
                                            alignment: Alignment.center,
                                            child: Text(
                                              'Next',
                                              style: TextStyle(
                                                fontSize: 20,
                                                color: KalanaColors.light,
                                                fontWeight: FontWeight.bold,
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              // ),
            ),
          // ),
        // ],
      // ),
    );
  }
}