|
import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// This is the main application widget. class MyApp extends StatelessWidget { ? const MyApp({Key? key}) : super(key: key);
? static const String _title = 'Flutter Code Sample';
? @override ? Widget build(BuildContext context) { ? ? return const MaterialApp( ? ? ? title: _title, ? ? ? home: MyStatefulWidget(), ? ? ); ? } }
class MyStatefulWidget extends StatefulWidget { ? const MyStatefulWidget({Key? key}) : super(key: key);
? @override ? State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); }
class _MyStatefulWidgetState extends State<MyStatefulWidget> ? ? with TickerProviderStateMixin { ? late final AnimationController _controller = AnimationController( ? ? duration: const Duration(seconds: 10), ? ? vsync: this, ? );
? final VelocityTracker _vt = ? ? ? VelocityTracker.withKind(PointerDeviceKind.touch); //手速控制
? //最大高度 ? double maxHeight = 700; ? //最小高度 ? double minHeight = 100; ? @override ? void dispose() { ? ? _controller.dispose(); ? ? super.dispose(); ? }
? @override ? Widget build(BuildContext context) { ? ? return Scaffold(body: LayoutBuilder( ? ? ? builder: (BuildContext context, BoxConstraints constraints) { ? ? ? ? return Container( ? ? ? ? ? ? height: constraints.maxHeight, ? ? ? ? ? ? width: constraints.maxWidth, ? ? ? ? ? ? child: Stack( ? ? ? ? ? ? ? children: [ ? ? ? ? ? ? ? ? Positioned( ? ? ? ? ? ? ? ? ? left: 0, ? ? ? ? ? ? ? ? ? // top: 0, ? ? ? ? ? ? ? ? ? right: 0, ? ? ? ? ? ? ? ? ? bottom: 0, ? ? ? ? ? ? ? ? ? child: Listener( ? ? ? ? ? ? ? ? ? ? onPointerDown: (PointerDownEvent p) => ? ? ? ? ? ? ? ? ? ? ? ? _vt.addPosition(p.timeStamp, p.position), ? ? ? ? ? ? ? ? ? ? onPointerMove: (PointerMoveEvent p) { ? ? ? ? ? ? ? ? ? ? ? _vt.addPosition(p.timeStamp, ? ? ? ? ? ? ? ? ? ? ? ? ? p.position); // add current position for velocity tracking ? ? ? ? ? ? ? ? ? ? ? _onGestureSlide(p.delta.dy); ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? onPointerUp: (PointerUpEvent p) => ? ? ? ? ? ? ? ? ? ? ? ? _onGestureEnd(_vt.getVelocity()), ? ? ? ? ? ? ? ? ? ? child: AnimatedBuilder( ? ? ? ? ? ? ? ? ? ? ? animation: _controller, ? ? ? ? ? ? ? ? ? ? ? builder: (BuildContext context, Widget? child) { ? ? ? ? ? ? ? ? ? ? ? ? return Container( ? ? ? ? ? ? ? ? ? ? ? ? ? height: _controller.value * (maxHeight - minHeight) + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? minHeight, ? ? ? ? ? ? ? ? ? ? ? ? ? color: Colors.purple, ? ? ? ? ? ? ? ? ? ? ? ? ); ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ), ? ? ? ? ? ? ? ? ? ), ? ? ? ? ? ? ? ? ) ? ? ? ? ? ? ? ], ? ? ? ? ? ? )); ? ? ? }, ? ? )); ? }
? void _onGestureSlide(double dy) { ? ? _controller.value -= dy / (maxHeight - minHeight); //滑动距离 ?跟高度差值的比例值 ? }
? void _onGestureEnd(Velocity v) { ? ? double minFlingVelocity = 365.0;
? ? if (_controller.isAnimating) return;
? ? double visualVelocity = ? ? ? ? -v.pixelsPerSecond.dy / (maxHeight - minHeight); //判断下滑还是上滑
? ? //这是速度很大时候 ? ? if (v.pixelsPerSecond.dy.abs() >= minFlingVelocity) { ? ? ? if (visualVelocity > 0) { ? ? ? ? _controller.value = 1; //如果是 ? ? ? } else { ? ? ? ? _controller.value = 0; ? ? ? }
? ? ? return; ? ? }
? ? // 检查控制器是否已经在一半的位置 ? ? if (_controller.value >= 0.5) { ? ? ? _controller.value = 1; ? ? } else { ? ? ? _controller.value = 0; ? ? } ? } }
这个思路由??sliding_up_panel? 这个插件而来。需要的朋友可以选择这个插件,很不错的
|