zoukankan      html  css  js  c++  java
  • Flutter 页面入栈和出栈

    Docs

    demo

    import 'package:flutter/material.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Center(
          child: RaisedButton(
            child: Text('跳转到 screen2'),
            onPressed: () {
              Navigator.of(context).push(MaterialPageRoute(
                builder: (context) => Screen2(),
              ));
            },
          ),
        ));
      }
    }
    
    class Screen2 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: RaisedButton(
              child: Text('back'),
              onPressed: () {
                Navigator.of(context).pop(null);
              },
            ),
          ),
        );
      }
    }
    

    配置 routes 跳转

    import 'package:flutter/material.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Ajnauw',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
          routes: <String, WidgetBuilder>{
            '/a': (BuildContext context) => MyPage(title: 'page A'),
            '/b': (BuildContext context) => MyPage(title: 'page B'),
            '/c': (BuildContext context) => MyPage(title: 'page C'),
          },
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key}) : super(key: key);
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final List<String> _pages = <String>['/a', '/b', '/c'];
      String togoPage = '/b';
    
      _handleRadioValueChange(String nv) {
        setState(() {
          togoPage = nv;
        });
      }
    
      // 返回一个小widget
      Widget _generagorRadio(String pageName) {
        return GestureDetector(
          onTap: () {
            _handleRadioValueChange(pageName);
          },
          child: Container(
            decoration: BoxDecoration(
              color: Colors.grey[200],
            ),
            margin: EdgeInsets.only(top: 8.0),
            child: Row(
              children: <Widget>[
                Radio(
                  value: pageName,
                  onChanged: _handleRadioValueChange,
                  groupValue: togoPage,
                ),
                Text(pageName),
              ],
            ),
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('demo'),
          ),
          body: ListView(
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(left: 8.0, right: 8.0),
                child: Column(
                  children: _pages
                      .map((String pageName) => _generagorRadio(pageName))
                      .toList(),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 8.0, right: 8.0, top: 8.0),
                child: RaisedButton(
                  onPressed: () {
                    // 点击触发事件,页面入栈
                    Navigator.pushNamed(context, togoPage);
                  },
                  color: Theme.of(context).primaryColor,
                  child: Text('跳转新页面 $togoPage'),
                ),
              ),
            ],
          ),
        );
      }
    }
    
    class MyPage extends StatelessWidget {
      final String title;
    
      MyPage({Key key, this.title}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(title),
          ),
          body: Center(
            child: Text(title),
          ),
        );
      }
    }
    

    动画页面路劲转换

              Navigator.of(context).push(
                PageRouteBuilder(
                  transitionDuration: Duration(milliseconds: 500),
                  pageBuilder: (context, animation, secondaryAnimation) =>
                      Screen2(),
                  transitionsBuilder:
                      (context, animation, secondaryAnimation, child) {
                    var begin = Offset(0.0, 1.0);
                    var end = Offset.zero;
                    var curve = Curves.ease;
    
                    var tween = Tween(begin: begin, end: end);
                    var curvedAnimation = CurvedAnimation(
                      parent: animation,
                      curve: curve,
                    );
                    return SlideTransition(
                      position: tween.animate(curvedAnimation),
                      child: child,
                    );
                  },
                ),
              );
    

    pushReplacement

    通过推送给定路线替换导航器的当前路线,然后在新路线完成动画输入后处置前一路线

    Navigator.of(context).pushReplacement(
      MaterialPageRoute(builder: (context) => HomePage())
    )
    

    设置404页面

        MaterialApp(
          navigatorObservers: [routeObserver],
          home: HomePage(),
          title: 'home',
          onUnknownRoute: (RouteSettings settings) {
            return MaterialPageRoute(
              builder: (context) => Title(
                title: '页面未找到',
                color: Theme.of(context).primaryColor,
                child: Scaffold(
                  appBar: AppBar(),
                  body: Center(child: Text('404')),
                ),
              ),
              settings: RouteSettings(
                name: 'not-found',
              ),
            );
          },
        );
    

    设置不同的进场/出场动画

    Navigator.of(context).push(
      PageRouteBuilder(
          transitionDuration: Duration(milliseconds: 500),
          pageBuilder: (context, animation, secondaryAnimation) =>
              Scaffold(
                backgroundColor: Colors.red,
                body: Center(child: Text('page 2')),
                floatingActionButton: FloatingActionButton(
                  child: Icon(Icons.backspace),
                  onPressed: () => Navigator.of(context).pop(),
                ),
              ),
          transitionsBuilder: (
            context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            child,
          ) {
            return SlideTransition(
              position: Tween(
                // 关键点
                begin: animation.status == AnimationStatus.reverse
                    ? const Offset(-1.0, -1.0)
                    : const Offset(1.0, 1.0),
                end: Offset.zero,
              ).animate(animation),
              child: child,
            );
          }),
    );
    
  • 相关阅读:
    cut的使用
    linux三剑客之一 sed
    uniq指令
    CF940A Points on the line 思维
    2018年全国多校算法寒假训练营练习比赛(第二场) B TaoTao要吃鸡 01背包变形题
    CF922A Cloning Toys
    牛客网 Wannafly挑战赛 C 列一列 简单题 (题目有点坑)
    牛客网 Wannafly挑战赛 A 找一找 思考题
    B. Tea Queue codeforces Round.37.div2 队列
    线段树+离散化 poj 2528 Mayor's posters
  • 原文地址:https://www.cnblogs.com/ajanuw/p/10475578.html
Copyright © 2011-2022 走看看