首页 > 解决方案 > 在 Flutter 上加载动态照片

问题描述

我正在开发一个新闻应用程序,在某些情况下有一个照片库,我需要在故事的结尾上传这些照片,但我很难在函数之间传递数据并在轮播中检索它们。

_getFotos (_id) 和 loadFotos 负责带入数据,在类 ImageCarousel 和 Widget 构建(BuildContext 上下文)中,我挂载了轮播的整个范围,它正在工作,只需要保持动态并检查是否有照片在新闻,如果有显示,或者安装轮播。如何兑换数据并使照片动态化?

打印轮播https://i.imgur.com/glISVp4.png



import 'dart:async';
import 'package:news/localization/MyLocalizations.dart';
import 'package:news/util/date_util.dart';
import 'package:news/util/functions.dart';
import 'package:share/share.dart';
import "package:http/http.dart" as http;
import "dart:convert";
import 'package:carousel_pro/carousel_pro.dart';

import 'package:flutter_html/flutter_html.dart';
const requestNotice = "${url}api/v1/noticias/fotos/";

class DetailPage extends StatelessWidget{
  final Completer<WebViewController> _controller =  Completer<WebViewController>();
  var _img;
  var _video_id;
  var _title;
  var _date;
  var _description;
  var _link;
  var _category;
  var _origin;
  var img;
  var _id;
  var _status;
  DetailPage(this._img,this._video_id,this._title,this._date,this._description,this._category,this._link,this._origin,this._id, this._status);
  MyLocalizations strl;

  @override
  Widget build(BuildContext context) {
    strl = MyLocalizations.of(context);
    // Chama a função das fotos e passa o id da notícia
   _getFotos(_id);

      return new Scaffold(
        appBar: new AppBar(
          title: new Text(_category),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.share),
              onPressed: () {
                shareNotice();
              },
              color: Colors.white,
            )
          ],
        ),
      );
  }

    // Várias funções aqui

 _getFotos(id) async {
   loadFotos(id);
   var fotos = await this.loadFotos(id);
   print(fotos);
   return fotos;
 }

  Future shareNotice() async {
    await Share.share("$_title:\n$_link");
  }







  Future<String>loadFotos(id) async {
    http.Response response = await http.get(requestNotice + (id));
    var fotos = json.decode(response.body);
    return fotos;
  }







}







class ImageCarousel extends StatefulWidget {
  _ImageCarouselState createState() => new _ImageCarouselState();
}

class _ImageCarouselState extends State<ImageCarousel> with SingleTickerProviderStateMixin {
  Animation<double> animation;
  AnimationController controller;

  initState() {
    super.initState();
    controller = new AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    animation = new Tween(begin: 0.0, end: 18.0).animate(controller)
      ..addListener(() {
        setState(() {
          // the state that has changed here is the animation object’s value
        });
      });
    controller.forward();
  }







  @override
  Widget build(BuildContext context) {
    return new Center(
        child: new Container(
          padding: EdgeInsets.all(20.0),
          height: 200.0,
          child: Carousel(
                boxFit: BoxFit.cover,
                images: [
                  AssetImage('assets/1.jpeg'),
                  AssetImage('assets/2.jpeg'),
                  AssetImage('assets/3.jpeg'),
                  AssetImage('assets/4.jpeg'),
                ],
                animationCurve: Curves.fastOutSlowIn,
                animationDuration: Duration(milliseconds:2000),
          )
        )
    );
  }

  dispose() {
    controller.dispose();
    super.dispose();
  }
}



标签: flutter

解决方案


推荐阅读