If you want to get a semitransparent blurring layer with a hole with soft edges. Use the below example code to get a semitransparent blurring layer with a hole with soft edges.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | import 'dart:async'; import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override State<StatefulWidget> createState() => MyAppState(); } class MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Coach Mark Demo', home: HomeScreen(), ); } } class HomeScreen extends StatefulWidget { @override _HomeScreenState createState() => _HomeScreenState(); } class _HomeScreenState extends State<HomeScreen> { final GlobalKey<ScaffoldState> _scaffold = GlobalKey(); final GlobalKey<CoachMarkState> _calendarMark = GlobalKey(); @override Widget build(BuildContext context) { return Scaffold( key: _scaffold, appBar: AppBar( title: Text("Hello"), actions: <Widget>[ CoachMark( key: _calendarMark, id: 'calendar_mark', text: 'Tap here to use the Calendar!', child: GestureDetector( onLongPress: () => _calendarMark.currentState.show(), child: IconButton( onPressed: () => print('calendar'), icon: Icon(Icons.calendar_today), ), ), ), PopupMenuButton<String>( itemBuilder: (BuildContext context) { return <PopupMenuEntry<String>>[ PopupMenuItem<String>( value: 'reset', child: Text('Reset'), ), ]; }, onSelected: (String value) { if (value == 'reset') { _calendarMark.currentState.reset(); _scaffold.currentState.showSnackBar(SnackBar( content: Text('Hot-restart the app to see the coach-mark again.'), )); } }, ), ], ), body: Container( decoration: BoxDecoration( image: DecorationImage( image: NetworkImage("http://www.mobileswall.com/wp-content/uploads/2015/03/640-Sunset-Beach-2-l.jpg"), fit: BoxFit.cover), ), ), ); } } class CoachMark extends StatefulWidget { const CoachMark({ Key key, @required this.id, @required this.text, @required this.child, }) : super(key: key); final String id; final String text; final Widget child; @override CoachMarkState createState() => CoachMarkState(); } typedef CoachMarkRect = Rect Function(); class CoachMarkState extends State<CoachMark> { _CoachMarkRoute _route; String get _key => 'mark_${widget.id}'; @override void initState() { super.initState(); test().then((bool seen) { if (seen == false) { show(); } }); } @override void didUpdateWidget(CoachMark oldWidget) { super.didUpdateWidget(oldWidget); _rebuild(); } @override void reassemble() { super.reassemble(); _rebuild(); } @override void dispose() { dismiss(); super.dispose(); } @override Widget build(BuildContext context) { _rebuild(); return widget.child; } void show() { if (_route == null) { _route = _CoachMarkRoute( rect: () { final box = context.findRenderObject() as RenderBox; return box.localToGlobal(Offset.zero) & box.size; }, text: widget.text, padding: EdgeInsets.all(4.0), onPop: () { _route = null; mark(); }, ); Navigator.of(context).push(_route); } } void _rebuild() { if (_route != null) { WidgetsBinding.instance.addPostFrameCallback((_) { _route.changedExternalState(); }); } } void dismiss() { if (_route != null) { _route.dispose(); _route = null; } } Future<bool> test() async { return (await SharedPreferences.getInstance()).getBool(_key) ?? false; } void mark() async { (await SharedPreferences.getInstance()).setBool(_key, true); } void reset() async { (await SharedPreferences.getInstance()).remove(_key); } } class _CoachMarkRoute<T> extends PageRoute<T> { _CoachMarkRoute({ @required this.rect, @required this.text, this.padding, this.onPop, this.shadow = const BoxShadow(color: const Color(0xB2212121), blurRadius: 8.0), this.maintainState = true, this.transitionDuration = const Duration(milliseconds: 450), RouteSettings settings, }) : super(settings: settings); final CoachMarkRect rect; final String text; final EdgeInsets padding; final BoxShadow shadow; final VoidCallback onPop; @override final bool maintainState; @override final Duration transitionDuration; @override bool didPop(T result) { onPop(); return super.didPop(result); } @override Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) { Rect position = rect(); if (padding != null) { position = padding.inflateRect(position); } position = Rect.fromCircle(center: position.center, radius: position.longestSide * 0.5); final clipper = _CoachMarkClipper(position); return Material( type: MaterialType.transparency, child: GestureDetector( behavior: HitTestBehavior.opaque, onTapDown: (d) => Navigator.of(context).pop(), child: IgnorePointer( child: FadeTransition( opacity: animation, child: Stack( children: <Widget>[ ClipPath( clipper: clipper, child: BackdropFilter( filter: ui.ImageFilter.blur(sigmaX: 2.0, sigmaY: 2.0), child: Container( color: Colors.transparent, ), ), ), CustomPaint( child: SizedBox.expand( child: Center( child: Text(text, style: const TextStyle( fontSize: 22.0, fontStyle: FontStyle.italic, color: Colors.white, )), ), ), painter: _CoachMarkPainter( rect: position, shadow: shadow, clipper: clipper, ), ), ], ), ), ), ), ); } @override bool get opaque => false; @override Color get barrierColor => null; @override String get barrierLabel => null; } class _CoachMarkClipper extends CustomClipper<Path> { final Rect rect; _CoachMarkClipper(this.rect); @override Path getClip(Size size) { return Path.combine(PathOperation.difference, Path()..addRect(Offset.zero & size), Path()..addOval(rect)); } @override bool shouldReclip(_CoachMarkClipper old) => rect != old.rect; } class _CoachMarkPainter extends CustomPainter { _CoachMarkPainter({ @required this.rect, @required this.shadow, this.clipper, }); final Rect rect; final BoxShadow shadow; final _CoachMarkClipper clipper; void paint(Canvas canvas, Size size) { final circle = rect.inflate(shadow.spreadRadius); canvas.saveLayer(Offset.zero & size, Paint()); canvas.drawColor(shadow.color, BlendMode.dstATop); canvas.drawCircle(circle.center, circle.longestSide * 0.5, shadow.toPaint()..blendMode = BlendMode.clear); canvas.restore(); } @override bool shouldRepaint(_CoachMarkPainter old) => old.rect != rect; @override bool shouldRebuildSemantics(_CoachMarkPainter oldDelegate) => false; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.