transparent_app_bar_widget.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'package:flutter/material.dart';
  2. class TransparentAppBarWidget extends StatefulWidget {
  3. final Widget? body;
  4. final Widget? title;
  5. final List<Widget>? actions;
  6. final Color backButtonColor;
  7. const TransparentAppBarWidget({super.key, required this.body, required this.title, required this.actions, required this.backButtonColor});
  8. @override
  9. State<StatefulWidget> createState() {
  10. return _TransparentAppBarState();
  11. }
  12. }
  13. class _TransparentAppBarState extends State<TransparentAppBarWidget> {
  14. @override
  15. Widget build(BuildContext context) {
  16. var padding = MediaQuery.of(context).padding;
  17. return Stack(
  18. children: stackChildren(padding),
  19. );
  20. }
  21. List<Widget> stackChildren(EdgeInsets padding) {
  22. if (widget.body!=null) {
  23. return <Widget>[
  24. widget.body!,
  25. Padding(
  26. padding: padding,
  27. child: SizedBox(
  28. height: kToolbarHeight,
  29. child: Row(
  30. children: appBarChildren(),
  31. ),
  32. ),
  33. )
  34. ];
  35. }else {
  36. return <Widget>[
  37. Padding(
  38. padding: padding,
  39. child: SizedBox(
  40. height: kToolbarHeight,
  41. child: Row(
  42. children: appBarChildren(),
  43. ),
  44. ),
  45. )
  46. ];
  47. }
  48. }
  49. List<Widget> appBarChildren() {
  50. if (widget.actions != null && widget.actions!.isNotEmpty) {
  51. return <Widget>[
  52. Padding(
  53. padding: const EdgeInsets.fromLTRB(5, 3, 5, 3),
  54. child: backAndTitle(),
  55. ),
  56. Expanded(
  57. flex: 1,
  58. child: Row(
  59. mainAxisAlignment: MainAxisAlignment.end,
  60. children: widget.actions!,
  61. ),
  62. )
  63. ];
  64. }else {
  65. return <Widget>[
  66. Padding(
  67. padding: const EdgeInsets.fromLTRB(5, 3, 5, 3),
  68. child: backAndTitle(),
  69. )
  70. ];
  71. }
  72. }
  73. Widget backAndTitle() {
  74. if (widget.title!= null) {
  75. return Row(
  76. mainAxisSize: MainAxisSize.min,
  77. children: <Widget>[
  78. BackButton(
  79. color: widget.backButtonColor,
  80. ),
  81. widget.title!
  82. ],
  83. );
  84. }else {
  85. return BackButton(
  86. color: widget.backButtonColor,
  87. );
  88. }
  89. }
  90. }