我试图制作一个应用程序来显示图像,为此,我正在使用Firestore,Flutter ...到滑块,正在使用carousel_slider插件来显示这些图像,但是当我从在Firestore中,滑块不会显示新图像,但是圆圈(指示器)会显示新圆圈,但是也无法使用,指示器仅适用于初始图像。
我的代码轮播:
_loadCarrouselFood(){
var carouselSlider = CarouselSlider(
viewportFraction: 1.0,
aspectRatio: 2.0,
autoPlay: false,
enlargeCenterPage: true,
items: child,
onPageChanged: (index) {
setState(() {
_current = index;
});
LatLng locationDish = LatLng(_locationDish[_current].latitude, _locationDish[_current].longitude);
moveCameraTo(locationDish);
},
);
setState(() {
_sliderDish = carouselSlider;
});
return carouselSlider;
}
}
Firestore功能:
_getInfoDish() async{
List<LocationDish> _locationDishTmp = <LocationDish>[];
List<Dish> _dishCardTmp = <Dish>[];
bool hasFoodInArray = false;
Firestore.instance // Get the firebase instance
.collection(Constants.NAME_COLECTION) // Get the informations collection
.snapshots() // Get the Stream of DocumentSnapshot
.listen((QuerySnapshot snapshot){
_locationDishTmp.clear();
_dishCardTmp.clear();
print(":::::: - FIRESTORE - ::::::");
snapshot.documents.forEach((obj){
//Set dish to Cards
_dishCardTmp.add(
Dish(
id: obj.data["id"],
name: obj.data["name"],
photoURL: 'https://recetaparahoy.com/wp-content/uploads/2017/06/ceviche-peruano-630x420.jpg',
score: obj.data["score"].toDouble(),
),
);
});
if(_locationDishTmp.length > 0)
{
setState(() {
//add only food recent data
for(int i = 0; i < _locationDishTmp.length; i++){
_locationDish.add(_locationDishTmp[i]);
_dishCard.add(_dishCardTmp[i]);
}
//_current = _locationDish.length-1;
_hasFoodOnMap = true;
});
}
});
}
请,有人可以告诉我错误,或者可能有其他解决方法,希望我的解释很清楚,谢谢!!
答案 0 :(得分:3)
您可以在下面复制粘贴运行完整代码
我使用以下演示来模拟这种情况
您可以在可见的情况下使用CarouselSlider.builder
构建项目
https://github.com/serenader2014/flutter_carousel_slider#build-item-widgets-on-demand
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 1;
String url = 'https://picsum.photos/250?image=';
List<String> urlList = ['https://picsum.photos/250?image=1'];
int _current = 0;
void _incrementCounter() {
setState(() {
_counter++;
urlList.add('https://picsum.photos/250?image=${_counter}');
});
}
List<T> map<T>(List list, Function handler) {
List<T> result = [];
for (var i = 0; i < list.length; i++) {
result.add(handler(i, list[i]));
}
return result;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CarouselSlider.builder(
autoPlay: false,
enlargeCenterPage: true,
aspectRatio: 2.0,
onPageChanged: (index) {
setState(() {
_current = index;
});
},
itemCount: urlList.length,
itemBuilder: (BuildContext context, int index) =>
Container(
child: Image.network(urlList[index]),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: map<Widget>(
urlList,
(index, url) {
return Container(
width: 8.0,
height: 8.0,
margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 2.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _current == index
? Color.fromRGBO(0, 0, 0, 0.9)
: Color.fromRGBO(0, 0, 0, 0.4)),
);
},
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}