【问题标题】:How to make this page transaction effect on flutter如何让这个页面交易对flutter产生影响
【发布时间】:2023-03-30 19:59:01
【问题描述】:

如何让这个页面在flutter上产生交易效果

例子:

instagram stories

【问题讨论】:

    标签: flutter flutter-animation


    【解决方案1】:

    编辑
    使用包https://pub.dev/packages/swipedetector,您可以检测 SwipeDown
    并在 OnSwipeDown() 中使用 PageRouteBuilder

    执行您的路线更改逻辑
    SwipeDetector(
        child: ... //You Widget Tree here
        ),
        onSwipeUp: () {
            setState(() {
                _swipeDirection = "Swipe Up";
            });
        },
        onSwipeDown: () {
            setState(() {
                _swipeDirection = "Swipe Down";
            });
        },
        onSwipeLeft: () {
          setState(() {
            _swipeDirection = "Swipe Left";
          });
        },
        onSwipeRight: () {
          setState(() {
            _swipeDirection = "Swipe Right";
          });
        },
    )
    

    请使用 PageRouteBuilder 构建动画
    完整代码

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            // This is the theme of your application.
            //
            // Try running your application with "flutter run". You'll see the
            // application has a blue toolbar. Then, without quitting the app, try
            // changing the primarySwatch below to Colors.green and then invoke
            // "hot reload" (press "r" in the console where you ran "flutter run",
            // or simply save your changes to "hot reload" in a Flutter IDE).
            // Notice that the counter didn't reset back to zero; the application
            // is not restarted.
            primarySwatch: Colors.blue,
          ),
          home: HomePage(),
            onGenerateRoute: (RouteSettings settings) {
              switch (settings.name) {
                case '/':
                  return SlideRightRoute(widget:HomePage());
                  break;
                case '/second':
                  return SlideRightRoute(widget:SecondHome());
                  break;
              }
            }
        );
      }
    }
    
    
    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Home'),
          ),
          body: new Center(
            child: RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/second');
              },
              child: Text('Second Home'),
            ),
          ),
        );
      }
    }
    class SecondHome extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Second Home'),
          ),
          body: new Center(
            child: RaisedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Go Back'),
            ),
          ),
        );
      }
    }
    
    class SlideRightRoute extends PageRouteBuilder {
      final Widget widget;
      SlideRightRoute({this.widget})
          : super(
        pageBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation) {
          return widget;
        },
        transitionsBuilder: (BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child) {
          return new SlideTransition(
            position: new Tween<Offset>(
              begin: const Offset(1.0, 0.0),
              end: Offset.zero,
            ).animate(animation),
            child: child,
          );
        },
      );
    }
    

    【讨论】:

    • 谢谢,但这不是我要找的,如果你看一下它通过向下滑动关闭的页面,同时它有那个动画,我正在寻找的是一种方式向下滑动关闭页面,同时播放动画。
    • 我有编辑答案。你可以用包 swipedetector 来做到这一点
    • 坦克,我去看看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2018-10-14
    • 2011-06-11
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多