mirrored_repositories/shop/lib
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';


import '../provider/products_provider.dart';
import '../widgets/user_product_item.dart';
import '../widgets/app_drawer.dart';
import '../screens/edit_product_screen.dart';


class UserProductsScreen extends StatelessWidget {

  static const routeName='/user-products';


  Future<void> _refreshProducts(BuildContext context) async{
    await Provider.of<Products>(context,listen: false).fetchAndSetProducts(true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Your Products'),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.add), 
            onPressed: (){
              Navigator.of(context).pushNamed(EditProductScreen.routeName);

            }
          ),
        ],
      ),
      drawer: AppDrawer(),
      body: FutureBuilder(
        future: _refreshProducts(context),
        builder: (ctx,snapshot)=>
        snapshot.connectionState==ConnectionState.waiting?
        Center(
          child:CircularProgressIndicator()
        ):
        RefreshIndicator(
          onRefresh: ()=>_refreshProducts(context) ,
          child: Consumer<Products>(
            builder: (ctx,productData,child)=>Padding(
              padding: EdgeInsets.all(8),
              child: ListView.builder(
                itemCount: productData.items.length,
                itemBuilder: (ctx,i)=>Column(
                  children: <Widget>[
                    UserProductItem(
                      title: productData.items[i].title,
                      imageUrl: productData.items[i].imageUrl,
                      id: productData.items[i].id,
                    ),
                    Divider(),
                    //here we need to provide Consumer else it goes to infinite loop
                   
                  ],
                ),
              ), 
            ),
          ),
        ),
      ),
      
      
    );
  }
}