如何使用“CupertinoFullscreenDialogTransition”?

Par*_*eri 2 dart flutter flutter-layout

我没有找到构造函数的任何示例CupertinoFullscreenDialogTransition

https://api.flutter.dev/flutter/cupertino/CupertinoFullscreenDialogTransition-class.html

我试图理解下面的代码,但我没有明白。

CupertinoFullscreenDialogTransition({
  Key key,
  @required Animation<double> animation,
  @required this.child,
}) : _positionAnimation = CurvedAnimation(
       parent: animation,
       curve: Curves.linearToEaseOut,
       // The curve must be flipped so that the reverse animation doesn't play
       // an ease-in curve, which iOS does not use.
       reverseCurve: Curves.linearToEaseOut.flipped,
     ).drive(_kBottomUpTween),
     super(key: key);
Run Code Online (Sandbox Code Playgroud)

小智 6

这是一个更完整的例子

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

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

class MyApp extends StatelessWidget {
  static const String _title = 'AppBar tutorial';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        brightness: Brightness.light,
        primaryColor: Colors.blue[900],
        appBarTheme: AppBarTheme(iconTheme: IconThemeData(color: Colors.white)),
      ),
      title: _title,
      home: CupertinoFullscreenDialogTransitionPage(),
    );
  }
}

//First Page
class CupertinoFullscreenDialogTransitionPage extends StatefulWidget {
  @override
  _CupertinoFullscreenDialogTransitionState createState() =>
      _CupertinoFullscreenDialogTransitionState();
}

class _CupertinoFullscreenDialogTransitionState
    extends State<CupertinoFullscreenDialogTransitionPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(60),
        child: AppBar(
          title: Text("Cupertino Screen Transition"),
          centerTitle: true,
        ),
      ),
      body: Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          CupertinoButton.filled(
            child: Text("Next Page Cupertino Transition"),
            onPressed: () => Navigator.of(context).push(
              PageRouteBuilder(
                opaque: false,
                pageBuilder: (context, _, __) {
                  return FullDialogPage();
                },
              ),
            ),
          ),
        ],
      )),
    );
  }
}

//Second Page
class FullDialogPage extends StatefulWidget {
  @override
  _FullDialogPageState createState() => _FullDialogPageState();
}

class _FullDialogPageState extends State<FullDialogPage>
    with TickerProviderStateMixin {
  AnimationController _primary, _secondary;
  Animation<double> _animationPrimary, _animationSecondary;

  @override
  void initState() {
    //Primaty
    _primary = AnimationController(vsync: this, duration: Duration(seconds: 1));
    _animationPrimary = Tween<double>(begin: 0, end: 1)
        .animate(CurvedAnimation(parent: _primary, curve: Curves.easeOut));
    //Secondary
    _secondary =
        AnimationController(vsync: this, duration: Duration(seconds: 1));
    _animationSecondary = Tween<double>(begin: 0, end: 1)
        .animate(CurvedAnimation(parent: _secondary, curve: Curves.easeOut));
    _primary.forward();
    super.initState();
  }

  @override
  void dispose() {
    _primary.dispose();
    _secondary.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoFullscreenDialogTransition(
      primaryRouteAnimation: _animationPrimary,
      secondaryRouteAnimation: _animationSecondary,
      linearTransition: false,
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.indigo[900],
          title: Text("Testing"),
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              _primary.reverse();
              Future.delayed(Duration(seconds: 1), () {
                Navigator.of(context).pop();
              });
            },
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Joã*_*res 5

我做了这个简单的示例,希望它可以帮助您了解如何实现 CupertinoFullscreenDialogTransition Widget。

import 'package:flutter/cupertino.dart';
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(
        primarySwatch: Colors.orange,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>  with SingleTickerProviderStateMixin{
 AnimationController _animationController;

 @override
  void initState() {
   _animationController = AnimationController(
     vsync: this,
     duration: Duration(milliseconds: 500),
   );
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Stackoverflow playground'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            CupertinoFullscreenDialogTransition(
            primaryRouteAnimation: _animationController,
            secondaryRouteAnimation: _animationController,
            linearTransition: false,
              child: Center(
                child: Container(
                  color: Colors.blueGrey,
                  width: 300,
                  height: 300,
                ),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                RaisedButton(
                  onPressed: () => _animationController.forward(),
                  child: Text('Forward'),
                ),
                RaisedButton(
                  onPressed: () => _animationController.reverse(),
                  child: Text('Reverse'),
                ),
              ],
            ),
          ],
        ),
      )
    );
  }
}
Run Code Online (Sandbox Code Playgroud)