扑,我想从StreamController更改数量列表吗?

时间:2019-04-25 10:37:42

标签: dart flutter


颤抖,我想从StreamController更改数量列表吗?

我想轻按
IconButton更改数据
Text(poduct[index].qty.toString())
来自StreamController
我不想使用setState(() {});

enter image description here

import 'package:flutter/material.dart';
import 'dart:async';
void main() {
  runApp(new MaterialApp(title: "Simple Material App", home: new MyHome()));
}

class MyHome extends StatefulWidget {
  @override
  MyHomeState createState() => new MyHomeState();
}

class Product {
  String productName;
  int qty;
  Product({this.productName, this.qty});
}

class MyHomeState extends State<MyHome> {
  List<Product> poduct = [Product(productName: "Nike",qty: 20),Product(productName: "Vans",qty: 30),];
  var listPoduct  = StreamController<List<Product>>();


  @override
  void initState() {
    listPoduct.sink.add(poduct);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("test stream"),
        ),
        body: Container(
          padding: EdgeInsets.all(8.0),
          child: StreamBuilder(
            stream: listPoduct.stream,
            builder: (context, snapshot) {
              return ListView.builder(
                itemCount: poduct.length,
                padding: EdgeInsets.all(10),
                itemBuilder: (BuildContext context, int index){
                  return Padding(
                    padding: const EdgeInsets.only(top: 20.0), 
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(poduct[index].productName,style: TextStyle(fontSize: 24.0),),
                        new IconButton(icon: new Icon(Icons.remove), onPressed: (){
                          // How to Add ? listPoduct.sink ?
                        }),
                        Text(poduct[index].qty.toString()),   /// <<< I Want Change Qty List Form StreamController
                        new IconButton(icon: new Icon(Icons.add), onPressed: (){
                          // How to Add ? listPoduct.sink ?
                        }),
                        Divider(),
                      ],
                    ),
                  );
                },
              );
            }
          ),
        ));
  }
}

我希望采取行动 IconButton更改数据 文本(poduct [index] .qty.toString()), 从StreamController 我不想使用setState((){});

1 个答案:

答案 0 :(得分:0)

void main() {
  runApp(new MaterialApp(title: "Simple Material App", home: new MyHome()));
}

class MyHome extends StatefulWidget {
  @override
  MyHomeState createState() => new MyHomeState();
}

class Product {
  String productName;
  int qty;
  Product({this.productName, this.qty});
}

class MyHomeState extends State<MyHome> {
  List<Product> poduct = [       // <<<<<<<< TYPO HERE
    Product(productName: "Nike",qty: 20),
    Product(productName: "Vans",qty: 30)];
  var listPoduct  = StreamController<List<Product>>();

  @override
  void initState() {
    listPoduct.sink.add(poduct);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("test stream"),
        ),
        body: Container(
          padding: EdgeInsets.all(8.0),
          child: StreamBuilder(
              stream: listPoduct.stream,
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return ListView.builder(
                    itemCount: snapshot.data.length,    // <<<<<<<< . note that listbuilder relies on snapshot not on your poduct property 
                    padding: EdgeInsets.all(10),
                    itemBuilder: (BuildContext context, int index){
                      return Padding(
                        padding: const EdgeInsets.only(top: 20.0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(poduct[index].productName,style: TextStyle(fontSize: 24.0),), // <<<<<<<< you can also use here the snapshot.data
                            new IconButton(icon: new Icon(Icons.remove), onPressed: () {
                              _update(index, -1);
                            }),
                            Text(poduct[index].qty.toString()),  // <<<<<<<< you can also use here the snapshot.data
                            new IconButton(icon: new Icon(Icons.add), onPressed: (){
                              _update(index, 1);
                            }),
                            Divider(),
                          ],
                        ),
                      );
                    },
                  );
                } else {
                  return Container()
                }
              }
          ),
        ));
  }

  _update(int index, int difference) {
    for (int i = 0; i < poduct.length; i++ ) {
      if (i == index) {
        poduct[i] =
            Product(productName: poduct[i].productName,
            qty: poduct[i].qty + difference);
      }
    }
    listPoduct.add(poduct);
  }
}

一些有用的链接:

StreamBuilder-class

Example