如何在不重置状态的情况下更新StreamBuilder中的Flutter Cards?

时间:2017-10-08 16:41:21

标签: stream widget state flutter

我让StreamBuilder正常工作。 1件事,每次1个元素改变整个列表重置。因此,例如每张卡都有一个计数器,我希望在没有屏幕变化的情况下进行加减。我怎样才能做到这一点?这是我的代码(用完整文件堆栈更新)......

    final FirebaseAuth _auth = FirebaseAuth.instance;
final fb = FirebaseDatabase.instance.reference();
final falQuery = fb.child('NumberOnes').orderByChild('Value');
final streamQuery = fb.child('NumberOnes').orderByChild('Value').onValue;

class NumberOnesPage extends StatefulWidget {
  @override
  NumberOnesPageState createState() => new NumberOnesPageState();
}

class NumberOnesPageState extends State<NumberOnesPage> {
  List names = new List();
  List numbers = new List();
  List ids = new List();
  List vidImages = new List();

  void _handleJson(value) {
    Map myMap = value; //store each map
    var titles = myMap.values;
    for (var items in titles) {
      names.add(items['vidTitle']);
      numbers.add(items['Value']);
      ids.add(items['vidId']);
      vidImages.add(items['vidImage']);
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          backgroundColor: Colors.amber,
          title: new Text(
            'Number Ones',
            style:
                new TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
          ),
        ),
        body: new Container(
          child: new Center(
              child: new Column(
            children: <Widget>[
              new Flexible(
                  child: new FirebaseAnimatedList(
                      query: falQuery,
                      padding: new EdgeInsets.all(15.0),
                      //sort: (a, b) => b.key.compareTo(a.key),
                      reverse: false,
                      itemBuilder: (_, DataSnapshot followerSnap,
                          Animation<double> animation, int Index) {
                        return new StreamBuilder<Event>(
                            stream: streamQuery,
                            builder: (BuildContext context,
                                AsyncSnapshot<Event> event) {
                              switch (event.connectionState) {
                                case ConnectionState.none:
                                  return new Card(
                                    child: new Text('Loading...',
                                        style: new TextStyle(
                                            fontSize: 12.0,
                                            fontWeight: FontWeight.bold,
                                            fontStyle: FontStyle.italic)),
                                  );
                                case ConnectionState.waiting:
                                  return new Card(
                                    child: new Text('Awaiting Results...',
                                        style: new TextStyle(
                                            fontSize: 12.0,
                                            fontWeight: FontWeight.bold,
                                            fontStyle: FontStyle.italic)),
                                  );
                                default:
                                  if (event.hasError)
                                    return new Card(
                                      child: new Text('Error: ${event.error}',
                                          style: new TextStyle(
                                              fontSize: 12.0,
                                              fontWeight: FontWeight.bold,
                                              fontStyle: FontStyle.italic)),
                                    );
                                  else
                                    _handleJson(event.data.snapshot.value);
                                  return new InkWell(
                                    splashColor: Colors.blueAccent,
                                    onTap: () {
                                      Navigator.push(
                                          context,
                                          new MaterialPageRoute(
                                              builder: (_) =>
                                                  new Video.VideoPage()));
                                      Video.id = ids[Index];
                                      Video.title = names[Index];
                                      Video.videoImage = vidImages[Index];
                                    },
                                    child: new Card(
                                      child: new Column(
                                        children: <Widget>[
                                          new Padding(
                                              padding: new EdgeInsets.all(5.0)),
                                          new Image.network(vidImages[Index]),
                                          new Padding(
                                              padding: new EdgeInsets.all(3.0)),
                                          new Text(
                                              '${numbers[Index]} MyFavKPopers Have Made This Their #1'),
                                          new Padding(
                                              padding: new EdgeInsets.all(3.0)),
                                          new Text(
                                            names[Index],
                                            style: new TextStyle(
                                                fontSize: 18.0,
                                                fontWeight: FontWeight.bold,
                                                color: Colors.black),
                                            //textAlign: TextAlign.center,
                                          ),
                                          new Padding(
                                              padding: new EdgeInsets.all(5.0)),
                                        ],
                                      ),
                                    ),
                                  );
                              }
                            });
                      }))
            ],
          )),
        ));
  }
}

因此,例如,我希望numbers[Index]更改甚至卡片根据数字值上下移动而不完全更改UI。

1 个答案:

答案 0 :(得分:0)

有一个小部件可以执行此操作:IndexedStack,它保留堆栈中所有子级的状态,同时允许您在它们之间进行切换:

IndexedStack(
index: _widgetIndex,
children: [
 WidgetOne(),
 WidgetTwo()
]
)

您可以在代码中的任何位置使用setState(),通过修改_widgetIndex的值来更改应显示的小部件

setState(() => _widgetIndex = 2);