1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import 'package:dayly/main.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart';
class Catalog extends StatelessWidget { const Catalog({Key? key}) : super(key: key);
@override Widget build(BuildContext context) { final List<String> itemNames = context.read<GoodsModel>().itemNames as List<String>; List<Good> goods=List.generate(itemNames.length, (index) => Good(color: Colors.primaries[index],id: index,name: itemNames[index],));
return Scaffold( appBar: AppBar( backgroundColor: Colors.black, title: const Text("Catalog"), centerTitle: true, actions: [IconButton( onPressed: (){ Navigator.pushNamed(context, 'cart'); }, icon: const Icon(Icons.shopping_cart) )], ), body: GridView( gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 1, mainAxisSpacing: 0, mainAxisExtent: 80 ), children: goods, ), ); } }
class Good extends StatelessWidget { final int id; final Color color; final String name;
const Good({Key? key, required this.color, required this.id, required this.name}) : super(key: key);
@override Widget build(BuildContext context) { bool selected=context.watch<GoodsModel>().isSelected[id]; return Padding( padding: const EdgeInsets.symmetric(horizontal: 16,vertical: 8), child: Row( children: [ Container( width: 50, height: 50, color: color, ), const SizedBox(width: 24), Expanded( child: Text(name,style: const TextStyle( fontSize: 24 ),), ), TextButton( onPressed: (){ if(selected==true){ selected=!selected; context.read<GoodsModel>().unselectItem(id); } else{ selected=!selected; context.read<GoodsModel>().selectItem(id); } }, child: selected ?const Icon(Icons.check,size: 20,) :const Text( "ADD", style: TextStyle( color: Colors.black, fontSize: 20 ), ), ), const SizedBox(width: 24), ], ), ); } }
|