Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [AlertDialog.actionsPadding].

class AlertDialogExample extends StatelessWidget {
const AlertDialogExample({super.key});

@override
Widget build(BuildContext context) {
return
// #region body
AlertDialog(
title: const Text('Title'),
contentPadding: const EdgeInsets.all(8.0),
content: Container(width: 200, height: 200, color: Colors.green),
actions: <Widget>[
ElevatedButton(onPressed: () {}, child: const Text('Button 1')),
ElevatedButton(onPressed: () {}, child: const Text('Button 2')),
],
actionsPadding: const EdgeInsets.symmetric(horizontal: 8.0),
)
// #endregion body
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [AlertDialog].

class AlertDialogExample extends StatefulWidget {
const AlertDialogExample({super.key});

@override
State<AlertDialogExample> createState() => AlertDialogExampleState();
}

class AlertDialogExampleState extends State<AlertDialogExample> {
// #region body
Future<void> _showMyDialog() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: const Text('AlertDialog Title'),
content: const SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('This is a demo alert dialog.'),
Text('Would you like to approve of this message?'),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('Approve'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
// #endregion body

Future<void> showMyDialog() => _showMyDialog();

@override
Widget build(BuildContext context) => const SizedBox.shrink();
}
20 changes: 20 additions & 0 deletions packages/material_ui/example/lib/dialog/dialog.snippet.0.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [Dialog].

class DialogExample extends StatelessWidget {
const DialogExample({super.key});

@override
Widget build(BuildContext context) {
return
// #region body
const Dialog(constraints: BoxConstraints(maxWidth: 560, minHeight: 280))
// #endregion body
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [SimpleDialog].

enum Department { treasury, state }

class SimpleDialogExample {
SimpleDialogExample(this.context);

final BuildContext context;

// #region body
Future<void> _askedToLead() async {
switch (await showDialog<Department>(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('Select assignment'),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, Department.treasury);
},
child: const Text('Treasury department'),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, Department.state);
},
child: const Text('State department'),
),
],
);
},
)) {
case Department.treasury:
// Let's go.
// ...
break;
case Department.state:
// ...
break;
case null:
// dialog dismissed
break;
}
}
Comment thread
dkwingsmt marked this conversation as resolved.
Resolved
// #endregion body

Future<void> askedToLead() => _askedToLead();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [SimpleDialogOption].

enum Department { treasury, state }

class SimpleDialogOptionExample extends StatelessWidget {
const SimpleDialogOptionExample({super.key});

@override
Widget build(BuildContext context) {
return
// #region body
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, Department.treasury);
},
child: const Text('Treasury department'),
)
// #endregion body
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/dialog/alert_dialog.actions_padding.snippet.0.dart'
as example;

void main() {
testWidgets('AlertDialog.actionsPadding aligns actions with content widget', (
WidgetTester tester,
) async {
await tester.pumpWidget(
const MaterialApp(home: Scaffold(body: example.AlertDialogExample())),
);

final Finder dialogMaterial = find
.descendant(
of: find.byType(AlertDialog),
matching: find.byType(Material),
)
.first;
final Finder contentContainer = find.byType(Container);
final Finder button2 = find.widgetWithText(ElevatedButton, 'Button 2');

final double dialogRight = tester.getTopRight(dialogMaterial).dx;
final double contentRight = tester.getTopRight(contentContainer).dx;
final double button2Right = tester.getTopRight(button2).dx;

expect(dialogRight - contentRight, 8.0);
expect(dialogRight - button2Right, 8.0);
expect(button2Right, contentRight);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/dialog/alert_dialog.snippet.0.dart'
as example;

void main() {
testWidgets(
'AlertDialog shows non-barrier-dismissible dialog and completes Future on Approve',
(WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: Scaffold(body: example.AlertDialogExample())),
);

expect(find.byType(AlertDialog), findsNothing);

final example.AlertDialogExampleState state = tester.state(
find.byType(example.AlertDialogExample),
);
bool dialogCompleted = false;
final Future<void> dialogFuture = state.showMyDialog().then((_) {
dialogCompleted = true;
});
await tester.pumpAndSettle();

expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('AlertDialog Title'), findsOneWidget);
expect(find.text('This is a demo alert dialog.'), findsOneWidget);
expect(
find.text('Would you like to approve of this message?'),
findsOneWidget,
);
expect(dialogCompleted, isFalse);

// Tapping outside the dialog does not dismiss it or complete the Future.
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(dialogCompleted, isFalse);

// Tapping Approve dismisses the dialog and completes the returned Future.
await tester.tap(find.text('Approve'));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(dialogCompleted, isTrue);
await dialogFuture;
},
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/dialog/dialog.snippet.0.dart' as example;

void main() {
testWidgets('Dialog applies M3 BoxConstraints', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: Scaffold(body: example.DialogExample())),
);

final Finder dialogMaterial = find.descendant(
of: find.byType(Dialog),
matching: find.byType(Material),
);
expect(tester.getSize(dialogMaterial), const Size(0, 280));

final RenderConstrainedBox constrainedBox = tester.renderObject(
find.descendant(
of: find.byType(Dialog),
matching: find.byWidgetPredicate(
(Widget widget) =>
widget is ConstrainedBox &&
widget.constraints ==
const BoxConstraints(maxWidth: 560, minHeight: 280),
),
),
);
expect(
constrainedBox.additionalConstraints,
const BoxConstraints(maxWidth: 560, minHeight: 280),
);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/dialog/simple_dialog.snippet.0.dart'
as example;

class _DialogObserver extends NavigatorObserver {
Future<dynamic>? lastPopped;

@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
lastPopped = route.popped;
}
}

void main() {
testWidgets(
'SimpleDialog returns selected Department or null when dismissed',
(WidgetTester tester) async {
final _DialogObserver observer = _DialogObserver();
late BuildContext savedContext;

await tester.pumpWidget(
MaterialApp(
navigatorObservers: <NavigatorObserver>[observer],
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
savedContext = context;
return const SizedBox.shrink();
},
),
),
),
);

final example.SimpleDialogExample dialogExample =
example.SimpleDialogExample(savedContext);

// Select Treasury department.
Future<void> future = dialogExample.askedToLead();
await tester.pumpAndSettle();
expect(find.text('Select assignment'), findsOneWidget);
Future<dynamic> popped = observer.lastPopped!;
await tester.tap(find.text('Treasury department'));
await tester.pumpAndSettle();
expect(await popped, example.Department.treasury);
await future;

// Select State department.
future = dialogExample.askedToLead();
await tester.pumpAndSettle();
popped = observer.lastPopped!;
await tester.tap(find.text('State department'));
await tester.pumpAndSettle();
expect(await popped, example.Department.state);
await future;

// Dismiss dialog by tapping the barrier.
future = dialogExample.askedToLead();
await tester.pumpAndSettle();
popped = observer.lastPopped!;
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
expect(await popped, isNull);
await future;
},
);
}
Loading
Loading