Commit 7a35db1f authored by Kyle Bradshaw's avatar Kyle Bradshaw Committed by Ian Hickson

_SaltedKey solution to `ExpansionPanelList` (#11902)

* _SaltedKey solution to `ExpansionPanelList`

_SaltedKey implementation courtesy of @Hixie
Tested and confirmed working.
Fixes #11166

* Added a simple test

* Style correction to test
parent bb0a724a
......@@ -13,6 +13,32 @@ import 'theme.dart';
const double _kPanelHeaderCollapsedHeight = 48.0;
const double _kPanelHeaderExpandedHeight = 64.0;
class _SaltedKey<S, V> extends LocalKey {
const _SaltedKey(this.salt, this.value);
final S salt;
final V value;
@override
bool operator ==(dynamic other) {
if (other.runtimeType != runtimeType)
return false;
final _SaltedKey<S, V> typedOther = other;
return salt == typedOther.salt
&& value == typedOther.value;
}
@override
int get hashCode => hashValues(runtimeType, salt, value);
@override
String toString() {
final String saltString = S == String ? '<\'$salt\'>' : '<$salt>';
final String valueString = V == String ? '<\'$value\'>' : '<$value>';
return '[$saltString $valueString]';
}
}
/// Signature for the callback that's called when an [ExpansionPanel] is
/// expanded or collapsed.
///
......@@ -109,7 +135,7 @@ class ExpansionPanelList extends StatelessWidget {
for (int i = 0; i < children.length; i += 1) {
if (_isChildExpanded(i) && i != 0 && !_isChildExpanded(i - 1))
items.add(new MaterialGap(key: new ValueKey<int>(i * 2 - 1)));
items.add(new MaterialGap(key: new _SaltedKey<BuildContext, int>(context, i * 2 - 1)));
final Row header = new Row(
children: <Widget>[
......@@ -144,7 +170,7 @@ class ExpansionPanelList extends StatelessWidget {
items.add(
new MaterialSlice(
key: new ValueKey<int>(i * 2),
key: new _SaltedKey<BuildContext, int>(context, i * 2),
child: new Column(
children: <Widget>[
header,
......@@ -163,7 +189,7 @@ class ExpansionPanelList extends StatelessWidget {
);
if (_isChildExpanded(i) && i != children.length - 1)
items.add(new MaterialGap(key: new ValueKey<int>(i * 2 + 1)));
items.add(new MaterialGap(key: new _SaltedKey<BuildContext, int>(context, i * 2 + 1)));
}
return new MergeableMaterial(
......
......@@ -71,4 +71,42 @@ void main() {
box = tester.renderObject(find.byType(ExpansionPanelList));
expect(box.size.height - oldHeight, greaterThanOrEqualTo(100.0)); // 100 + some margin
});
testWidgets("Multiple Panel List test", (WidgetTester tester) async {
await tester.pumpWidget(
new MaterialApp(
home: new ListView(
children: <ExpansionPanelList>[
new ExpansionPanelList(
children: <ExpansionPanel>[
new ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
return new Text(isExpanded ? 'B' : 'A');
},
body: const SizedBox(height:100.0),
isExpanded: true,
),
],
),
new ExpansionPanelList(
children: <ExpansionPanel>[
new ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded){
return new Text(isExpanded ? 'D' : 'C');
},
body: const SizedBox(height:100.0),
isExpanded: true,
),
],
),
],
),
),
);
await tester.pump(const Duration(milliseconds: 200));
expect(find.text('A'), findsNothing);
expect(find.text('B'), findsOneWidget);
expect(find.text('C'), findsNothing);
expect(find.text('D'), findsOneWidget);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment