|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/widgets.dart'; |
| 3 | +import 'package:getflutter/getflutter.dart'; |
| 4 | +import 'package:getflutter/types/gf_alert_type.dart'; |
| 5 | + |
| 6 | +class GFAlert extends StatefulWidget { |
| 7 | + |
| 8 | + /// Alert has to be wrap inside the body like [GFFloatingWidget]. See [GFFloatingWidget] |
| 9 | + GFAlert( |
| 10 | + {Key key, |
| 11 | + this.child, |
| 12 | + this.backgroundColor, |
| 13 | + this.content, |
| 14 | + this.width, |
| 15 | + this.type = GFAlertType.basic, |
| 16 | + this.alignment, |
| 17 | + this.contentChild, |
| 18 | + this.title, |
| 19 | + this.bottombar, |
| 20 | + this.animationDuration = const Duration(milliseconds: 300), |
| 21 | + this.textStyle = const TextStyle(color: Colors.black87), |
| 22 | + this.titleTextStyle = const TextStyle( |
| 23 | + color: Colors.black87, fontSize: 17, fontWeight: FontWeight.w500)}) |
| 24 | + : super(key: key); |
| 25 | + |
| 26 | + /// child of type [Widget]is alternative to text key. text will get priority over child |
| 27 | + final Widget child; |
| 28 | + |
| 29 | + /// title of type [String] used to descripe the title of the [GFAlert] |
| 30 | + final String title; |
| 31 | + |
| 32 | + /// child of type [Widget]is alternative to title key. title will get priority over contentchild |
| 33 | + final Widget contentChild; |
| 34 | + |
| 35 | + /// title of type [String] used to describe the content of the [GFAlert] |
| 36 | + final String content; |
| 37 | + |
| 38 | + final TextStyle titleTextStyle; |
| 39 | + |
| 40 | + ///pass color of type [Color] or [GFColor] for background of [GFAlert] |
| 41 | + final dynamic backgroundColor; |
| 42 | + |
| 43 | + /// textStyle of type [textStyle] will be applicable to text only and not for the child |
| 44 | + final TextStyle textStyle; |
| 45 | + |
| 46 | + /// width of type [double] used to control the width of the [GFAlert] |
| 47 | + final double width; |
| 48 | + |
| 49 | + ///type of [GFAlertType] which takes the type ie, basic, rounded and fullWidth for the [GFAlert] |
| 50 | + final GFAlertType type; |
| 51 | + |
| 52 | + ///type of [Duration] which takes the duration of the fade in animation |
| 53 | + final Duration animationDuration; |
| 54 | + |
| 55 | + /// type of [Alignment] used to align the text inside the toast |
| 56 | + final Alignment alignment; |
| 57 | + |
| 58 | + ///type of [Widget] used for the buttons ie, OK, Cancel for the action in [GFAlert] |
| 59 | + final Widget bottombar; |
| 60 | + @override |
| 61 | + _GFAlertState createState() => _GFAlertState(); |
| 62 | +} |
| 63 | + |
| 64 | +class _GFAlertState extends State<GFAlert> with TickerProviderStateMixin { |
| 65 | + AnimationController animationController; |
| 66 | + Animation<double> animation; |
| 67 | + |
| 68 | + @override |
| 69 | + void initState() { |
| 70 | + animationController = AnimationController( |
| 71 | + duration: const Duration(milliseconds: 300), vsync: this); |
| 72 | + animation = CurvedAnimation( |
| 73 | + parent: animationController, curve: Curves.fastOutSlowIn); |
| 74 | + |
| 75 | + animationController.forward(); |
| 76 | + super.initState(); |
| 77 | + } |
| 78 | + |
| 79 | + @override |
| 80 | + void dispose() { |
| 81 | + animationController.dispose(); |
| 82 | + super.dispose(); |
| 83 | + } |
| 84 | + |
| 85 | + @override |
| 86 | + Widget build(BuildContext context) { |
| 87 | + return Stack( |
| 88 | + children: <Widget>[ |
| 89 | + Container( |
| 90 | + height: MediaQuery.of(context).size.height, |
| 91 | + ), |
| 92 | + FadeTransition( |
| 93 | + opacity: animation, |
| 94 | + child: Column( |
| 95 | + children: <Widget>[ |
| 96 | + Container( |
| 97 | + width: widget.type == GFAlertType.fullWidth |
| 98 | + ? MediaQuery.of(context).size.width |
| 99 | + : widget.width, |
| 100 | + constraints: BoxConstraints(minHeight: 50.0), |
| 101 | + margin: widget.type == GFAlertType.fullWidth |
| 102 | + ? EdgeInsets.only(left: 0, right: 0) |
| 103 | + : EdgeInsets.only(left: 20, right: 20), |
| 104 | + padding: EdgeInsets.all(15), |
| 105 | + decoration: BoxDecoration( |
| 106 | + borderRadius: widget.type == GFAlertType.basic |
| 107 | + ? BorderRadius.circular(3.0) |
| 108 | + : widget.type == GFAlertType.rounded |
| 109 | + ? BorderRadius.circular(10.0) |
| 110 | + : BorderRadius.zero, |
| 111 | + color: widget.backgroundColor != null |
| 112 | + ? GFColors.getGFColor(widget.backgroundColor) |
| 113 | + : GFColors.getGFColor(GFColor.white), |
| 114 | + boxShadow: [ |
| 115 | + BoxShadow( |
| 116 | + color: Colors.black.withOpacity(0.40), |
| 117 | + blurRadius: 3.0) |
| 118 | + ]), |
| 119 | + child: Column( |
| 120 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 121 | + children: <Widget>[ |
| 122 | + widget.title != null |
| 123 | + ? Text(widget.title, style: widget.titleTextStyle) |
| 124 | + : (widget.child ?? Container()), |
| 125 | + SizedBox( |
| 126 | + height: 10, |
| 127 | + ), |
| 128 | + Align( |
| 129 | + alignment: widget.alignment != null |
| 130 | + ? widget.alignment |
| 131 | + : Alignment.topLeft, |
| 132 | + child: widget.content != null |
| 133 | + ? Text(widget.content, style: widget.textStyle) |
| 134 | + : (widget.contentChild ?? Container()), |
| 135 | + ), |
| 136 | + SizedBox( |
| 137 | + height: 10, |
| 138 | + ), |
| 139 | + widget.bottombar != null ? widget.bottombar : Container(), |
| 140 | + ], |
| 141 | + ), |
| 142 | + ), |
| 143 | + ], |
| 144 | + ), |
| 145 | + ), |
| 146 | + ], |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
0 commit comments