forked from firka/flutter
use RadioListTile in expansion panels demo (#20240)
* use RadioListTile in expansion panels demo Fixes #6048. - expose `_Location` - appease analyzer
This commit is contained in:
@@ -6,7 +6,8 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import '../../gallery/demo.dart';
|
||||
|
||||
enum _Location {
|
||||
@visibleForTesting
|
||||
enum Location {
|
||||
Barbados,
|
||||
Bahamas,
|
||||
Bermuda
|
||||
@@ -225,12 +226,12 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
|
||||
);
|
||||
},
|
||||
),
|
||||
DemoItem<_Location>(
|
||||
DemoItem<Location>(
|
||||
name: 'Location',
|
||||
value: _Location.Bahamas,
|
||||
value: Location.Bahamas,
|
||||
hint: 'Select location',
|
||||
valueToString: (_Location location) => location.toString().split('.')[1],
|
||||
builder: (DemoItem<_Location> item) {
|
||||
valueToString: (Location location) => location.toString().split('.')[1],
|
||||
builder: (DemoItem<Location> item) {
|
||||
void close() {
|
||||
setState(() {
|
||||
item.isExpanded = false;
|
||||
@@ -242,47 +243,32 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
|
||||
return CollapsibleBody(
|
||||
onSave: () { Form.of(context).save(); close(); },
|
||||
onCancel: () { Form.of(context).reset(); close(); },
|
||||
child: FormField<_Location>(
|
||||
child: FormField<Location>(
|
||||
initialValue: item.value,
|
||||
onSaved: (_Location result) { item.value = result; },
|
||||
builder: (FormFieldState<_Location> field) {
|
||||
onSaved: (Location result) { item.value = result; },
|
||||
builder: (FormFieldState<Location> field) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Radio<_Location>(
|
||||
value: _Location.Bahamas,
|
||||
groupValue: field.value,
|
||||
onChanged: field.didChange,
|
||||
),
|
||||
const Text('Bahamas')
|
||||
]
|
||||
RadioListTile<Location>(
|
||||
value: Location.Bahamas,
|
||||
title: const Text('Bahamas'),
|
||||
groupValue: field.value,
|
||||
onChanged: field.didChange,
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Radio<_Location>(
|
||||
value: _Location.Barbados,
|
||||
groupValue: field.value,
|
||||
onChanged: field.didChange,
|
||||
),
|
||||
const Text('Barbados')
|
||||
]
|
||||
RadioListTile<Location>(
|
||||
value: Location.Barbados,
|
||||
title: const Text('Barbados'),
|
||||
groupValue: field.value,
|
||||
onChanged: field.didChange,
|
||||
),
|
||||
RadioListTile<Location>(
|
||||
value: Location.Bermuda,
|
||||
title: const Text('Bermuda'),
|
||||
groupValue: field.value,
|
||||
onChanged: field.didChange,
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Radio<_Location>(
|
||||
value: _Location.Bermuda,
|
||||
groupValue: field.value,
|
||||
onChanged: field.didChange,
|
||||
),
|
||||
const Text('Bermuda')
|
||||
]
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// Copyright 2018 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_gallery/demo/material/expansion_panels_demo.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
testWidgets('Expansion panel demo: radio tile selection changes on tap',
|
||||
(WidgetTester tester) async {
|
||||
await tester.pumpWidget(MaterialApp(home: ExpansionPanelsDemo()));
|
||||
|
||||
expect(_expandIcons, findsNWidgets(3));
|
||||
|
||||
await tester.tap(find.byWidget(_radioPanelExpandIcon));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(_radioFinder, findsNWidgets(3));
|
||||
|
||||
const int i = 1;
|
||||
|
||||
expect(_isRadioSelected(0), isTrue);
|
||||
expect(_isRadioSelected(i), isFalse);
|
||||
|
||||
await tester.tap(find.byWidget(_radioListTiles[i]));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(_isRadioSelected(0), isFalse);
|
||||
expect(_isRadioSelected(i), isTrue);
|
||||
});
|
||||
}
|
||||
|
||||
Finder get _expandIcons => find.byType(ExpandIcon);
|
||||
|
||||
Widget get _radioPanelExpandIcon => _expandIcons.evaluate().toList()[1].widget;
|
||||
|
||||
bool _isRadioSelected(int index) =>
|
||||
_radios[index].value == _radios[index].groupValue;
|
||||
|
||||
List<Radio<Location>> get _radios => List<Radio<Location>>.from(
|
||||
_radioFinder.evaluate().map<Widget>((Element e) => e.widget));
|
||||
|
||||
// [find.byType] and [find.widgetWithText] do not match subclasses; `Radio` is not sufficient to find a `Radio<_Location>`.
|
||||
// Another approach is to grab the `runtimeType` of a dummy instance; see packages/flutter/test/material/control_list_tile_test.dart.
|
||||
Finder get _radioFinder =>
|
||||
find.byWidgetPredicate((Widget w) => w is Radio<Location>);
|
||||
|
||||
List<RadioListTile<Location>> get _radioListTiles =>
|
||||
List<RadioListTile<Location>>.from(
|
||||
_radioListTilesFinder.evaluate().map<Widget>((Element e) => e.widget));
|
||||
|
||||
Finder get _radioListTilesFinder =>
|
||||
find.byWidgetPredicate((Widget w) => w is RadioListTile<Location>);
|
||||
Reference in New Issue
Block a user