import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _currentIndex = 0;
final List<String> images = [
'https://via.placeholder.com/600x200',
'https://via.placeholder.com/600x200/FF0000',
'https://via.placeholder.com/600x200/00FF00',
];
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Carousel with Indicators')),
body: Column(
children: [
CarouselSlider(
options: CarouselOptions(
height: 200,
autoPlay: true,
onPageChanged: (index, reason) => setState(() => _currentIndex = index),
),
items: images.map((url) {
return Image.network(url, fit: BoxFit.cover);
}).toList(),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: images.asMap().entries.map((entry) {
return GestureDetector(
onTap: () => setState(() => _currentIndex = entry.key),
child: Container(
width: 8.0,
height: 8.0,
margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 4.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _currentIndex == entry.key ? Colors.blue : Colors.grey,
),
),
);
}).toList(),
),
],
),
),
);
}
}
flutter CarouselSlider
最新推荐文章于 2025-03-01 14:48:31 发布