Skip to content

Instantly share code, notes, and snippets.

@ercantomac
Last active July 3, 2022 21:49
Show Gist options
  • Save ercantomac/2c787703e40d28ae34f1d5a8da0c7f95 to your computer and use it in GitHub Desktop.
Save ercantomac/2c787703e40d28ae34f1d5a8da0c7f95 to your computer and use it in GitHub Desktop.

Revisions

  1. ercantomac revised this gist Jul 3, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -78,6 +78,7 @@ class _MyHomePageState extends State<MyHomePage> {
    void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
    //THE REASON FOR DOING THIS: MAIN ROUTE HAS TO BE PUSHED WITH THE CUSTOM ROUTE AS WELL, OTHERWISE IT DOESN'T WORK, SO THIS IS THE BEST SOLUTION I CAME UP WITH.
    if (widget.replace) {
    Navigator.of(context).pushReplacement(_createRoute(const MyHomePage(replace: false)));
    }
  2. ercantomac revised this gist Jul 3, 2022. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,12 @@ class MyApp extends StatelessWidget {

    @override
    Widget build(BuildContext context) {
    return const MaterialApp(
    return MaterialApp(
    debugShowCheckedModeBanner: false,
    home: MyHomePage(replace: true),
    theme: ThemeData(
    primarySwatch: Colors.amber,
    ),
    home: const MyHomePage(replace: true),
    );
    }
    }
  3. ercantomac created this gist Jul 3, 2022.
    99 changes: 99 additions & 0 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    import 'package:flutter/material.dart';

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

    class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
    return const MaterialApp(
    debugShowCheckedModeBanner: false,
    home: MyHomePage(replace: true),
    );
    }
    }

    class SecondRoute extends StatelessWidget {
    const SecondRoute({Key? key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    backgroundColor: Colors.redAccent.shade400,
    body: Center(
    child: TextButton.icon(
    onPressed: () {
    Navigator.of(context).pop();
    },
    icon: const Icon(Icons.arrow_back_rounded),
    label: const Text('Return to 1st route'),
    ),
    ),
    );
    }
    }

    class MyHomePage extends StatefulWidget {
    const MyHomePage({Key? key, required this.replace}) : super(key: key);
    final bool replace;

    @override
    State<MyHomePage> createState() => _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {
    Route _createRoute(Widget newRoute) {
    return PageRouteBuilder(
    pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return Align(
    alignment: Alignment.topCenter,
    child: SizeTransition(
    sizeFactor: Tween<double>(begin: 1.0, end: 0.0)
    .animate(CurvedAnimation(parent: secondaryAnimation, curve: Curves.linearToEaseOut)),
    axisAlignment: -1.0,
    child: newRoute,
    ),
    );
    },
    transitionDuration: const Duration(milliseconds: 800),
    reverseTransitionDuration: const Duration(milliseconds: 800),
    transitionsBuilder:
    (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return SlideTransition(
    position: Tween<Offset>(begin: const Offset(0.0, 1.0), end: Offset.zero)
    .animate(CurvedAnimation(parent: animation, curve: Curves.easeInOutQuart)),
    child: child,
    );
    },
    );
    }

    @override
    void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
    if (widget.replace) {
    Navigator.of(context).pushReplacement(_createRoute(const MyHomePage(replace: false)));
    }
    });
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    backgroundColor: Colors.blueAccent.shade400,
    body: Center(
    child: TextButton.icon(
    onPressed: () {
    Navigator.of(context).push(_createRoute(const SecondRoute()));
    },
    icon: const Icon(Icons.arrow_forward_rounded),
    label: const Text('Go to 2nd route'),
    ),
    ),
    );
    }
    }