如何像下面的示例一样创建图像轮播?
Image Carousel Overlay我尝试使用以下代码,但仅将图像设置为彼此相邻。
Widget build(BuildContext context) {
return Container(
width: 150,
child: CarouselSlider(items: ['https://picsum.photos/200/300','https://picsum.photos/200/300','https://picsum.photos/200/300'].map((i) {
return Builder(
builder: (BuildContext context) {
return Container(
width: 150,
margin: EdgeInsets.symmetric(horizontal: 2.0),
decoration: BoxDecoration(color: Colors.amber),
child: GestureDetector(child: Image.network(i,fit:BoxFit.fill,),
onTap: (){
//add TODO:
},
)
);
},
);
}).toList(),
height: 200.0,
),
);
我已经尝试过对齐,但是效果不佳。
您可以使用软件包https://pub.dev/packages/flutter_multi_carousel
代码段
Center(
child: Carousel(
height: 350.0,
width: 350,
type: "slideswiper",
indicatorType: "bar",
arrowColor: Colors.white,
axis: Axis.horizontal,
showArrow: true,
children: List.generate(
7,
(i) => Center(
child: Container(color: Colors.red),
))),
),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_multi_carousel/carousel.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Carousel Demo',
home: CarouselExample(),
);
}
}
class CarouselExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Carousel(
height: 350.0,
width: 350,
type: "slideswiper",
indicatorType: "bar",
arrowColor: Colors.white,
axis: Axis.horizontal,
showArrow: true,
children: List.generate(
7,
(i) => Center(
child: Container(color: Colors.red),
))),
),
);
}
}