Flutter FutureBuilder如何在布局中的多个位置更改文本?

时间:2018-10-12 08:13:23

标签: flutter

我仔细阅读了Flutter教程;从互联网获取数据:https://flutter.io/cookbook/networking/fetch-data/

我担心的是,我想更新布局中的多个文本。

该实现仅显示一种更新方式:

FutureBuilder<Post>(
  future: fetchPost(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      return Text(snapshot.data.title);
    } else if (snapshot.hasError) {
      return Text("${snapshot.error}");
    }
    // By default, show a loading spinner
    return CircularProgressIndicator();
  },
);

这可以正常工作,并一次显示一个视图。

在Android Studio / Java中,我会做类似的事情:

myTextView1.setText(snapshot.data.data1)
myTextView2.setText(snapshot.data.data2)
myTextView3.setText(snapshot.data.data3)
.....
myTextView10.setText(snapshot.data.data3)

但是在Flutter中,我目前一次只能使用一个“小部件”。

当然,我可以在return参数中提供整个布局,但这太疯狂了!

有任何想法/建议吗?

2 个答案:

答案 0 :(得分:0)

另一种策略是在状态类中具有局部变量,并在将来到来时对其进行更新。因此,您可以在任何需要的地方引用该变量。

这里是一个例子:

import 'dart:async';

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Post _post = Post("Title 0", "Subtitle0 ", "description 0");

  @override
  void initState() {
    super.initState();
    _getPost();
  }

  void _getPost() async {
    _post = await fetchPost();
    setState(() {});
  }

  Future<Post> fetchPost() {
    return Future.delayed(Duration(seconds: 4), () {
      return Post("Title new", "Subtitle new", "description new");
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            new Text(_post.title),
            new Text(_post.subtitle),
            new Text(_post.description),
          ],
        ),
      ),
    );
  }
}

class Post {
  final String title;
  final String subtitle;
  final String description;

  Post(this.title, this.subtitle, this.description);
}

答案 1 :(得分:0)

您可以将请求转换为Stream

import 'package:flutter/material.dart';
import 'package:random_pk/random_pk.dart';
import 'dart:async';

class TestWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(child: RandomContainer(
        width: 200.0,
        height: 200.0,
        child: Center(child: _MyTextWidget(fetchPost().asStream())),
      ),),
    );
  }

  Future<String> fetchPost() {
    return Future.delayed(Duration(seconds: 4), () {
      return "Title data";
    });
  }

}
class _MyTextWidget extends StatefulWidget {
  _MyTextWidget(this.stream);
  final Stream<String> stream;

  @override
  State<StatefulWidget> createState() => _MyTextWidgetState();
}

class _MyTextWidgetState extends State<_MyTextWidget> {

  String text;

  @override
  void initState() {
    widget.stream.listen((String data) {
      setState(() {
        text = data;
      });
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Text(text == null ? 'loading' : text);
  }

}

在此示例中,RandomContainer会在每个setState上更改其颜色并用作指示符,而更改仅发生在_MyTextWidget