Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Li committed Aug 6, 2019
1 parent 99a57d6 commit c0e11c8
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/pages/category_page/category_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
import '../../utils/color.dart';
Expand All @@ -9,6 +11,8 @@ class CategoryPage extends StatefulWidget {
}

class _CategoryPageState extends State<CategoryPage> {
Timer _timer;
bool _isSelectedTimeOut = true;
ScrollController _scrollController = ScrollController();
AutoScrollController _controller = AutoScrollController();
var _keys = {};
Expand Down Expand Up @@ -47,6 +51,12 @@ class _CategoryPageState extends State<CategoryPage> {
});
}

@override
void dispose() {
_timer?.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -84,14 +94,15 @@ class _CategoryPageState extends State<CategoryPage> {
},
child: ListView(
controller: _controller,
physics: ClampingScrollPhysics(),
children: _categorys.map<Widget>((f) {
_keys[f['index']] = new GlobalKey();
return AutoScrollTag(
key: ValueKey(f['index']),
controller: _controller,
index: f['index'],
child: Container(
height: 160.0,
height: 200.0,
child: Text(f['name'] + ': ${f['index']}'),
key: _keys[f['index']],
),
Expand All @@ -106,21 +117,28 @@ class _CategoryPageState extends State<CategoryPage> {
}

_leftAnimateToIndex(int index) {
if (_selectedCategory['index'] != index) {
if (_selectedCategory['index'] != index && _isSelectedTimeOut) {
setState(() {
_selectedCategory = _categorys[index];
});
_scrollController.position.moveTo(index * 60.0);
_scrollController.position.moveTo(index * 50.0 - 50.0);
}
}

_rightAnimateToIndex(f) {
setState(() {
_isSelectedTimeOut = false;
_selectedCategory = f;
});
_controller.scrollToIndex(f['index'],
duration: Duration(milliseconds: 200),
preferPosition: AutoScrollPosition.begin);

_timer = Timer(Duration(milliseconds: 2000), () {
setState(() {
_isSelectedTimeOut = true;
});
});
}

getRectFromKey(int index, GlobalKey globalKey) {
Expand Down Expand Up @@ -178,7 +196,7 @@ class _CategoryPageState extends State<CategoryPage> {
? Colors.white
: Colors.transparent,
alignment: Alignment.center,
height: 60.0,
height: 50.0,
child: Container(
alignment: Alignment.center,
width: 100.0,
Expand All @@ -201,7 +219,7 @@ class _CategoryPageState extends State<CategoryPage> {
? ColorUtil.getColor('primary')
: null,
fontSize:
f['index'] == _selectedCategory['index'] ? 16.0 : null,
f['index'] == _selectedCategory['index'] ? 14.0 : 12.0,
fontWeight: f['index'] == _selectedCategory['index']
? FontWeight.w600
: null,
Expand Down

0 comments on commit c0e11c8

Please sign in to comment.