我使用 PageRouteBuilder 做了类似的事情,它是 DialogBuilder 的父级,带有动画参数,你也可以设置暗淡的颜色等。
比如说,在主小部件中有一个按钮:
TextButton(
child: Text("open drawer"),
onPressed: () {
Navigator.of(context).push(_createRoute());
},
)
然后是带有动画等的路线构建器:
Route _createRoute() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => TopDrawer(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, -4.0);
const end = Offset.zero;
const curve = Curves.decelerate;
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
return SlideTransition(
position: tween.animate(animation),
child: child,
);
},
opaque: false,
barrierDismissible: true,
barrierColor: Color.fromRGBO(100, 100, 100, 0.5), //color of the grayed under
transitionDuration: Duration(milliseconds: 450),
reverseTransitionDuration: Duration(milliseconds: 450),
);
}
它显示的抽屉页面:
import 'package:flutter/material.dart';
class TopDrawer extends StatelessWidget {
const TopDrawer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
height: MediaQuery.of(context).size.height/2,
width: MediaQuery.of(context).size.width,
child: Text("some content here"),
color: Colors.green,
),
],
);
}
}