DiscountCalculatorProvider manages the state and business logic for discount calculations. It extends ChangeNotifier and persists user inputs using SharedPreferences.
Overview
This provider handles two types of discounts:- Percentage discounts: Apply sequential percentage-based discounts
- Fixed amount discounts: Apply dollar-value discounts
Enums
DiscountType
Defines the type of discount being applied.Apply discounts as percentages (e.g., 10% off)
Apply discounts as fixed dollar amounts (e.g., $50 off)
Constructor
The SharedPreferences instance used for persisting calculator state
Getters
Calculation Results
The price after discounts but before tax
The final price after all discounts and tax
The total amount saved from all discounts
The calculated tax amount based on the subtotal
Error message if validation fails, null otherwise
Configuration
The current discount type (percentage or fixedAmount)
Input State
The persisted original price input string
The persisted primary discount input string
The persisted additional discount input string
The persisted tax percentage input string
The parsed original price value for display
Methods
calculateDiscount
Performs the discount calculation with the provided inputs.The original price before any discounts
The primary discount value (percentage or fixed amount depending on discountType)
Optional additional discount applied sequentially after the primary discount
Optional tax percentage to apply to the subtotal
Calculation Logic
For Percentage Discounts:- Apply primary discount:
saved1 = originalPrice * (primaryDiscount / 100) - Apply additional discount to new price:
saved2 = priceAfterFirst * (additionalDiscount / 100) - Calculate subtotal:
subtotal = originalPrice - saved1 - saved2 - Apply tax:
taxAmount = subtotal * (taxPercent / 100) - Calculate final price:
finalPrice = subtotal + taxAmount
- Sum all discounts:
totalDiscount = primaryDiscount + additionalDiscount - Calculate subtotal:
subtotal = originalPrice - totalDiscount - Apply tax:
taxAmount = subtotal * (taxPercent / 100) - Calculate final price:
finalPrice = subtotal + taxAmount
Validation
The method validates:- All numeric inputs are valid numbers
- No negative values
- Percentage discounts don’t exceed 100%
- Fixed discounts don’t exceed original price
errorMessage is set and results are cleared.
setDiscountType
Changes the discount type and persists the selection.The new discount type to apply
clear
Resets all inputs and calculation results.- Clears all input strings
- Removes persisted values from SharedPreferences
- Clears all calculation results
- Clears any error messages
- Notifies listeners of the state change
Usage Example
UI Integration
This provider is typically used withConsumer widgets to reactively update the UI:
Persistence
All user inputs are automatically persisted using the following SharedPreferences keys:disc_orig- Original pricedisc_pri- Primary discountdisc_add- Additional discountdisc_tax- Tax percentagedisc_type- Discount type (0 = percentage, 1 = fixed amount)