如何使页面事务对扑动有影响

时间:2019-08-18 23:06:39

标签: flutter flutter-animation

如何使页面事务在颤动中发挥作用

示例:

instagram stories

1 个答案:

答案 0 :(得分:0)

修改
使用软件包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,
      );
    },
  );
}

enter image description here