From 31e2a500f73f11ce76250950bba809b7bddd90ae Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sun, 5 Feb 2017 21:39:24 -0800 Subject: [PATCH] Use sliver-based scrolling in more places (#7892) This patch uses sliver-based two more gallery demos, the stocks example, in the date picker, and in markdown. --- .../lib/demo/date_and_time_picker_demo.dart | 128 +++++++++--------- .../lib/demo/progress_indicator_demo.dart | 2 +- examples/stocks/lib/stock_list.dart | 9 +- .../flutter/lib/src/material/date_picker.dart | 40 +++--- .../flutter/lib/src/widgets/scroll_view.dart | 25 +++- packages/flutter/lib/src/widgets/sliver.dart | 2 +- .../flutter_markdown/lib/src/markdown.dart | 16 +-- .../lib/src/markdown_raw.dart | 40 +++--- 8 files changed, 135 insertions(+), 127 deletions(-) diff --git a/examples/flutter_gallery/lib/demo/date_and_time_picker_demo.dart b/examples/flutter_gallery/lib/demo/date_and_time_picker_demo.dart index a2647d3268..c2398864f2 100644 --- a/examples/flutter_gallery/lib/demo/date_and_time_picker_demo.dart +++ b/examples/flutter_gallery/lib/demo/date_and_time_picker_demo.dart @@ -128,75 +128,69 @@ class _DateAndTimePickerDemoState extends State { Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar(title: new Text('Date and time pickers')), - body: new ScrollableViewport( - child: new Container( + body: new DropdownButtonHideUnderline( + child: new ListView( padding: const EdgeInsets.all(16.0), - alignment: FractionalOffset.topLeft, - child: new DropdownButtonHideUnderline( - child: new Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - new TextField( - labelText: 'Event name', - style: Theme.of(context).textTheme.display1, - ), - new TextField( - labelText: 'Location', - style: Theme.of(context).textTheme.display1.copyWith(fontSize: 20.0), - ), - new _DateTimePicker( - labelText: 'From', - selectedDate: _fromDate, - selectedTime: _fromTime, - selectDate: (DateTime date) { - setState(() { - _fromDate = date; - }); - }, - selectTime: (TimeOfDay time) { - setState(() { - _fromTime = time; - }); - }, - ), - new _DateTimePicker( - labelText: 'To', - selectedDate: _toDate, - selectedTime: _toTime, - selectDate: (DateTime date) { - setState(() { - _toDate = date; - }); - }, - selectTime: (TimeOfDay time) { - setState(() { - _toTime = time; - }); - }, - ), - new InputContainer( - labelText: 'Activity', - hintText: 'Choose an activity', - isEmpty: _activity == null, - child: new DropdownButton( - value: _activity, - isDense: true, - onChanged: (String newValue) { - setState(() { - _activity = newValue; - }); - }, - items: _allActivities.map((String value) { - return new DropdownMenuItem( - value: value, - child: new Text(value), - ); - }).toList(), - ), - ), - ], + children: [ + new TextField( + labelText: 'Event name', + style: Theme.of(context).textTheme.display1, ), - ), + new TextField( + labelText: 'Location', + style: Theme.of(context).textTheme.display1.copyWith(fontSize: 20.0), + ), + new _DateTimePicker( + labelText: 'From', + selectedDate: _fromDate, + selectedTime: _fromTime, + selectDate: (DateTime date) { + setState(() { + _fromDate = date; + }); + }, + selectTime: (TimeOfDay time) { + setState(() { + _fromTime = time; + }); + }, + ), + new _DateTimePicker( + labelText: 'To', + selectedDate: _toDate, + selectedTime: _toTime, + selectDate: (DateTime date) { + setState(() { + _toDate = date; + }); + }, + selectTime: (TimeOfDay time) { + setState(() { + _toTime = time; + }); + }, + ), + new InputContainer( + labelText: 'Activity', + hintText: 'Choose an activity', + isEmpty: _activity == null, + child: new DropdownButton( + value: _activity, + isDense: true, + onChanged: (String newValue) { + setState(() { + _activity = newValue; + }); + }, + items: _allActivities.map((String value) { + return new DropdownMenuItem( + value: value, + child: new Text(value), + ); + }).toList(), + ), + ), + ], ), ), ); diff --git a/examples/flutter_gallery/lib/demo/progress_indicator_demo.dart b/examples/flutter_gallery/lib/demo/progress_indicator_demo.dart index 05619a21d5..8f773fb33c 100644 --- a/examples/flutter_gallery/lib/demo/progress_indicator_demo.dart +++ b/examples/flutter_gallery/lib/demo/progress_indicator_demo.dart @@ -101,7 +101,7 @@ class _ProgressIndicatorDemoState extends State with Sing return new Scaffold( appBar: new AppBar(title: new Text('Progress indicators')), body: new Center( - child: new ScrollableViewport( + child: new SingleChildScrollView( child: new DefaultTextStyle( style: Theme.of(context).textTheme.title, child: new GestureDetector( diff --git a/examples/stocks/lib/stock_list.dart b/examples/stocks/lib/stock_list.dart index 8185c9fa19..c247dfc8ea 100644 --- a/examples/stocks/lib/stock_list.dart +++ b/examples/stocks/lib/stock_list.dart @@ -17,17 +17,18 @@ class StockList extends StatelessWidget { @override Widget build(BuildContext context) { - return new ScrollableList( + return new ListView.builder( key: const ValueKey('stock-list'), itemExtent: StockRow.kHeight, - children: stocks.map((Stock stock) { + itemCount: stocks.length, + itemBuilder: (BuildContext context, int index) { return new StockRow( - stock: stock, + stock: stocks[index], onPressed: onOpen, onDoubleTap: onShow, onLongPressed: onAction ); - }) + }, ); } } diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index c13f952a93..84aa8f1c28 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -544,34 +544,28 @@ class YearPicker extends StatefulWidget { class _YearPickerState extends State { static const double _itemExtent = 50.0; - List _buildItems(BuildContext context, int start, int count) { - final ThemeData themeData = Theme.of(context); - final TextStyle style = themeData.textTheme.body1; - final List items = new List(); - for (int i = start; i < start + count; i++) { - final int year = config.firstDate.year + i; - final TextStyle itemStyle = year == config.selectedDate.year ? - themeData.textTheme.headline.copyWith(color: themeData.accentColor) : style; - items.add(new InkWell( - key: new ValueKey(year), - onTap: () { - config.onChanged(new DateTime(year, config.selectedDate.month, config.selectedDate.day)); - }, - child: new Center( - child: new Text(year.toString(), style: itemStyle) - ) - )); - } - return items; - } - @override Widget build(BuildContext context) { assert(debugCheckHasMaterial(context)); - return new ScrollableLazyList( + final ThemeData themeData = Theme.of(context); + final TextStyle style = themeData.textTheme.body1; + return new ListView.builder( itemExtent: _itemExtent, itemCount: config.lastDate.year - config.firstDate.year + 1, - itemBuilder: _buildItems + itemBuilder: (BuildContext context, int index) { + final int year = config.firstDate.year + index; + final TextStyle itemStyle = year == config.selectedDate.year ? + themeData.textTheme.headline.copyWith(color: themeData.accentColor) : style; + return new InkWell( + key: new ValueKey(year), + onTap: () { + config.onChanged(new DateTime(year, config.selectedDate.month, config.selectedDate.day)); + }, + child: new Center( + child: new Text(year.toString(), style: itemStyle), + ), + ); + }, ); } } diff --git a/packages/flutter/lib/src/widgets/scroll_view.dart b/packages/flutter/lib/src/widgets/scroll_view.dart index a0e7369463..dbbec81c09 100644 --- a/packages/flutter/lib/src/widgets/scroll_view.dart +++ b/packages/flutter/lib/src/widgets/scroll_view.dart @@ -168,6 +168,25 @@ class ListView extends BoxScrollView { padding: padding, ); + ListView.builder({ + Key key, + Axis scrollDirection: Axis.vertical, + bool reverse: false, + ScrollPhysics physics, + bool shrinkWrap: false, + EdgeInsets padding, + this.itemExtent, + IndexedWidgetBuilder itemBuilder, + int itemCount, + }) : childrenDelegate = new SliverChildBuilderDelegate(itemBuilder, childCount: itemCount), super( + key: key, + scrollDirection: scrollDirection, + reverse: reverse, + physics: physics, + shrinkWrap: shrinkWrap, + padding: padding, + ); + ListView.custom({ Key key, Axis scrollDirection: Axis.vertical, @@ -190,7 +209,7 @@ class ListView extends BoxScrollView { final double itemExtent; - final SliverChildListDelegate childrenDelegate; + final SliverChildDelegate childrenDelegate; @override Widget buildChildLayout(BuildContext context) { @@ -317,7 +336,7 @@ class GridView extends BoxScrollView { final SliverGridDelegate gridDelegate; - final SliverChildListDelegate childrenDelegate; + final SliverChildDelegate childrenDelegate; @override Widget buildChildLayout(BuildContext context) { @@ -373,7 +392,7 @@ class PageView extends BoxScrollView { assert(childrenDelegate != null); } - final SliverChildListDelegate childrenDelegate; + final SliverChildDelegate childrenDelegate; @override Widget buildChildLayout(BuildContext context) { diff --git a/packages/flutter/lib/src/widgets/sliver.dart b/packages/flutter/lib/src/widgets/sliver.dart index bc9997a965..ea75e10aa3 100644 --- a/packages/flutter/lib/src/widgets/sliver.dart +++ b/packages/flutter/lib/src/widgets/sliver.dart @@ -63,7 +63,7 @@ class SliverChildBuilderDelegate extends SliverChildDelegate { int get estimatedChildCount => childCount; @override - bool shouldRebuild(@checked SliverChildListDelegate oldDelegate) => true; + bool shouldRebuild(@checked SliverChildBuilderDelegate oldDelegate) => true; } // /// diff --git a/packages/flutter_markdown/lib/src/markdown.dart b/packages/flutter_markdown/lib/src/markdown.dart index 3549ae47e0..4012510624 100644 --- a/packages/flutter_markdown/lib/src/markdown.dart +++ b/packages/flutter_markdown/lib/src/markdown.dart @@ -64,15 +64,15 @@ class MarkdownBody extends MarkdownBodyRaw { /// by default not using syntax highlighting, but it's possible to pass in /// a custom [syntaxHighlighter]. /// - /// Typically, you may want to wrap the [MarkdownBody] widget in a [Padding] and - /// a [ScrollableViewport], or use the [Markdown] class + /// Typically, you may want to wrap the [MarkdownBody] widget in a + /// [SingleChildScrollView], or use the [Markdown] class. /// - /// new ScrollableViewport( - /// child: new Padding( - /// padding: new EdgeInsets.all(16.0), - /// child: new Markdown(data: markdownSource) - /// ) - /// ) + /// ```dart + /// new SingleChildScrollView( + /// padding: new EdgeInsets.all(16.0), + /// child: new Markdown(data: markdownSource), + /// ), + /// ``` MarkdownBody({ String data, SyntaxHighlighter syntaxHighlighter, diff --git a/packages/flutter_markdown/lib/src/markdown_raw.dart b/packages/flutter_markdown/lib/src/markdown_raw.dart index b26a7eee1d..a00d48a8cb 100644 --- a/packages/flutter_markdown/lib/src/markdown_raw.dart +++ b/packages/flutter_markdown/lib/src/markdown_raw.dart @@ -52,16 +52,16 @@ class MarkdownRaw extends StatelessWidget { @override Widget build(BuildContext context) { - return new ScrollableViewport( - child: new Padding( - padding: padding, - child: createMarkdownBody( - data: data, - markdownStyle: markdownStyle, - syntaxHighlighter: syntaxHighlighter, - onTapLink: onTapLink - ) - ) + // TODO(abarth): We should use a ListView here and lazily build the widgets + // from the markdown. + return new SingleChildScrollView( + padding: padding, + child: createMarkdownBody( + data: data, + markdownStyle: markdownStyle, + syntaxHighlighter: syntaxHighlighter, + onTapLink: onTapLink, + ), ); } @@ -93,17 +93,17 @@ class MarkdownBodyRaw extends StatefulWidget { /// highlighting, but it's possible to pass in a custom [syntaxHighlighter]. /// /// Typically, you may want to wrap the [MarkdownBodyRaw] widget in a - /// [Padding] and a [ScrollableViewport], or use the [Markdown class] + /// a [ScrollableViewport], or use the [Markdown class]. /// - /// new ScrollableViewport( - /// child: new Padding( - /// padding: new EdgeInsets.all(16.0), - /// child: new MarkdownBodyRaw( - /// data: markdownSource, - /// markdownStyle: myStyle - /// ) - /// ) - /// ) + /// ```dart + /// new SingleChildScrollView( + /// padding: new EdgeInsets.all(16.0), + /// child: new MarkdownBodyRaw( + /// data: markdownSource, + /// markdownStyle: myStyle, + /// ), + /// ), + /// ``` MarkdownBodyRaw({ this.data, this.markdownStyle,