Commit aef4032
derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-03-08 15:04:39
make CustomBanner material banner and test it
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
Changed files (2)
lib
components
test
ui
components
lib/components/custom_banner.dart
@@ -15,14 +15,11 @@ import 'package:flutter/material.dart';
/// )
/// )
/// ```
-class CustomBanner extends StatelessWidget {
+class CustomBanner extends MaterialBanner {
/// Create a banner that displays information and an action.
- const CustomBanner({super.key, required this.content, this.action});
-
- /// The main content of the banner.
- ///
- /// Gets displayed on the left side.
- final Widget content;
+ CustomBanner({super.key, required super.content, this.action}) : super(
+ actions: [const SizedBox.shrink()],
+ );
/// Primary action of the banner.
///
@@ -31,6 +28,17 @@ class CustomBanner extends StatelessWidget {
/// When this is larger than the screen width, overflow occurs.
final Widget? action;
+
+ @override
+ MaterialBanner withAnimation(Animation<double> newAnimation, {Key? fallbackKey})
+ => CustomBanner(content: content, action: action, key: key ?? fallbackKey,);
+ // TODO: animate
+
+ @override
+ State<CustomBanner> createState() => _CustomBannerState();
+}
+
+class _CustomBannerState extends State<CustomBanner> {
@override
Widget build(BuildContext context) => Container(
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 4),
@@ -41,9 +49,9 @@ class CustomBanner extends StatelessWidget {
),
child: Row(
children: [
- Expanded(child: content),
- if (action != null)
- action!,
+ Expanded(child: widget.content),
+ if (widget.action != null)
+ widget.action!,
],
),
);
test/ui/components/custom_banner_test.dart
@@ -0,0 +1,63 @@
+
+
+import 'package:blood_pressure_app/components/custom_banner.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+import 'util.dart';
+
+void main() {
+ testWidgets('should show information and allow interaction', (widgetTester) async {
+ int callCount = 0;
+ await widgetTester.pumpWidget(materialApp(CustomBanner(
+ content: const Text('custom banner text'),
+ action: IconButton(
+ icon: const Icon(Icons.add_circle_outline),
+ onPressed: () {
+ callCount++;
+ },
+ ),
+ ),),);
+ expect(find.text('custom banner text'), findsOneWidget);
+ expect(find.byIcon(Icons.add_circle_outline), findsOneWidget);
+
+ expect(callCount, 0);
+ await widgetTester.tap(find.byIcon(Icons.add_circle_outline));
+ await widgetTester.pumpAndSettle();
+ expect(callCount, 1);
+ });
+ testWidgets('should work after launched as MaterialBanner', (widgetTester) async {
+ int callCount = 0;
+ await widgetTester.pumpWidget(materialApp(Builder(
+ builder: (context) => IconButton(
+ icon: const Icon(Icons.start),
+ onPressed: () {
+ ScaffoldMessenger.of(context).showMaterialBanner(
+ CustomBanner(
+ content: const Text('custom banner text'),
+ action: IconButton(
+ icon: const Icon(Icons.add_circle_outline),
+ onPressed: () {
+ callCount++;
+ },
+ ),
+ ),
+ );
+ },
+ ),
+ ),),);
+ expect(find.byType(CustomBanner), findsNothing);
+ expect(find.byIcon(Icons.start), findsOneWidget);
+ await widgetTester.tap(find.byIcon(Icons.start));
+ await widgetTester.pumpAndSettle();
+ expect(find.byType(CustomBanner), findsOneWidget);
+
+ expect(find.text('custom banner text'), findsOneWidget);
+ expect(find.byIcon(Icons.add_circle_outline), findsOneWidget);
+
+ expect(callCount, 0);
+ await widgetTester.tap(find.byIcon(Icons.add_circle_outline));
+ await widgetTester.pumpAndSettle();
+ expect(callCount, 1);
+ });
+}