Skip to content

Commit

Permalink
Fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
jzoom committed Aug 28, 2018
1 parent e362d90 commit 082a8b5
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 51 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG-ZH.md
Expand Up @@ -51,4 +51,7 @@
* 修复一些错别字,感谢[csharad](https://github.com/csharad)

## [1.0.5] - [2018/08/09]
* viewportFraction<1.0增加fade参数
* viewportFraction<1.0增加fade参数

## [1.0.6] - [2018/08/28]
* `TINDER` 和 `STACK` 这两种布局方式实现垂直滚动, #9
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -57,4 +57,7 @@
* Fix some typo,thanks to [csharad](https://github.com/csharad)

## [1.0.5] - [2018/08/09]
* Add feature : support fade for `viewportFraction`
* Add feature : support fade for `viewportFraction`

## [1.0.6] - [2018/08/28]
* Implement vertical scroll direction for `TINDER` and `STACK` layout, see #9
14 changes: 4 additions & 10 deletions example/lib/main.dart
Expand Up @@ -18,18 +18,12 @@ class MyApp extends StatelessWidget {
child: new Column(
children: <Widget>[
new SizedBox(
height: 200.0,
height: 300.0,
child: Swiper(
layout: SwiperLayout.DEFAULT,
customLayoutOption: new CustomLayoutOption(
startIndex: -1, stateCount: 3)
.addRotate([-45.0 / 180, 0.0, 45.0 / 180]).addTranslate([
new Offset(-370.0, -40.0),
new Offset(0.0, 0.0),
new Offset(370.0, -40.0)
]),
layout: SwiperLayout.TINDER,
itemWidth: 300.0,
itemHeight: 200.0,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
return new Container(
color: Colors.grey,
Expand All @@ -55,7 +49,7 @@ class MyApp extends StatelessWidget {
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Swiper'),
// home: buildHome() ,
//home: buildHome() ,
routes: {
'/example01': (BuildContext context) => new ExampleHorizontal(),
'/example02': (BuildContext context) => new ExampleVertical(),
Expand Down
15 changes: 12 additions & 3 deletions example/lib/src/ExampleCustom.dart
Expand Up @@ -21,7 +21,6 @@ class _ExampleCustomState extends State<ExampleCustom> {

int _autoplayDely;

Axis _axis;

double _padding;

Expand All @@ -37,6 +36,9 @@ class _ExampleCustomState extends State<ExampleCustom> {

double _scale;


Axis _scrollDirection;

Curve _curve;

double _fade;
Expand Down Expand Up @@ -84,9 +86,9 @@ class _ExampleCustomState extends State<ExampleCustom> {
_itemCount = 3;
_autoplay = false;
_autoplayDely = 3000;
_axis = Axis.horizontal;
_viewportFraction = 0.8;
_outer = false;
_scrollDirection = Axis.horizontal;
super.initState();
}

Expand Down Expand Up @@ -115,6 +117,7 @@ class _ExampleCustomState extends State<ExampleCustom> {
_currentIndex = index;
});
},

curve: _curve,
scale: _scale,
itemWidth: 300.0,
Expand All @@ -128,7 +131,7 @@ class _ExampleCustomState extends State<ExampleCustom> {
autoplay: _autoplay,
itemBuilder: _buildItem,
itemCount: _itemCount,
scrollDirection: _axis,
scrollDirection: _scrollDirection,
pagination: new SwiperPagination(),
);
}
Expand Down Expand Up @@ -178,6 +181,12 @@ class _ExampleCustomState extends State<ExampleCustom> {
_layout = value;
setState(() {});
})),
new FormWidget(
label: "scrollDirection",
child: new Switch(
value: _scrollDirection == Axis.horizontal,
onChanged: (bool value) => setState(() => _scrollDirection = value ? Axis.horizontal : Axis.vertical)),
),
//Pannel Begin
new FormWidget(
label: "loop",
Expand Down
10 changes: 10 additions & 0 deletions example/lib/src/ExampleSwiperInScrollView.dart
Expand Up @@ -23,6 +23,14 @@ class _ExampleState extends State<ExampleSwiperInScrollView> with TickerProvider

_ExampleState();


@override
void dispose() {
controller.dispose();

super.dispose();
}

@override
void initState() {
controller = new AnimationController(vsync: this);
Expand All @@ -34,6 +42,8 @@ class _ExampleState extends State<ExampleSwiperInScrollView> with TickerProvider
super.initState();
}



Widget _buildDynamicCard(){
return new Card(
elevation: 2.0,
Expand Down
9 changes: 8 additions & 1 deletion example/pubspec.lock
Expand Up @@ -82,7 +82,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.2"
version: "1.0.6"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -214,6 +214,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.1"
percent_indicator:
dependency: "direct main"
description:
name: percent_indicator
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.9"
plugin:
dependency: transitive
description:
Expand Down
65 changes: 52 additions & 13 deletions lib/src/custom_layout.dart
Expand Up @@ -5,7 +5,8 @@ part of 'swiper.dart';

abstract class _CustomLayoutStateBase<T extends _SubSwiper> extends State<T>
with SingleTickerProviderStateMixin {
double _screenWidth;
double _swiperWidth;
double _swiperHeight;
Animation<double> _animation;
AnimationController _animationController;
int _startIndex;
Expand All @@ -23,6 +24,8 @@ abstract class _CustomLayoutStateBase<T extends _SubSwiper> extends State<T>
super.initState();
}



void _createAnimationController() {
_animationController = new AnimationController(vsync: this, value: 0.5);
Tween<double> tween = new Tween(begin: 0.0, end: 1.0);
Expand All @@ -31,10 +34,30 @@ abstract class _CustomLayoutStateBase<T extends _SubSwiper> extends State<T>

@override
void didChangeDependencies() {
_screenWidth = MediaQuery.of(context).size.width;


WidgetsBinding.instance
.addPostFrameCallback(_getSize);
super.didChangeDependencies();
}

void _getSize(_){
afterRender();
}

@mustCallSuper
void afterRender(){

RenderObject renderObject = context.findRenderObject();
Size size = renderObject.paintBounds.size;
_swiperWidth = size.width;
_swiperHeight = size.height;
setState(() {


});
}

@override
void didUpdateWidget(T oldWidget) {
if (widget.controller != oldWidget.controller) {
Expand Down Expand Up @@ -75,6 +98,7 @@ abstract class _CustomLayoutStateBase<T extends _SubSwiper> extends State<T>
}

Widget _buildAnimation(BuildContext context, Widget w) {

List<Widget> list = [];

double animationValue = _animation.value;
Expand All @@ -94,14 +118,19 @@ abstract class _CustomLayoutStateBase<T extends _SubSwiper> extends State<T>
onPanStart: _onPanStart,
onPanEnd: _onPanEnd,
onPanUpdate: _onPanUpdate,
child: new Center(
child: _buildContainer(list),
child: new ClipRect(
child: new Center(
child: _buildContainer(list),
),
),
);
}

@override
Widget build(BuildContext context) {
if(_animationCount==null){
return new Container();
}
return new AnimatedBuilder(
animation: _animationController, builder: _buildAnimation);
}
Expand Down Expand Up @@ -176,14 +205,18 @@ abstract class _CustomLayoutStateBase<T extends _SubSwiper> extends State<T>

void _onPanEnd(DragEndDetails details) {
if (_lockScroll) return;

double velocity = widget.scrollDirection == Axis.horizontal?
details.velocity.pixelsPerSecond.dx :details.velocity.pixelsPerSecond.dy;

if (_animationController.value >= 0.75 ||
details.velocity.pixelsPerSecond.dx > 500.0) {
velocity > 500.0) {
if (_currentIndex <= 0 && !widget.loop) {
return;
}
_move(1.0, nextIndex: _currentIndex - 1);
} else if (_animationController.value < 0.25 ||
details.velocity.pixelsPerSecond.dx < -500.0) {
velocity < -500.0) {
if (_currentIndex >= widget.itemCount - 1 && !widget.loop) {
return;
}
Expand All @@ -193,10 +226,19 @@ abstract class _CustomLayoutStateBase<T extends _SubSwiper> extends State<T>
}
}

void _onPanStart(DragStartDetails details) {
if (_lockScroll) return;
_currentValue = _animationController.value ;
_currentPos =widget.scrollDirection == Axis.horizontal ?
details.globalPosition.dx : details.globalPosition.dy;
}


void _onPanUpdate(DragUpdateDetails details) {
if (_lockScroll) return;
double value = _currentValue +
(details.globalPosition.dx - _currentPos) / _screenWidth / 2;
((widget.scrollDirection == Axis.horizontal ?
details.globalPosition.dx : details.globalPosition.dy) - _currentPos) / _swiperWidth / 2;
// no loop ?
if (!widget.loop) {
if (_currentIndex >= widget.itemCount - 1) {
Expand All @@ -213,11 +255,6 @@ abstract class _CustomLayoutStateBase<T extends _SubSwiper> extends State<T>
_animationController.value = value;
}

void _onPanStart(DragStartDetails details) {
if (_lockScroll) return;
_currentValue = _animationController.value;
_currentPos = details.globalPosition.dx;
}

int _currentIndex = 0;
}
Expand Down Expand Up @@ -357,6 +394,7 @@ class _CustomLayoutSwiper extends _SubSwiper {
int duration,
int index,
int itemCount,
Axis scrollDirection,
SwiperController controller})
: assert(option != null),
super(
Expand All @@ -370,7 +408,8 @@ class _CustomLayoutSwiper extends _SubSwiper {
duration: duration,
index: index,
itemCount: itemCount,
controller: controller);
controller: controller,
scrollDirection:scrollDirection);

@override
State<StatefulWidget> createState() {
Expand Down

0 comments on commit 082a8b5

Please sign in to comment.