Created
May 11, 2022 23:29
-
-
Save BarryDaBee/ce63bd7f977ab87b232ca69d057573e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
| final List<Product> products = [ | |
| Product(name: 'Potato', quantity: '100', value: 1000), | |
| Product(name: 'Pie', quantity: '100', value: 1500), | |
| ]; | |
| ///ViewModel/Provider | |
| double total = 0; | |
| ////// | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| theme: ThemeData.dark().copyWith( | |
| scaffoldBackgroundColor: darkBlue, | |
| ), | |
| debugShowCheckedModeBanner: false, | |
| home: Scaffold( | |
| body: ListView.builder( | |
| itemBuilder: (context, index){ | |
| return ProductTile(product: products[index], onChanged: (amount){ | |
| products[index].value = amount; | |
| calculateTotal(products); | |
| }); | |
| }, | |
| itemCount: products.length, | |
| ), | |
| ), | |
| ); | |
| } | |
| ///View Model | |
| void calculateTotal(List<Product> products){ | |
| total = 0; | |
| for(var product in products){ | |
| total += product.value; | |
| } | |
| notifyListeners(); | |
| } | |
| ///Fake data | |
| notifyListeners(){} | |
| } | |
| class Product{ | |
| String name; | |
| String quantity; | |
| double value; | |
| Product({required this.name, required this.quantity, required this.value}); | |
| factory Product.fromMap(Map<String, dynamic> map)=> | |
| Product( | |
| name: map['name'], | |
| quantity: map['quantity'], | |
| value: map['value']); | |
| } | |
| class ProductTile extends StatefulWidget { | |
| final ValueChanged<double>? onChanged; | |
| final Product product; | |
| const ProductTile({Key? key, this.onChanged, required this.product}) : super(key: key); | |
| @override | |
| State<ProductTile> createState() => _ProductTileState(); | |
| } | |
| class _ProductTileState extends State<ProductTile> { | |
| @override | |
| Widget build(BuildContext context) { | |
| return TextField( | |
| onChanged: (value){ | |
| widget.onChanged?.call(double.tryParse(value)??0.0); | |
| } | |
| ); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment