Skip to content

Instantly share code, notes, and snippets.

@Roaa94
Last active April 27, 2022 23:12
Show Gist options
  • Save Roaa94/b47d79d91deffb6cdcd2a9787e516d87 to your computer and use it in GitHub Desktop.
Save Roaa94/b47d79d91deffb6cdcd2a9787e516d87 to your computer and use it in GitHub Desktop.
Flutter Carousel
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Carousel',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: StarterPage(),
);
}
}
class StarterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Carousel')),
body: const Center(
child: Carousel(),
),
);
}
}
class Carousel extends StatefulWidget {
const Carousel({Key? key}) : super(key: key);
@override
_CarouselState createState() => _CarouselState();
}
class _CarouselState extends State<Carousel> {
late final PageController _pageController;
int _activePageIndex = 0;
@override
void initState() {
_pageController = PageController(
initialPage: 0,
viewportFraction: 0.8,
);
super.initState();
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: 200,
child: PageView.builder(
clipBehavior: Clip.none,
controller: _pageController,
itemCount: 3,
onPageChanged: (int index) {
setState(() {
_activePageIndex = index;
});
},
itemBuilder: (c, i) {
return AnimatedScale(
scale: _activePageIndex == i ? 1 : 0.85,
duration: const Duration(milliseconds: 300),
child: Container(
height: 200,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
blurRadius: 10,
color: Colors.black.withOpacity(0.1),
),
],
),
child: const Center(child: Text('foo')), // Add your Panorama widget here
),
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment