Flutter 基础学习之 路由切换效果

main函数

import 'package:flutter/material.dart';
import 'first_page.dart';

void main() {
  runApp(MyApp());
}

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

FirstPage

import 'package:flutter/material.dart';
import 'custome_router.dart';

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(title: Text('First Page',
      style: TextStyle(fontSize: 36.0)),
      elevation: 0.0,),
      body: Center(
        child: MaterialButton(
         child: Icon(
            Icons.navigate_next,
            color: Colors.white,
            size: 64.0,
         ),
         onPressed: (){
           Navigator.of(context).push(CustomeRoute(SecondPage()));
         },
        ),
      ),
    );
  }
}

class  SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.pinkAccent,
      appBar: AppBar(title: Text('Second Page',
      style: TextStyle(fontSize: 36.0)),
      backgroundColor: Colors.pinkAccent,
      leading: Container(),
      elevation: 0.0,),
      body: Center(
        child: MaterialButton(
         child: Icon(
            Icons.navigate_before,
            color: Colors.white,
            size: 64.0,
         ),
         onPressed: (){
           Navigator.of(context).pop();
         },
        ),
      ),
    );
  }
}

动画组件

import 'package:flutter/material.dart';

class CustomeRoute extends PageRouteBuilder{

  final Widget widget;
  
  CustomeRoute(this.widget):super(
    transitionDuration: Duration(seconds: 1),
    pageBuilder:(
      BuildContext context,
      Animation<double> animation1,
      Animation<double> animation2,

    ){
        return widget;
    } ,
    transitionsBuilder: (
       BuildContext context,
      Animation<double> animation1,
      Animation<double> animation2,
       Widget child
    ){
      //渐隐渐显的动画效果
      // return FadeTransition(
      //   opacity: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(
      //     parent: animation1,
      //     curve: Curves.fastOutSlowIn
      //   )),
      //   child: child,
      // );
      //旋转加缩放动画效果
      // return ScaleTransition(scale: Tween(begin:0.0,end:1.0).animate(CurvedAnimation(
      //      parent: animation1,
      //      curve: Curves.fastOutSlowIn,)),
      //      child: child,
      // );
      //旋转
      // return RotationTransition(turns: Tween(begin:0.0,end:1.0).animate(CurvedAnimation(
      //     parent: animation1,
      //     curve: Curves.fastOutSlowIn,
      // )),
      // child: ScaleTransition(scale: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(
      //     parent: animation1,
      //     curve: Curves.fastOutSlowIn,
      // )),
      //     child:child,  
      // ),
      
      // );
      //左右滑动动画效果
      return SlideTransition(position: Tween<Offset>(
        begin: Offset(-1.0,0.0),
        end: Offset(0.0,0.0),).animate(CurvedAnimation(
          parent: animation1,
          curve: Curves.easeInOutCirc
        )),
        child: child,
        );
    }    
  );

}

参考文献:

https://www.bilibili.com/video/BV1kt411B7mu?p=6

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值