Eliminated DrawerItem, use ListTile instead (#8992)

This commit is contained in:
Hans Muller
2017-03-24 15:34:43 -07:00
committed by GitHub
parent 51ea62c143
commit 606f56221b
21 changed files with 774 additions and 653 deletions

View File

@@ -34,7 +34,6 @@ export 'src/material/dialog.dart';
export 'src/material/divider.dart';
export 'src/material/drawer.dart';
export 'src/material/drawer_header.dart';
export 'src/material/drawer_item.dart';
export 'src/material/dropdown.dart';
export 'src/material/expand_icon.dart';
export 'src/material/expansion_panel.dart';

View File

@@ -257,8 +257,8 @@ class LicenseEntryWithLineBreaks extends LicenseEntry {
/// * [showAboutDialog], which shows a Material-style dialog with information
/// about the application, including a button that shows a [LicensePage] that
/// uses this API to select licenses to show.
/// * [AboutDrawerItem], which is a widget to put in a [Drawer] which
/// automatically calls [showAboutDialog].
/// * [AboutListTile], which is a widget that can be added to a [Drawer]. When
/// tapped it calls [showAboutDialog].
class LicenseRegistry {
LicenseRegistry._();

View File

@@ -11,21 +11,21 @@ import 'package:flutter/foundation.dart';
import 'app_bar.dart';
import 'debug.dart';
import 'dialog.dart';
import 'drawer_item.dart';
import 'flat_button.dart';
import 'icon.dart';
import 'icon_theme.dart';
import 'icon_theme_data.dart';
import 'list_tile.dart';
import 'page.dart';
import 'progress_indicator.dart';
import 'scaffold.dart';
import 'scrollbar.dart';
import 'theme.dart';
/// A [DrawerItem] to show an about box.
/// A [ListTile] that shows an about box.
///
/// Place this in a [Drawer], specifying your preferred application name,
/// version, icon, and copyright in the appropriate fields.
/// This widget is often added to an app's [Drawer]. When tapped it shows
/// an about box dialog with [showAboutDialog].
///
/// The about box will include a button that shows licenses for software used by
/// the application. The licenses shown are those returned by the
@@ -33,13 +33,13 @@ import 'theme.dart';
///
/// If your application does not have a [Drawer], you should provide an
/// affordance to call [showAboutDialog] or (at least) [showLicensePage].
class AboutDrawerItem extends StatelessWidget {
/// Creates a drawer item for showing an about box.
class AboutListTile extends StatelessWidget {
/// Creates a list tile for showing an about box.
///
/// The arguments are all optional. The application name, if omitted, will be
/// derived from the nearest [Title] widget. The version, icon, and legalese
/// values default to the empty string.
AboutDrawerItem({
AboutListTile({
Key key,
this.icon: const Icon(null),
this.child,
@@ -109,10 +109,10 @@ class AboutDrawerItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
return new DrawerItem(
icon: icon,
child: child ?? new Text('About ${applicationName ?? _defaultApplicationName(context)}'),
onPressed: () {
return new ListTile(
leading: icon,
title: child ?? new Text('About ${applicationName ?? _defaultApplicationName(context)}'),
onTap: () {
showAboutDialog(
context: context,
applicationName: applicationName,
@@ -131,7 +131,7 @@ class AboutDrawerItem extends StatelessWidget {
///
/// The arguments correspond to the properties on [AboutDialog].
///
/// If the application has a [Drawer], consider using [AboutDrawerItem] instead
/// If the application has a [Drawer], consider using [AboutListTile] instead
/// of calling this directly.
///
/// If you do not need an about box in your application, you should at least
@@ -164,7 +164,7 @@ void showAboutDialog({
///
/// The arguments correspond to the properties on [LicensePage].
///
/// If the application has a [Drawer], consider using [AboutDrawerItem] instead
/// If the application has a [Drawer], consider using [AboutListTile] instead
/// of calling this directly.
///
/// The [AboutDialog] shown by [showAboutDialog] includes a button that calls
@@ -196,7 +196,7 @@ void showLicensePage({
///
/// To show an [AboutDialog], use [showAboutDialog].
///
/// If the application has a [Drawer], the [AboutDrawerItem] widget can make the
/// If the application has a [Drawer], the [AboutListTile] widget can make the
/// process of showing an about dialog simpler.
///
/// The [AboutDialog] shown by [showAboutDialog] includes a button that calls
@@ -316,7 +316,7 @@ class AboutDialog extends StatelessWidget {
///
/// To show a [LicensePage], use [showLicensePage].
///
/// The [AboutDialog] shown by [showAboutDialog] and [AboutDrawerItem] includes
/// The [AboutDialog] shown by [showAboutDialog] and [AboutListTile] includes
/// a button that calls [showLicensePage].
///
/// The licenses shown on the [LicensePage] are those returned by the

View File

@@ -92,7 +92,7 @@ class ButtonTheme extends InheritedWidget {
/// Defaults to 16.0 pixels of horizontal padding.
final EdgeInsets padding;
/// The color from the closest instance of this class that encloses the given context.
/// The closest instance of this class that encloses the given context.
///
/// Typical usage is as follows:
///

View File

@@ -5,6 +5,7 @@
import 'package:flutter/widgets.dart';
import 'colors.dart';
import 'list_tile.dart';
import 'material.dart';
// TODO(eseidel): Draw width should vary based on device size:
@@ -30,7 +31,23 @@ const Duration _kBaseSettleDuration = const Duration(milliseconds: 246);
///
/// Drawers are typically used with the [Scaffold.drawer] property. The child of
/// the drawer is usually a [ListView] whose first child is a [DrawerHeader]
/// that displays status information about the current user.
/// that displays status information about the current user. The remaining
/// drawer children are often constructed with [ListTile]s, often concluding
/// with an [AboutListTile].
///
/// An open drawer can be closed by calling [Navigator.pop]. For example
/// a drawer item might close the drawer when tapped:
///
/// ```dart
/// new ListTile(
/// leading: new Icon(Icons.change_history),
/// title: new Text('Change history'),
/// onTap: () {
/// // change app state...
/// Navigator.pop(context); // close the drawer
/// },
/// );
/// ```
///
/// The [AppBar] automatically displays an appropriate [IconButton] to show the
/// [Drawer] when a [Drawer] is available in the [Scaffold]. The [Scaffold]
@@ -43,9 +60,6 @@ const Duration _kBaseSettleDuration = const Duration(milliseconds: 246);
/// * [Scaffold.of], to obtain the current [ScaffoldState], which manages the
/// display and animation of the drawer.
/// * [ScaffoldState.openDrawer], which displays its [Drawer], if any.
/// * [Navigator.pop], which closes the drawer if it is open.
/// * [DrawerItem], a widget for items in drawers.
/// * [DrawerHeader], a widget for the top part of a drawer.
/// * <https://material.google.com/patterns/navigation-drawer.html>
class Drawer extends StatelessWidget {
/// Creates a material design drawer.
@@ -233,8 +247,7 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
final ColorTween _color = new ColorTween(begin: Colors.transparent, end: Colors.black54);
final GlobalKey _gestureDetectorKey = new GlobalKey();
@override
Widget build(BuildContext context) {
Widget _buildDrawer(BuildContext context) {
if (_controller.status == AnimationStatus.dismissed) {
return new Align(
alignment: FractionalOffset.centerLeft,
@@ -245,7 +258,7 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
behavior: HitTestBehavior.translucent,
excludeFromSemantics: true,
child: new Container(width: _kEdgeDragWidth)
)
),
);
} else {
return new GestureDetector(
@@ -263,8 +276,8 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
decoration: new BoxDecoration(
backgroundColor: _color.evaluate(_controller)
),
child: new Container()
)
child: new Container(),
),
),
new Align(
alignment: FractionalOffset.centerLeft,
@@ -275,14 +288,21 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
child: new Focus(
key: _drawerKey,
child: config.child
)
)
)
)
]
)
)
),
),
),
),
],
),
),
);
}
}
@override
Widget build(BuildContext context) {
return new ListTileTheme(
style: ListTileStyle.drawer,
child: _buildDrawer(context),
);
}
}

View File

@@ -17,14 +17,12 @@ const double _kDrawerHeaderHeight = 160.0 + 1.0; // bottom edge
/// Part of the material design [Drawer].
///
/// Requires one of its ancestors to be a [Material] widget. This condition is
/// satisfied by putting the [DrawerItem] in a [Drawer].
/// satisfied by putting the [DrawerHeader] in a [Drawer].
///
/// See also:
///
/// * [Drawer]
/// * [UserAccountsDrawerHeader], a variant of [DrawerHeader] that is
/// specialized for showing user accounts.
/// * [DrawerItem]
/// * <https://material.google.com/patterns/navigation-drawer.html>
class DrawerHeader extends StatelessWidget {
/// Creates a material design drawer header.

View File

@@ -1,146 +0,0 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'colors.dart';
import 'constants.dart';
import 'debug.dart';
import 'icon.dart';
import 'icon_theme.dart';
import 'icon_theme_data.dart';
import 'image_icon.dart';
import 'ink_well.dart';
import 'theme.dart';
/// An item in a material design drawer.
///
/// Part of the material design [Drawer].
///
/// Requires one of its ancestors to be a [Material] widget. This condition is
/// satisfied by putting the [DrawerItem] in a [Drawer].
///
/// See also:
///
/// * [Drawer]
/// * [DrawerHeader]
/// * <https://material.google.com/patterns/navigation-drawer.html>
class DrawerItem extends StatelessWidget {
/// Creates a material design drawer item.
///
/// Requires one of its ancestors to be a [Material] widget.
const DrawerItem({
Key key,
this.icon: const Icon(null),
@required this.child,
this.onPressed,
this.selected: false
}) : super(key: key);
/// The icon to display before the child widget.
///
/// The size and color of the icon is configured automatically using an
/// [IconTheme] and therefore do not need to be explicitly given in the
/// icon widget.
///
/// See [Icon], [ImageIcon].
final Widget icon;
/// The widget below this widget in the tree.
final Widget child;
/// Called when the user taps this drawer item.
///
/// If null, the drawer item is displayed as disabled.
///
/// To close the [Drawer] when an item is pressed, call [Navigator.pop].
final VoidCallback onPressed;
/// Whether this drawer item is currently selected.
///
/// The currently selected item is highlighted to distinguish it from other
/// drawer items.
final bool selected;
Color _getIconColor(ThemeData themeData) {
switch (themeData.brightness) {
case Brightness.light:
if (selected)
return themeData.primaryColor;
if (onPressed == null)
return Colors.black26;
return Colors.black45;
case Brightness.dark:
if (selected)
return themeData.accentColor;
if (onPressed == null)
return Colors.white30;
return null; // use default icon theme color unmodified
}
assert(themeData.brightness != null);
return null;
}
TextStyle _getTextStyle(ThemeData themeData) {
final TextStyle result = themeData.textTheme.body2;
if (selected) {
switch (themeData.brightness) {
case Brightness.light:
return result.copyWith(color: themeData.primaryColor);
case Brightness.dark:
return result.copyWith(color: themeData.accentColor);
}
}
return result;
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
final ThemeData themeData = Theme.of(context);
final List<Widget> children = <Widget>[];
if (icon != null) {
children.add(
new Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: new IconTheme.merge(
context: context,
data: new IconThemeData(
color: _getIconColor(themeData),
size: 24.0
),
child: icon
)
)
);
}
if (child != null) {
children.add(
new Expanded(
child: new Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: new AnimatedDefaultTextStyle(
style: _getTextStyle(themeData),
duration: kThemeChangeDuration,
child: child
)
)
)
);
}
return new MergeSemantics(
child: new Container(
height: 48.0,
child: new InkWell(
onTap: onPressed,
child: new Row(children: children)
)
)
);
}
}

View File

@@ -5,8 +5,11 @@
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'colors.dart';
import 'constants.dart';
import 'debug.dart';
import 'icon_theme.dart';
import 'icon_theme_data.dart';
import 'ink_well.dart';
import 'theme.dart';
@@ -28,7 +31,20 @@ enum MaterialListType {
twoLine,
/// A list tile that contains three lines of text.
threeLine
threeLine,
}
/// Defines the title font used for [ListTile] descendants of a [ListTileTheme].
///
/// List tiles that appear in a [Drawer] use the theme's [TextTheme.body2]
/// text style, which is a little smaller than the theme's [TextTheme.subhead]
/// text style, which is used by default.
enum ListTileStyle {
// Use a title font that's appropriate for a [ListTile] in a list.
list,
// Use a title font that's appropriate for a [ListTile] that appears in a [Drawer].
drawer,
}
/// The vertical extent of the different types of material list tiles.
@@ -46,6 +62,64 @@ Map<MaterialListType, double> kListTileExtent = const <MaterialListType, double>
MaterialListType.threeLine: 88.0,
};
/// An inherited widget that defines color and style parameters for [ListTile]s
/// in this widget's subtree.
///
/// Values specified here are used for [ListTile] properties that are not given
/// an explicit non-null value.
///
/// The [Drawer] widget specifies a tile theme for its children which sets
/// [style] to [ListTileStyle.drawer].
class ListTileTheme extends InheritedWidget {
/// Creates an inherited widget that defines color and style parameters for [ListTile]s.
const ListTileTheme({
Key key,
this.dense: false,
this.style: ListTileStyle.list,
this.selectedColor,
this.iconColor,
this.textColor,
Widget child,
}) : super(key: key, child: child);
/// If true then [ListTile]s will have the vertically dense layout.
final bool dense;
/// If specified, [style] defines the font used for [ListTile] titles.
final ListTileStyle style;
/// If specified, the color used for icons and text when a [ListTile] is selected.
final Color selectedColor;
/// If specified, the icon color used for enabled [ListTile]s that are not selected.
final Color iconColor;
/// If specified, the text color used for enabled [ListTile]s that are not selected.
final Color textColor;
/// The closest instance of this class that encloses the given context.
///
/// Typical usage is as follows:
///
/// ```dart
/// ListTileTheme theme = ListTileTheme.of(context);
/// ```
static ListTileTheme of(BuildContext context) {
final ListTileTheme result = context.inheritFromWidgetOfExactType(ListTileTheme);
return result ?? new ListTileTheme();
}
@override
bool updateShouldNotify(ListTileTheme oldTheme) {
return dense != oldTheme.dense
|| style != oldTheme.style
|| selectedColor != oldTheme.selectedColor
|| iconColor != oldTheme.iconColor
|| textColor != oldTheme.textColor;
}
}
/// A single fixed-height row that typically contains some text as well as
/// a leading or trailing icon.
///
@@ -69,14 +143,15 @@ Map<MaterialListType, double> kListTileExtent = const <MaterialListType, double>
///
/// See also:
///
/// * [kListTileExtent], which defines the ListTile sizes.
/// * [ListTileTheme], which defines visual properties for [ListTile]s.
/// * [ListView], which can display an arbitrary number of [ListTile]s
/// in a scrolling list.
/// * [Card], which can be used with [Column] to show a few [ListTile]s.
/// * [CircleAvatar], which shows an icon representing a person and is often
/// used as the [leading] element of a ListTile.
/// * [Card], which can be used with [Column] to show a few [ListTile]s.
/// * [Divider], which can be used to separate [ListTile]s.
/// * [ListTile.divideTiles], a utility for inserting [Divider]s in between [ListTile]s.
/// * [kListTileExtent], which defines the ListTile sizes.
/// * <https://material.google.com/components/lists.html>
class ListTile extends StatelessWidget {
/// Creates a list tile.
@@ -91,15 +166,20 @@ class ListTile extends StatelessWidget {
this.subtitle,
this.trailing,
this.isThreeLine: false,
this.dense: false,
this.dense,
this.enabled: true,
this.onTap,
this.onLongPress
}) : super(key: key);
this.onLongPress,
this.selected: false,
}) : super(key: key) {
assert(isThreeLine != null);
assert(enabled != null);
assert(selected != null);
}
/// A widget to display before the title.
///
/// Typically a [CircleAvatar] widget.
/// Typically an [Icon] or a [CircleAvatar] widget.
final Widget leading;
/// The primary content of the list tile.
@@ -124,6 +204,8 @@ class ListTile extends StatelessWidget {
final bool isThreeLine;
/// Whether this list tile is part of a vertically dense list.
///
/// If this property is null then its value is based on [ListTileTheme.dense].
final bool dense;
/// Whether this list tile is interactive.
@@ -143,6 +225,12 @@ class ListTile extends StatelessWidget {
/// Inoperative if [enabled] is false.
final GestureLongPressCallback onLongPress;
/// If this tile is also [enabled] then icons and text are rendered with the same color.
///
/// By default the selected color is the theme's primary color. The selected color
/// can be overridden with a [ListTileTheme].
final bool selected;
/// Add a one pixel border in between each tile. If color isn't specified the
/// [ThemeData.dividerColor] of the context's [Theme] is used.
///
@@ -174,50 +262,103 @@ class ListTile extends StatelessWidget {
yield tile;
}
TextStyle _primaryTextStyle(BuildContext context) {
final ThemeData theme = Theme.of(context);
final TextStyle style = theme.textTheme.subhead;
if (!enabled) {
final Color color = theme.disabledColor;
return dense ? style.copyWith(fontSize: 13.0, color: color) : style.copyWith(color: color);
Color _iconColor(ThemeData theme, ListTileTheme tileTheme) {
if (!enabled)
return theme.disabledColor;
if (selected && tileTheme?.selectedColor != null)
return tileTheme.selectedColor;
if (!selected && tileTheme?.iconColor != null)
return tileTheme.iconColor;
switch (theme.brightness) {
case Brightness.light:
return selected ? theme.primaryColor : Colors.black45;
case Brightness.dark:
return selected ? theme.accentColor : null; // null - use current icon theme color
}
return dense ? style.copyWith(fontSize: 13.0) : style;
assert(theme.brightness != null);
return null;
}
TextStyle _secondaryTextStyle(BuildContext context) {
final ThemeData theme = Theme.of(context);
final Color color = theme.textTheme.caption.color;
Color _textColor(ThemeData theme, ListTileTheme tileTheme, Color defaultColor) {
if (!enabled)
return theme.disabledColor;
if (selected && tileTheme?.selectedColor != null)
return tileTheme.selectedColor;
if (!selected && tileTheme?.textColor != null)
return tileTheme.textColor;
if (selected) {
switch (theme.brightness) {
case Brightness.light:
return theme.primaryColor;
case Brightness.dark:
return theme.accentColor;
}
}
return defaultColor;
}
bool _denseLayout(ListTileTheme tileTheme) {
return dense != null ? dense : (tileTheme?.dense ?? false);
}
TextStyle _titleTextStyle(ThemeData theme, ListTileTheme tileTheme) {
final TextStyle style = tileTheme?.style == ListTileStyle.drawer
? theme.textTheme.body2
: theme.textTheme.subhead;
final Color color = _textColor(theme, tileTheme, style.color);
return _denseLayout(tileTheme)
? style.copyWith(fontSize: 13.0, color: color)
: style.copyWith(color: color);
}
TextStyle _subtitleTextStyle(ThemeData theme, ListTileTheme tileTheme) {
final TextStyle style = theme.textTheme.body1;
return dense ? style.copyWith(color: color, fontSize: 12.0) : style.copyWith(color: color);
final Color color = _textColor(theme, tileTheme, theme.textTheme.caption.color);
return _denseLayout(tileTheme)
? style.copyWith(color: color, fontSize: 12.0)
: style.copyWith(color: color);
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
final ThemeData theme = Theme.of(context);
final ListTileTheme tileTheme = ListTileTheme.of(context);
final bool isTwoLine = !isThreeLine && subtitle != null;
final bool isOneLine = !isThreeLine && !isTwoLine;
double tileHeight;
if (isOneLine)
tileHeight = dense ? 48.0 : 56.0;
tileHeight = _denseLayout(tileTheme) ? 48.0 : 56.0;
else if (isTwoLine)
tileHeight = dense ? 60.0 : 72.0;
tileHeight = _denseLayout(tileTheme) ? 60.0 : 72.0;
else
tileHeight = dense ? 76.0 : 88.0;
tileHeight = _denseLayout(tileTheme) ? 76.0 : 88.0;
// Overall, the list tile is a Row() with these children.
final List<Widget> children = <Widget>[];
if (leading != null) {
children.add(new Container(
margin: const EdgeInsets.only(right: 16.0),
width: 40.0,
alignment: FractionalOffset.centerLeft,
child: leading
children.add(new IconTheme.merge(
context: context,
data: new IconThemeData(color: _iconColor(theme, tileTheme)),
child: new Container(
margin: const EdgeInsets.only(right: 16.0),
width: 40.0,
alignment: FractionalOffset.centerLeft,
child: leading
),
));
}
final Widget primaryLine = new AnimatedDefaultTextStyle(
style: _primaryTextStyle(context),
style: _titleTextStyle(theme, tileTheme),
duration: kThemeChangeDuration,
child: title ?? new Container()
);
@@ -229,22 +370,22 @@ class ListTile extends StatelessWidget {
children: <Widget>[
primaryLine,
new AnimatedDefaultTextStyle(
style: _secondaryTextStyle(context),
style: _subtitleTextStyle(theme, tileTheme),
duration: kThemeChangeDuration,
child: subtitle
child: subtitle,
)
]
);
}
children.add(new Expanded(
child: center
child: center,
));
if (trailing != null) {
children.add(new Container(
margin: const EdgeInsets.only(left: 16.0),
alignment: FractionalOffset.centerRight,
child: trailing
child: trailing,
));
}
@@ -254,9 +395,7 @@ class ListTile extends StatelessWidget {
child: new Container(
height: tileHeight,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: new Row(
children: children
)
child: new Row(children: children),
)
);
}

View File

@@ -9,7 +9,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('AboutDrawerItem control test', (WidgetTester tester) async {
testWidgets('AboutListTile control test', (WidgetTester tester) async {
await tester.pumpWidget(
new MaterialApp(
title: 'Pirate app',
@@ -20,7 +20,7 @@ void main() {
drawer: new Drawer(
child: new ListView(
children: <Widget>[
new AboutDrawerItem(
new AboutListTile(
applicationVersion: '0.1.2',
applicationIcon: const FlutterLogo(),
applicationLegalese: 'I am the very model of a modern major general.',
@@ -67,12 +67,12 @@ void main() {
testWidgets('About box logic defaults to executable name for app name', (WidgetTester tester) async {
await tester.pumpWidget(
new Material(child: new AboutDrawerItem()),
new Material(child: new AboutListTile()),
);
expect(find.text('About sky_shell'), findsOneWidget);
});
testWidgets('AboutDrawerItem control test', (WidgetTester tester) async {
testWidgets('AboutListTile control test', (WidgetTester tester) async {
final List<String> log = <String>[];
Future<Null> licenseFuture;

View File

@@ -20,9 +20,9 @@ void main() {
child: new Text('header')
)
),
new DrawerItem(
icon: new Icon(Icons.archive),
child: new Text('Archive')
new ListTile(
leading: new Icon(Icons.archive),
title: new Text('Archive')
)
]
)

View File

@@ -1,50 +0,0 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('ListTile control test', (WidgetTester tester) async {
await tester.pumpWidget(new MaterialApp(
home: new Material(
child: new Center(
child: new ListTile(
leading: new Icon(Icons.thumb_up),
title: new Text('Title'),
subtitle: new Text('Subtitle'),
trailing: new Icon(Icons.info),
enabled: false,
),
),
),
));
expect(find.text('Title'), findsOneWidget);
expect(find.text('Subtitle'), findsOneWidget);
});
testWidgets('ListTile control test', (WidgetTester tester) async {
final List<String> titles = <String>[ 'first', 'second', 'third' ];
await tester.pumpWidget(new MaterialApp(
home: new Material(
child: new Builder(
builder: (BuildContext context) {
return new ListView(
children: ListTile.divideTiles(
context: context,
tiles: titles.map((String title) => new ListTile(title: new Text(title))),
).toList(),
);
},
),
),
));
expect(find.text('first'), findsOneWidget);
expect(find.text('second'), findsOneWidget);
expect(find.text('third'), findsOneWidget);
});
}

View File

@@ -0,0 +1,234 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class TestIcon extends StatefulWidget {
TestIcon({ Key key }) : super(key: key);
@override
TestIconState createState() => new TestIconState();
}
class TestIconState extends State<TestIcon> {
IconThemeData iconTheme;
@override
Widget build(BuildContext context) {
iconTheme = IconTheme.of(context);
return new Icon(Icons.add);
}
}
class TestText extends StatefulWidget {
TestText(this.text, { Key key }) : super(key: key);
final String text;
@override
TestTextState createState() => new TestTextState();
}
class TestTextState extends State<TestText> {
TextStyle textStyle;
@override
Widget build(BuildContext context) {
textStyle = DefaultTextStyle.of(context).style;
return new Text(config.text);
}
}
void main() {
testWidgets('ListTile geometry', (WidgetTester tester) async {
// See https://material.io/guidelines/components/lists.html
bool hasSubtitle;
Widget buildFrame({ bool dense: false, bool isTwoLine: false, bool isThreeLine: false }) {
hasSubtitle = isTwoLine || isThreeLine;
return new MaterialApp(
home: new Material(
child: new Center(
child: new ListTile(
leading: new Text('leading'),
title: new Text('title'),
subtitle: hasSubtitle ? new Text('subtitle') : null,
trailing: new Text('trailing'),
dense: dense,
isThreeLine: isThreeLine,
),
),
),
);
}
void testChildren() {
expect(find.text('leading'), findsOneWidget);
expect(find.text('title'), findsOneWidget);
if (hasSubtitle)
expect(find.text('subtitle'), findsOneWidget);
expect(find.text('trailing'), findsOneWidget);
}
double left(String text) => tester.getTopLeft(find.text(text)).x;
double right(String text) => tester.getTopRight(find.text(text)).x;
double top(String text) => tester.getTopLeft(find.text(text)).y;
double bottom(String text) => tester.getBottomLeft(find.text(text)).y;
// 16.0 padding to the left and right of the leading and trailing widgets
void testHorizontalGeometry() {
expect(left('leading'), 16.0);
expect(left('title'), 72.0);
if (hasSubtitle)
expect(left('subtitle'), 72.0);
expect(left('title'), right('leading') + 16.0);
expect(right('trailing'), 800.0 - 16.0);
}
void testVerticalGeometry(double expectedHeight) {
expect(tester.getSize(find.byType(ListTile)), new Size(800.0, expectedHeight));
if (hasSubtitle)
expect(top('subtitle'), bottom('title'));
}
await tester.pumpWidget(buildFrame());
testChildren();
testHorizontalGeometry();
testVerticalGeometry(56.0);
await tester.pumpWidget(buildFrame(dense: true));
testChildren();
testHorizontalGeometry();
testVerticalGeometry(48.0);
await tester.pumpWidget(buildFrame(isTwoLine: true));
testChildren();
testHorizontalGeometry();
testVerticalGeometry(72.0);
await tester.pumpWidget(buildFrame(isTwoLine: true, dense: true));
testChildren();
testHorizontalGeometry();
testVerticalGeometry(60.0);
await tester.pumpWidget(buildFrame(isThreeLine: true));
testChildren();
testHorizontalGeometry();
testVerticalGeometry(88.0);
await tester.pumpWidget(buildFrame(isThreeLine: true, dense: true));
testChildren();
testHorizontalGeometry();
testVerticalGeometry(76.0);
});
testWidgets('ListTile.divideTiles', (WidgetTester tester) async {
final List<String> titles = <String>[ 'first', 'second', 'third' ];
await tester.pumpWidget(new MaterialApp(
home: new Material(
child: new Builder(
builder: (BuildContext context) {
return new ListView(
children: ListTile.divideTiles(
context: context,
tiles: titles.map((String title) => new ListTile(title: new Text(title))),
).toList(),
);
},
),
),
));
expect(find.text('first'), findsOneWidget);
expect(find.text('second'), findsOneWidget);
expect(find.text('third'), findsOneWidget);
});
testWidgets('ListTileTheme', (WidgetTester tester) async {
final Key titleKey = new UniqueKey();
final Key subtitleKey = new UniqueKey();
ThemeData theme;
Widget buildFrame({
bool enabled: true,
bool dense: false,
bool selected: false,
Color selectedColor,
Color iconColor,
Color textColor,
}) {
return new MaterialApp(
home: new Material(
child: new Center(
child: new ListTileTheme(
dense: dense,
selectedColor: selectedColor,
iconColor: iconColor,
textColor: textColor,
child: new Builder(
builder: (BuildContext context) {
theme = Theme.of(context);
return new ListTile(
enabled: enabled,
selected: selected,
leading: new TestIcon(),
title: new TestText('title', key: titleKey),
subtitle: new TestText('subtitle', key: subtitleKey),
);
}
),
),
),
),
);
}
const Color green = const Color(0xFF00FF00);
const Color red = const Color(0xFFFF0000);
Color iconColor() => tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color;
Color textColor(Key key) => tester.state<TestTextState>(find.byKey(key)).textStyle.color;
// A selected ListTile's leading and text get the primary color by default
await(tester.pumpWidget(buildFrame(selected: true)));
await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
expect(iconColor(), theme.primaryColor);
expect(textColor(titleKey), theme.primaryColor);
expect(textColor(subtitleKey), theme.primaryColor);
// A selected ListTile's leading and text get the ListTileTheme's selectedColor
await(tester.pumpWidget(buildFrame(selected: true, selectedColor: green)));
await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
expect(iconColor(), green);
expect(textColor(titleKey), green);
expect(textColor(subtitleKey), green);
// An unselected ListTile's leading icon gets the ListTileTheme's iconColor
// An unselected ListTile's title texts get the ListTileTheme's textColor
await(tester.pumpWidget(buildFrame(iconColor: red, textColor: green)));
await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
expect(iconColor(), red);
expect(textColor(titleKey), green);
expect(textColor(subtitleKey), green);
// If the item is disabled it's rendered with the theme's disabled color.
await(tester.pumpWidget(buildFrame(enabled: false)));
await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
expect(iconColor(), theme.disabledColor);
expect(textColor(titleKey), theme.disabledColor);
expect(textColor(subtitleKey), theme.disabledColor);
// If the item is disabled it's rendered with the theme's disabled color.
// Even if it's selected.
await(tester.pumpWidget(buildFrame(enabled: false, selected: true)));
await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
expect(iconColor(), theme.disabledColor);
expect(textColor(titleKey), theme.disabledColor);
expect(textColor(subtitleKey), theme.disabledColor);
});
}