Created
May 21, 2021 15:40
-
-
Save fabiancrx/84151a10660a471a69a9cca93adf368f to your computer and use it in GitHub Desktop.
Flutter PageView with different page size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: PageviewGallery(), | |
); | |
} | |
} | |
class PageviewGallery extends StatefulWidget { | |
@override | |
_PageviewGalleryState createState() => _PageviewGalleryState(); | |
} | |
class _PageviewGalleryState extends State<PageviewGallery> { | |
final PageController ctrl = PageController( | |
viewportFraction: 0.55, | |
); | |
int currentPage = 0; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: PageView.builder( | |
controller: ctrl, | |
itemCount: 8, | |
physics: const BouncingScrollPhysics(), | |
itemBuilder: (context, int index) { | |
// Active page | |
bool active = index == currentPage; | |
return _buildStoryPage(active); | |
}), | |
)); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
ctrl.addListener(() { | |
int pos = ctrl.page!.round(); | |
if (currentPage != pos) { | |
{ | |
setState(() { | |
currentPage = pos; | |
}); | |
} | |
} | |
}); | |
} | |
} | |
_buildStoryPage( bool active) { | |
// Animated Properties | |
final double blur = active ? 30 : 0; | |
final double offset = active ? 20 : 0; | |
final double top = active ? 100 : 200; | |
return AnimatedContainer( | |
duration: Duration(milliseconds: 500), | |
curve: Curves.easeOutQuint, | |
margin: EdgeInsets.only(top: top, bottom: 50, right: 30), | |
decoration: BoxDecoration( | |
borderRadius: BorderRadius.circular(20), | |
color :Colors.red, | |
boxShadow: [BoxShadow(color: Colors.black87, blurRadius: blur, offset: Offset(offset, offset))]), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment