auto scroll on aya added.
parent
b0ed7662d5
commit
8e4e9f9b91
@ -0,0 +1,78 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:tangheem/widgets/auto_scroll_view/scroll_id.dart';
|
||||||
|
|
||||||
|
class AyaScrollViewer extends StatefulWidget {
|
||||||
|
final ScrollToId scrollToId;
|
||||||
|
final List<ScrollContent> children;
|
||||||
|
final Axis scrollDirection;
|
||||||
|
|
||||||
|
AyaScrollViewer({@required this.children, @required this.scrollToId, this.scrollDirection = Axis.vertical});
|
||||||
|
|
||||||
|
@override
|
||||||
|
_InteractiveScrollViewerState createState() => _InteractiveScrollViewerState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _InteractiveScrollViewerState extends State<AyaScrollViewer> {
|
||||||
|
List<String> _idList = [];
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
setData();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setData() {
|
||||||
|
widget.scrollToId.scrollDirection = widget.scrollDirection;
|
||||||
|
_idList = [];
|
||||||
|
widget.scrollToId.scrollContentsList = [];
|
||||||
|
for (ScrollContent scrollContents in widget.children) {
|
||||||
|
if (_idList.contains(scrollContents.id)) {
|
||||||
|
throw Exception('Do not use the same id');
|
||||||
|
} else {
|
||||||
|
_idList.add(scrollContents.id);
|
||||||
|
}
|
||||||
|
widget.scrollToId.scrollContentsList.add(ScrollContentWithKey.fromWithout(scrollContents));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didUpdateWidget(covariant AyaScrollViewer oldWidget) {
|
||||||
|
if (widget.children != oldWidget.children) {
|
||||||
|
setData();
|
||||||
|
}
|
||||||
|
super.didUpdateWidget(oldWidget);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return SingleChildScrollView(
|
||||||
|
scrollDirection: widget.scrollDirection,
|
||||||
|
controller: widget.scrollToId.scrollController,
|
||||||
|
physics: BouncingScrollPhysics(),
|
||||||
|
child: buildContent(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildContent() {
|
||||||
|
if (widget.scrollDirection == Axis.vertical) {
|
||||||
|
return Column(
|
||||||
|
children: widget.scrollToId.scrollContentsList.map((scrollContents) {
|
||||||
|
return buildRepaintBoundary(scrollContents);
|
||||||
|
}).toList(),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return Row(
|
||||||
|
children: widget.scrollToId.scrollContentsList.map((scrollContents) {
|
||||||
|
return buildRepaintBoundary(scrollContents);
|
||||||
|
}).toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RepaintBoundary buildRepaintBoundary(ScrollContentWithKey scrollContents) {
|
||||||
|
return RepaintBoundary(
|
||||||
|
key: scrollContents.key,
|
||||||
|
child: scrollContents.child,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,136 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class ScrollToId {
|
||||||
|
final ScrollController scrollController;
|
||||||
|
List<ScrollContentWithKey> scrollContentsList = [];
|
||||||
|
Axis scrollDirection;
|
||||||
|
|
||||||
|
ScrollToId({this.scrollController});
|
||||||
|
|
||||||
|
String idPosition() {
|
||||||
|
double sumSize = 0;
|
||||||
|
for (ScrollContentWithKey scrollContents in scrollContentsList) {
|
||||||
|
if (scrollDirection == Axis.vertical) {
|
||||||
|
sumSize += scrollContents.key.currentContext.size.height;
|
||||||
|
} else {
|
||||||
|
sumSize += scrollContents.key.currentContext.size.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scrollController.offset < sumSize) {
|
||||||
|
return scrollContents.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> animateTo(String id, {@required Duration duration, @required Curve curve}) async {
|
||||||
|
Function _function = (double offset) {
|
||||||
|
scrollController.animateTo(offset, duration: duration, curve: curve);
|
||||||
|
};
|
||||||
|
_scroll(id: id, scrollFunction: _function);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> jumpTo(String id) async {
|
||||||
|
Function _function = (double offset) {
|
||||||
|
scrollController.jumpTo(offset);
|
||||||
|
};
|
||||||
|
_scroll(id: id, scrollFunction: _function);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> animateToNext({@required Duration duration, @required Curve curve}) async {
|
||||||
|
Function _function = (double offset) {
|
||||||
|
scrollController.animateTo(offset, duration: duration, curve: curve);
|
||||||
|
};
|
||||||
|
_scroll(id: _getNextId(number: 1), scrollFunction: _function);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> jumpToNext() async {
|
||||||
|
Function _function = (double offset) {
|
||||||
|
scrollController.jumpTo(offset);
|
||||||
|
};
|
||||||
|
_scroll(id: _getNextId(number: 1), scrollFunction: _function);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> animateToBefore({@required Duration duration, @required Curve curve}) async {
|
||||||
|
Function _function = (double offset) {
|
||||||
|
scrollController.animateTo(offset, duration: duration, curve: curve);
|
||||||
|
};
|
||||||
|
_scroll(id: _getNextId(number: -1), scrollFunction: _function);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> jumpToBefore() async {
|
||||||
|
Function _function = (double offset) {
|
||||||
|
scrollController.jumpTo(offset);
|
||||||
|
};
|
||||||
|
_scroll(id: _getNextId(number: -1), scrollFunction: _function);
|
||||||
|
}
|
||||||
|
|
||||||
|
String _getNextId({int number}) {
|
||||||
|
double sumSize = 0;
|
||||||
|
for (int i = 0; i < scrollContentsList.length; i++) {
|
||||||
|
if (scrollDirection == Axis.vertical) {
|
||||||
|
sumSize += scrollContentsList[i].key.currentContext.size.height;
|
||||||
|
} else {
|
||||||
|
sumSize += scrollContentsList[i].key.currentContext.size.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scrollController.offset < sumSize) {
|
||||||
|
if (number == 1 && i == scrollContentsList.length - 1) return null;
|
||||||
|
if (number == -1 && i == 0) return null;
|
||||||
|
|
||||||
|
return scrollContentsList[i + number].id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _scroll({String id, Function scrollFunction}) {
|
||||||
|
double sumSize = 0;
|
||||||
|
|
||||||
|
for (ScrollContentWithKey scrollContents in scrollContentsList) {
|
||||||
|
if (scrollContents.id == id) {
|
||||||
|
try {
|
||||||
|
if (sumSize < scrollController.position.maxScrollExtent) {
|
||||||
|
scrollFunction(sumSize);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
scrollFunction(scrollController.position.maxScrollExtent);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('$e');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
if (scrollDirection == Axis.vertical) {
|
||||||
|
sumSize += scrollContents.key.currentContext.size.height;
|
||||||
|
} else {
|
||||||
|
sumSize += scrollContents.key.currentContext.size.width;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('$e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ScrollContent {
|
||||||
|
final String id;
|
||||||
|
final Widget child;
|
||||||
|
|
||||||
|
ScrollContent({@required this.id, @required this.child});
|
||||||
|
}
|
||||||
|
|
||||||
|
class ScrollContentWithKey {
|
||||||
|
final Widget child;
|
||||||
|
final String id;
|
||||||
|
final GlobalKey key;
|
||||||
|
|
||||||
|
ScrollContentWithKey({this.child, this.key, this.id});
|
||||||
|
|
||||||
|
factory ScrollContentWithKey.fromWithout(ScrollContent scrollContent) {
|
||||||
|
return ScrollContentWithKey(child: scrollContent.child, id: scrollContent.id, key: GlobalKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue