p2_carousel_slider 0.0.2
p2_carousel_slider: ^0.0.2 copied to clipboard
PageView to Set InitialPageOffset
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:p2_carousel_slider/p2_carousel_slider.dart';
final List<String> imgList = [
'https://images.unsplash.com/photo-1520342868574-5fa3804e551c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=6ff92caffcdd63681a35134a6770ed3b&auto=format&fit=crop&w=1951&q=80',
'https://images.unsplash.com/photo-1522205408450-add114ad53fe?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=368f45b0888aeb0b7b08e3a1084d3ede&auto=format&fit=crop&w=1950&q=80',
'https://images.unsplash.com/photo-1519125323398-675f0ddb6308?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=94a1e718d89ca60a6337a6008341ca50&auto=format&fit=crop&w=1950&q=80',
'https://images.unsplash.com/photo-1523205771623-e0faa4d2813d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=89719a0d55dd05e2deae4120227e6efc&auto=format&fit=crop&w=1953&q=80',
'https://images.unsplash.com/photo-1508704019882-f9cf40e475b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8c6e5e3aba713b17aa1fe71ab4f0ae5b&auto=format&fit=crop&w=1352&q=80',
'https://images.unsplash.com/photo-1519985176271-adb1088fa94c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a0c8d632e977f94e5d312d9893258f59&auto=format&fit=crop&w=1355&q=80'
];
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int ctgrListIndex = 0;
int ptcoIndex = 0;
P2CarouselController ctgrCarouselController = P2CarouselController();
P2CarouselController ptcoCarouselController = P2CarouselController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
P2CarouselSlider.builder(
carouselController: ctgrCarouselController,
disableGesture: true,
options: CarouselOptions(
height: 110,
viewportFraction: 0.2,
initialPage: 0,
initialPageOffset: 50,
padEnds: false,
enableInfiniteScroll: false,
pageSnapping: false,
disableCenter: false,
enlargeCenterPage: false,
),
itemCount: imgList.length ?? 0,
itemBuilder: (BuildContext context, int index, int realIndex) {
return GestureDetector(
onTap: () => {
setState(() {
ctgrListIndex = index;
ptcoCarouselController.jumpToPage(0);
})
},
child: PtcoHotIssueCtgrCard(
ctgr: imgList[index],
),
);
},
),
P2CarouselSlider.builder(
carouselController: ptcoCarouselController,
disableGesture: true,
options: CarouselOptions(
height: 100,
aspectRatio: 2.0,
viewportFraction: 0.9,
initialPage: 0,
padEnds: true,
enableInfiniteScroll: true,
disableCenter: false,
onPageChanged: (idx, reason) {
setState(() {
ptcoIndex = idx;
});
},
),
itemCount: imgList.length ?? 0,
itemBuilder: (context, index, realIndex) => PtcoHotIssueCard(
ptcoCtnts : imgList[index],
),
)
],
),
),
);
}
}
class PtcoHotIssueCtgrCard extends StatelessWidget {
const PtcoHotIssueCtgrCard({super.key, required this.ctgr});
final String ctgr;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.fromLTRB(0, 10, 0, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
width: 60,
height: 60,
child : Image.network(ctgr),
),
],
)
);
}
}
class PtcoHotIssueCard extends StatelessWidget {
const PtcoHotIssueCard({super.key, required this.ptcoCtnts});
final String ptcoCtnts;
@override
Widget build(BuildContext context) {
return Card(
elevation: 5,
margin: const EdgeInsets.fromLTRB(10, 10, 10, 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
clipBehavior: Clip.antiAlias,
shadowColor: Colors.grey.withOpacity(0.3),
child: Stack(
fit: StackFit.expand,
children: [
Image.network(ptcoCtnts)
],
),
);
}
}