此应用程序的同一行中有两个旋转木马幻灯片 运行,但我只想要一个。请告诉我什么是问题
There are two carousel slides running in same line this app but I want only one. please tell me what is Problem
这段代码没有错误
但是对于导致两个滑块的原因一无所知。
在这个 android 模拟器中看到两个轮播。
请告诉我这段代码有什么问题。
不明白现在写什么,这个stockoverflow被告知多久了,添加更多细节,
脑洞大开,所以才写到这里,不好意思,没注意这个,注意提问。
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)
...
这段代码没有错误 但是对于导致两个滑块的原因一无所知。 在这个 android 模拟器中看到两个轮播。 请告诉我这段代码有什么问题。 不明白现在写什么,这个stockoverflow被告知多久了,添加更多细节, 脑洞大开,所以才写到这里,不好意思,没注意这个,注意提问。
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)
...