|
| 1 | +/// |
| 2 | +/// Created by NieBin on 2019/6/15 |
| 3 | +/// Github: https://github.com/nb312 |
| 4 | +/// Email: niebin312@gmail.com |
| 5 | +
|
| 6 | +import "package:flutter/material.dart"; |
| 7 | +import 'package:flutter_widgets/const/_const.dart'; |
| 8 | +import 'NavPage.dart'; |
| 9 | + |
| 10 | +class NavigatorPage extends StatefulWidget { |
| 11 | + Key _key = GlobalKey<_NavigatorState>(); |
| 12 | + |
| 13 | + @override |
| 14 | + _NavigatorState createState() => _NavigatorState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _NavigatorState extends State<NavigatorPage> { |
| 18 | + Future<void> _startPage() async { |
| 19 | + var content = await Navigator.push( |
| 20 | + context, |
| 21 | + MaterialPageRoute<String>( |
| 22 | + builder: (context) => NavPage(), |
| 23 | + ), |
| 24 | + ); |
| 25 | + print("Content: $content"); |
| 26 | + } |
| 27 | + |
| 28 | + Widget _pushWithBack() => FlatButton( |
| 29 | + onPressed: () { |
| 30 | + print("Hello world"); |
| 31 | + _startPage(); |
| 32 | + }, |
| 33 | + child: Text("jump"), |
| 34 | + color: PURPLE, |
| 35 | + ); |
| 36 | + |
| 37 | + Future<void> _transPage() { |
| 38 | + Navigator.push( |
| 39 | + context, |
| 40 | + PageRouteBuilder( |
| 41 | + opaque: false, |
| 42 | + barrierColor: GREEN, |
| 43 | + pageBuilder: (BuildContext context, _, __) { |
| 44 | + return NavPage(); |
| 45 | + }, |
| 46 | + transitionsBuilder: |
| 47 | + (___, Animation<double> animation, ____, Widget child) { |
| 48 | + return FadeTransition( |
| 49 | + opacity: animation, |
| 50 | + child: RotationTransition( |
| 51 | + turns: Tween<double>(begin: 0.5, end: 2.0).animate(animation), |
| 52 | + child: child, |
| 53 | + ), |
| 54 | + ); |
| 55 | + })); |
| 56 | + } |
| 57 | + |
| 58 | + Widget _transButton() => FlatButton( |
| 59 | + child: Text("Trans"), |
| 60 | + color: RED_LIGHT, |
| 61 | + onPressed: () { |
| 62 | + _transPage(); |
| 63 | + }, |
| 64 | + ); |
| 65 | + |
| 66 | + Widget _slidButton(BuildContext context) => FlatButton( |
| 67 | + child: Text("Slid"), |
| 68 | + color: RED_LIGHT, |
| 69 | + onPressed: () { |
| 70 | + Navigator.push(context, SlideRightRoute(page: NavPage())); |
| 71 | + }, |
| 72 | + ); |
| 73 | + |
| 74 | + Widget _doublePage(BuildContext context) => Container( |
| 75 | + constraints: BoxConstraints.expand(height: 100), |
| 76 | + child: FlatButton( |
| 77 | + child: Text("Double Page"), |
| 78 | + color: RED_LIGHT, |
| 79 | + onPressed: () { |
| 80 | + Navigator.push(context, |
| 81 | + EnterExitRoute(exitPage: widget, enterPage: NavPage())); |
| 82 | + }, |
| 83 | + ), |
| 84 | + ); |
| 85 | + |
| 86 | + @override |
| 87 | + Widget build(BuildContext context) { |
| 88 | + return Scaffold( |
| 89 | + appBar: AppBar( |
| 90 | + title: Text(PageName.INTER_NAV), |
| 91 | + ), |
| 92 | + body: Column( |
| 93 | + mainAxisAlignment: MainAxisAlignment.spaceAround, |
| 94 | + children: <Widget>[ |
| 95 | + Container( |
| 96 | + constraints: BoxConstraints.expand(height: 100), |
| 97 | + child: _pushWithBack(), |
| 98 | + ), |
| 99 | + Container( |
| 100 | + constraints: BoxConstraints.expand(height: 100), |
| 101 | + child: _transButton(), |
| 102 | + ), |
| 103 | + Container( |
| 104 | + constraints: BoxConstraints.expand(height: 100), |
| 105 | + child: _slidButton(context), |
| 106 | + ), |
| 107 | + _doublePage(context), |
| 108 | + ], |
| 109 | + ), |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class SlideRightRoute extends PageRouteBuilder { |
| 115 | + final Widget page; |
| 116 | + |
| 117 | + SlideRightRoute({this.page}) |
| 118 | + : super( |
| 119 | + pageBuilder: ( |
| 120 | + BuildContext context, |
| 121 | + Animation<double> animation, |
| 122 | + Animation<double> secondaryAnimation, |
| 123 | + ) => |
| 124 | + page, |
| 125 | + transitionsBuilder: ( |
| 126 | + BuildContext context, |
| 127 | + Animation<double> animation, |
| 128 | + Animation<double> secondaryAnimation, |
| 129 | + Widget child, |
| 130 | + ) => |
| 131 | + SlideTransition( |
| 132 | + position: Tween<Offset>( |
| 133 | + begin: const Offset(-1, 0), |
| 134 | + end: Offset.zero, |
| 135 | + ).animate(animation), |
| 136 | + child: child, |
| 137 | + ), |
| 138 | + barrierColor: GREEN, |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +class EnterExitRoute extends PageRouteBuilder { |
| 143 | + final Widget enterPage; |
| 144 | + final Widget exitPage; |
| 145 | + |
| 146 | + EnterExitRoute({this.exitPage, this.enterPage}) |
| 147 | + : super( |
| 148 | + pageBuilder: ( |
| 149 | + BuildContext context, |
| 150 | + Animation<double> animation, |
| 151 | + Animation<double> secondaryAnimation, |
| 152 | + ) => |
| 153 | + enterPage, |
| 154 | + transitionsBuilder: ( |
| 155 | + BuildContext context, |
| 156 | + Animation<double> animation, |
| 157 | + Animation<double> secondaryAnimation, |
| 158 | + Widget child, |
| 159 | + ) => |
| 160 | + Stack( |
| 161 | + children: <Widget>[ |
| 162 | + SlideTransition( |
| 163 | + position: new Tween<Offset>( |
| 164 | + begin: const Offset(0.0, 0.0), |
| 165 | + end: const Offset(-1.0, -1.0), |
| 166 | + ).animate(animation), |
| 167 | + child: exitPage, |
| 168 | + ), |
| 169 | + SlideTransition( |
| 170 | + position: new Tween<Offset>( |
| 171 | + begin: const Offset(1.0, 1.0), |
| 172 | + end: Offset.zero, |
| 173 | + ).animate(animation), |
| 174 | + child: enterPage, |
| 175 | + ) |
| 176 | + ], |
| 177 | + ), |
| 178 | + ); |
| 179 | +} |
0 commit comments