Flutter E Commerce app order management – 18

Orders play an important role in every E commerce app. Flutter e commerce app is no exception. Therefore, we discuss order management here.

To begin with, we must provide and tackle order separately.

Why? 

Because, if we insert order into our products class, it will be cumbersome to handle the process that relates to order only.

Therefore, let’s add the order model class first.

import 'package:flutter/foundation.dart';
import 'cart_model.dart';

class OrderItem {
  final String id;
  final double amount;
  final List<CartModel> products;
  final DateTime dateTime;

  OrderItem({
    required this.id,
    required this.amount,
    required this.products,
    required this.dateTime,
  });
}

class Orders with ChangeNotifier {
  final List<OrderItem> _orders = [];

  List<OrderItem> get orders {
    return [..._orders];
  }

  void addOrder(List<CartModel> cartProducts, double total) {
    _orders.insert(
      0,
      OrderItem(
        id: DateTime.now().toString(),
        amount: total,
        dateTime: DateTime.now(),
        products: cartProducts,
      ),
    );
    notifyListeners();
  }
}

Here we relate the order object to the cart object which we have discussed before.

Why?

What is the common relation between them?

It is because the cart page has its answer. 

How? Because in the cart page what do we get? All the added products. Right? 

Therefore, when we press the “order now” button, the cart clears up and we place the order at the same time.

Cart page where order can be placed
Cart page where order can be placed

Not only that.

It doesn’t end here. On the contrary, the real journey starts.

How? You may ask.

Well, the simple answer is in any e-commerce app, order has the closest relationship with the cart objects.

However, we order products, not carts. Right? 

Therefore, order also has a distant relationship with the products also.

But in the cart page, we place the order as follows. 

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/cart_model.dart';

import '../controllers/cart_item.dart';
import '../models/orders.dart';

class CartScreen extends StatelessWidget {
  static const routeName = '/cart';

  const CartScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final cart = Provider.of<CartModel>(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Your Cart'),
      ),
      body: Column(
        children: <Widget>[
          Card(
            margin: const EdgeInsets.all(15),
            child: Padding(
              padding: const EdgeInsets.all(8),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  const Text(
                    'Total',
                    style: TextStyle(fontSize: 20),
                  ),
                  const Spacer(),
                  Chip(
                    label: Text(
                      '\$${cart.itemCount}',
                      style: TextStyle(
                        color: Theme.of(context).colorScheme.error,
                      ),
                    ),
                    backgroundColor: Theme.of(context).primaryColor,
                  ),
                  TextButton(
                    child: Text(
                      'ORDER NOW',
                      style: TextStyle(
                        color: Theme.of(context).colorScheme.outline,
                      ),
                    ),
                    onPressed: () {
                      Provider.of<Orders>(context, listen: false).addOrder(
                        cart.items.values.toList()
                        cart.totalAmount,
                      );
                      cart.clear();
                    },
                  )
                ],
              ),
            ),
          ),
          const SizedBox(height: 10),
          Expanded(
            child: ListView.builder(
              itemCount: cart.items.length,
              itemBuilder: (ctx, i) => CartItem(
                cart.items.values.toList()[i].id,
                cart.items.keys.toList()[i],
                cart.items.values.toList()[i].price,
                cart.items.values.toList()[i].quantity,
                cart.items.values.toList()[i].title,
              ),
            ),
          )
        ],
      ),
    );
  }
}

In the next section, we will dive deep and see how it works.

So stay tuned.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Twitter

Comments

2 responses to “Flutter E Commerce app order management – 18”

  1. […] Firstly, we have seen how we can manage our orders. And to do that we have a separate order model class. […]

  2. […] when we clear the cart, we need to provide the order items. So that we can finally buy […]

Leave a Reply