在一类中调用streambuilder的小部件到另一类的多个位置

时间:2020-08-30 16:41:17

标签: flutter bloc

import 'package:amazon_bloc/bloc/cart_items_bloc.dart';
import 'package:amazon_bloc/pages/topBar.dart';

import 'package:flutter/material.dart';

class ShopItems extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
        
        body: ProductList());
  }
}

class ShopItemsWidget extends StatelessWidget {
  Widget build(BuildContext context) {
    return StreamBuilder(
      initialData: bloc.allItems, 
      stream: bloc.getStream, 
      builder: (context, snapshot) {
        
        return shopItemsListBuilder(snapshot);
      },
    );
  }
}

Widget shopItemsListBuilder(snapshot) {
  return ListView.builder(
    itemCount: snapshot.data["shop items"].length,
    scrollDirection: Axis.horizontal,
    itemBuilder: (BuildContext context, i) {
      final shopList = snapshot.data["shop items"];
      return Container(
        height: 100,
        width: 200,
        child: Column(
          children: <Widget>[
            Card(
              elevation: 5,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30.0)),
              child: Container(
                width: 200,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      height: 15,
                    ),
                    Stack(
                      children: <Widget>[
                        Icon(Icons.bookmark),
                        Image.asset(
                          shopList[i]['productImage'],
                          height: 130.0,
                          width: 180.0,
                        ),
                      ],
                    ),
                    Text(
                      shopList[i]['productTitle'],
                      style: TextStyle(fontSize: 12),
                    ),
                    
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Container(
                          height: 30,
                          width: 80,
                          child: Card(
                              elevation: 5,
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(30.0)),
                              child: Text(
                                shopList[i]['productTitle'],
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    fontSize: 18.0, color: Colors.grey),
                              )),
                        ),
                        IconButton(
                          icon: Icon(Icons.add_shopping_cart),
                          onPressed: () {
                            bloc.addToCart(shopList[i]);
                          },
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      );
    },
  );
}

class ProductList extends StatefulWidget {
  @override
  _ProductListState createState() => _ProductListState();
}

class _ProductListState extends State<ProductList> {
  Widget build(BuildContext context) {
    return SafeArea(
        child: Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          
          ShopItemsWidget(),
          Container(
            padding: EdgeInsets.only(left: 40.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  "Most Popular",
                  style: TextStyle(
                    fontSize: 28,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  "see more >",
                  style: TextStyle(
                    fontSize: 18,
                  ),
                ),
              ],
            ),
          ),
  
    ));
  }
}

我想在ProductList的多个位置使用streambuilder。产品列表是静态ui部分...我想放置流生成器,以便可以在ui的中间使用动态listview.builder
注意:这只是随机文本这只是随机文本这只是随机文本这只是随机文本这只是随机文本

0 个答案:

没有答案