-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to show a floating message (replacement for
SnackBar
)
- Loading branch information
Showing
13 changed files
with
154 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/smooth_app/lib/widgets/smooth_floating_message.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:smooth_app/generic_lib/design_constants.dart'; | ||
|
||
class SmoothFloatingMessage { | ||
SmoothFloatingMessage({ | ||
required this.message, | ||
}); | ||
|
||
final String message; | ||
|
||
OverlayEntry? _entry; | ||
Timer? _autoDismissMessage; | ||
|
||
/// Show the message during [duration]. | ||
/// You can call [hide] if you want to dismiss it before | ||
void show( | ||
BuildContext context, { | ||
AlignmentGeometry? alignment, | ||
Duration? duration, | ||
}) { | ||
_entry?.remove(); | ||
|
||
final double appBarHeight = Scaffold.maybeOf(context)?.hasAppBar == true | ||
? (Scaffold.of(context).appBarMaxHeight ?? kToolbarHeight) | ||
: 0.0; | ||
|
||
_entry = OverlayEntry(builder: (BuildContext context) { | ||
return _SmoothFloatingMessageView( | ||
message: message, | ||
alignment: alignment, | ||
margin: EdgeInsetsDirectional.only( | ||
top: appBarHeight, | ||
start: SMALL_SPACE, | ||
end: SMALL_SPACE, | ||
bottom: SMALL_SPACE, | ||
), | ||
); | ||
}); | ||
|
||
Overlay.of(context).insert(_entry!); | ||
_autoDismissMessage = Timer(duration ?? const Duration(seconds: 5), () { | ||
hide(); | ||
}); | ||
} | ||
|
||
void hide() { | ||
_autoDismissMessage?.cancel(); | ||
_entry?.remove(); | ||
} | ||
} | ||
|
||
class _SmoothFloatingMessageView extends StatefulWidget { | ||
const _SmoothFloatingMessageView({ | ||
required this.message, | ||
this.alignment, | ||
this.margin, | ||
}); | ||
|
||
final String message; | ||
final AlignmentGeometry? alignment; | ||
final EdgeInsetsGeometry? margin; | ||
|
||
@override | ||
State<_SmoothFloatingMessageView> createState() => | ||
_SmoothFloatingMessageViewState(); | ||
} | ||
|
||
class _SmoothFloatingMessageViewState extends State<_SmoothFloatingMessageView> | ||
with SingleTickerProviderStateMixin { | ||
bool initial = true; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
WidgetsBinding.instance.addPostFrameCallback((_) { | ||
setState(() { | ||
initial = false; | ||
}); | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final SnackBarThemeData snackBarTheme = Theme.of(context).snackBarTheme; | ||
|
||
return AnimatedOpacity( | ||
opacity: initial ? 0.0 : 1.0, | ||
duration: const Duration(milliseconds: 200), | ||
child: SafeArea( | ||
top: false, | ||
child: Container( | ||
width: initial ? 0.0 : null, | ||
height: initial ? 0.0 : null, | ||
margin: widget.margin, | ||
alignment: widget.alignment ?? AlignmentDirectional.topCenter, | ||
child: Card( | ||
elevation: 4.0, | ||
shadowColor: Colors.black.withOpacity(0.1), | ||
color: snackBarTheme.backgroundColor, | ||
child: Container( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Text( | ||
widget.message, | ||
textAlign: TextAlign.center, | ||
style: snackBarTheme.contentTextStyle, | ||
), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |