Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent PageRoute in Flutter for displaying a (semi-) transparent page

Would it be possible to have a page route with a transparent background so I can show a (semi-)transparent page on top of an existing page?

semi-transparent screen transition

like image 934
Frederik Schweiger Avatar asked Feb 07 '19 11:02

Frederik Schweiger


People also ask

How do you make a widget transparent in flutter?

You need to wrap widget with Opacity() widget and set opacity paramater with transparent value.

How do you make a transparent container with color in flutter?

Flutter uses a 32 bit color value in ARGB format, where A = Alpha, R = RED, G = GREEN and B = BLUE. So to control the opacity you can change the values of first two digits of the hex value in const Color(0xFF0E3311) , you can use values ranging from 0x000E3311 , 0x010E3311 .... 0xFF0E3311 . Hope that helps!


2 Answers

You also don't need to override any class, simply use:

Navigator.of(context).push(   PageRouteBuilder(     opaque: false, // set to false     pageBuilder: (_, __, ___) => Page2(),   ), ); 
like image 174
CopsOnRoad Avatar answered Oct 20 '22 03:10

CopsOnRoad


Yes, definitely! One solution would be to extend PageRoute and make the opaque getter return false. A possible solution could look like the following:

import 'package:flutter/widgets.dart';  class TransparentRoute extends PageRoute<void> {   TransparentRoute({     @required this.builder,     RouteSettings settings,   })  : assert(builder != null),         super(settings: settings, fullscreenDialog: false);    final WidgetBuilder builder;    @override   bool get opaque => false;    @override   Color get barrierColor => null;    @override   String get barrierLabel => null;    @override   bool get maintainState => true;    @override   Duration get transitionDuration => Duration(milliseconds: 350);    @override   Widget buildPage(BuildContext context, Animation<double> animation,       Animation<double> secondaryAnimation) {     final result = builder(context);     return FadeTransition(       opacity: Tween<double>(begin: 0, end: 1).animate(animation),       child: Semantics(         scopesRoute: true,         explicitChildNodes: true,         child: result,       ),     );   } } 

Keep in mind that this would also create a custom transition animation and behave differently than the more complex MaterialPageRoute (e.g. the swipe-back gesture would not work with the current implementation on iOS).

You could use it like this:

Navigator.of(context).push(     TransparentRoute(builder: (BuildContext context) => YourSecondPage()) ); 

For more info on the PageRoute and the different implementers, head over to https://docs.flutter.io/flutter/widgets/PageRoute-class.html

like image 23
Frederik Schweiger Avatar answered Oct 20 '22 04:10

Frederik Schweiger