在这段代码中没有任何错误,但没有什么被理解为是什么导致了两个滑块。看到在这个android模拟器工作两个旋转木马。请告诉我什么是这个代码中的问题。不明白现在要写什么,多久了这个stock overflow被告知,添加一些更多的细节,头脑被宠坏了,所以我写这个,对不起,不注意这个,注意问题。
import 'dart:convert';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class Carousel extends StatefulWidget {
@override
_CarouselState createState() => _CarouselState();
}
class _CarouselState extends State<Carousel> {
var api = Uri.parse('http://192.168.43.162/flutter/bannerApi.php');
var response;
var bannerApi;
@override
void initState() {
super.initState();
// for loading
fetchData();
}
fetchData() async {
response = await http.get(api);
print(response.body);
bannerApi = jsonDecode(response.body);
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: response != null
? Stack(
children: List.generate(
bannerApi.length,
(index) => CarouselSlider(
items: [
//1st Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage(bannerApi[index]['img']),
// image: AssetImage(assignmets[index]['img']),
fit: BoxFit.cover,
),
),
),
],
//Slider Container properties
options: CarouselOptions(
height: 180.0,
enlargeCenterPage: true,
autoPlay: true,
aspectRatio: 16 / 9,
autoPlayCurve: Curves.fastOutSlowIn,
enableInfiniteScroll: true,
autoPlayAnimationDuration: Duration(milliseconds: 800),
viewportFraction: 0.8,
),
),
))
: CircularProgressIndicator(
backgroundColor: Colors.white,
)),
);
}
}
这是一个api数据。
[{"img":"https:\/\/images.unsplash.com\/photo-1517245386807-bb43f82c33c4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80"},{"img":"https:\/\/images.unsplash.com\/photo-1517245386807-bb43f82c33c4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80"},{"img":"https:\/\/images.unsplash.com\/photo-1517245386807-bb43f82c33c4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80"},{"img":"https:\/\/images.unsplash.com\/photo-1517245386807-bb43f82c33c4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80"},{"img":"https:\/\/images.unsplash.com\/photo-1517245386807-bb43f82c33c4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80"},{"img":"https:\/\/images.unsplash.com\/photo-1517245386807-bb43f82c33c4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80"}]
我使用的是容器而不是您的堆栈。
Container(
child: CarouselSlider(
items: List.generate(
bannerApi.length,
(index) => Container(
margin: edgeInsects.all(6.0)
...