Inside the product_screen.dart file under src/product/view/products/ add the following code:
Inside the product_screen.dart file under src/product/view/products/ add the following code:
In this section we are going to be setting up our UI.
class _ProductsErrorWidget extends StatelessWidget {
final String error;
const _ProductsErrorWidget({Key key, @required this.error})
: assert(error != null),
super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
error,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headline6,
),
const SizedBox(height: 16),
Align(
child: RaisedButton(
onPressed: context.read<ProductsCubit>().refresh,
child: const Text('Retry'),
),
),
],
);
}
}
class _ProductsGridView extends StatelessWidget {
final List<Product> products;
const _ProductsGridView({Key key, @required this.products})
: assert(products != null),
super(key: key);
@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
childAspectRatio: 1 / 1.2,
),
padding: const EdgeInsets.all(8),
itemCount: products.length,
physics: const BouncingScrollPhysics(),
itemBuilder: (_, index) => _ProductItem(product: products[index]),
);
}
}
Inside the product_screen.dart file under src/product/view/products/ add the following code:
class _ProductItem extends StatelessWidget {
final Product product;
const _ProductItem({Key key, @required this.product})
: assert(product != null),
super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => context.navigator.push(
Routes.productScreen,
arguments: ProductScreenArguments(product: product),
),
child: Card(
margin: EdgeInsets.zero,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Container(
color: Colors.white,
padding: const EdgeInsets.all(8),
child: Image.network(product.image),
),
),
Container(
height: 48,
color: Colors.grey[200],
padding: const EdgeInsets.all(4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
product.title,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontWeight: FontWeight.w600),
),
const SizedBox(height: 4),
Text('${product.price} ETB'),
],
),
),
),
IconButton(
onPressed: () =>
context.read<CartCubit>().addProduct(product),
icon: const Icon(Icons.add_shopping_cart_rounded),
),
],
),
),
],
),
),
);
}
}