通过向左或向右滑动来管理Offset的dx。

在Flutter应用程序中,我想将屏幕宽度分为10个部分,当用户向右或向左滑动时,我可以检测到这一滑动的每个部分,例如

当我把屏幕分割成10部分后,我有一个变量,名为 screenParts 双倍 0.0 默认情况下,当用户向右滑动时,变量值应该是加1分,向左滑动时应该是减去变量值减去

这个变量值应该是 between 0.0 and 1你可以考虑 Tween<double>

double screenParts = 0.0;
final double screenWidth = MediaQuery.of(context).size.width / 10;

我想在这部分代码中使用这个值。

return GestureDetector(
  onPanUpdate: (details) {
    if (details.delta.dx > 0) {
      if (screenParts / screenWidth == 0) screenParts = screenParts += 0.1;
    } else if (details.delta.dx < 0) {
      if (screenParts / screenWidth == 0) screenParts = screenParts -= 0.1;
    }
    setState(() {});
  },
  child: SafeArea(
    child: FadeTransition(
      opacity: CurvedAnimation(parent: animation, curve: Curves.fastLinearToSlowEaseIn),
      child: SlideTransition(
          position: Tween<Offset>(
            begin: animateDirection,
            end: Offset(screenParts, 0), //<---- this part
          ).animate(CurvedAnimation(
            parent: animation,
            curve: Curves.fastLinearToSlowEaseIn,
          )),
          // ignore: void_checks
          child: Material(elevation: 8.0, child: child)),
    ),
  ),
);

这意味着我试图管理 dxOffsetscreenParts 左右滑动

解决方案:

enter image description here这是你正在寻找什么?

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: SafeArea(
          child: MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  double position = 0.0;

  @override
  void initState() {
    super.initState();
  }

  void handleXChange(double deltaX) {
    setState(() {
      position = deltaX / MediaQuery.of(context).size.width;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Text(position.toString()),
        DragArea(
          handleXChange: handleXChange,
        ),
      ],
    );
  }
}

class DragArea extends StatefulWidget {
  const DragArea({Key key, @required this.handleXChange}) : super(key: key);

  final void Function(double newX) handleXChange;

  @override
  _DragAreaState createState() => _DragAreaState();
}

class _DragAreaState extends State<DragArea> {
  double initX;

  void onPanStart(DragStartDetails details) {
    initX = details.globalPosition.dx;
  }

  void onPanUpdate(DragUpdateDetails details) {
    var x = details.globalPosition.dx;
    var deltaX = x - initX;
    widget.handleXChange(deltaX);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onPanStart: onPanStart,
      onPanUpdate: onPanUpdate,
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(
            color: Colors.blueAccent,
          ),
        ),
      ),
    );
  }
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

指针。读取访问违规 - 为什么?

2022-9-9 2:34:20

未分类

我如何在当前目录下创建一个以g开头的所有文件的档案?

2022-9-9 2:34:22

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索