refs #60: expose state to let use a key to open the dialog

This commit is contained in:
Salvatore Giordano
2020-04-04 16:25:19 +02:00
parent bb7d3c75c8
commit c74dcf43ca
2 changed files with 9 additions and 12 deletions

View File

@@ -11,11 +11,6 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new MaterialApp( return new MaterialApp(
@@ -38,6 +33,7 @@ class _MyAppState extends State<MyApp> {
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[ children: <Widget>[
CountryCodePicker( CountryCodePicker(
key: key,
onChanged: print, onChanged: print,
// Initial selection and favorite can be one of code ('IT') OR dial_code('+39') // Initial selection and favorite can be one of code ('IT') OR dial_code('+39')
initialSelection: 'IT', initialSelection: 'IT',

View File

@@ -78,7 +78,8 @@ class CountryCodePicker extends StatefulWidget {
this.comparator, this.comparator,
this.customList, this.customList,
this.dialogSize, this.dialogSize,
}); Key key,
}) : super(key: key);
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
@@ -104,29 +105,29 @@ class CountryCodePicker extends StatefulWidget {
elements = elements.where((c) => countryFilter.contains(c.code)).toList(); elements = elements.where((c) => countryFilter.contains(c.code)).toList();
} }
return _CountryCodePickerState(elements); return CountryCodePickerState(elements);
} }
} }
class _CountryCodePickerState extends State<CountryCodePicker> { class CountryCodePickerState extends State<CountryCodePicker> {
CountryCode selectedItem; CountryCode selectedItem;
List<CountryCode> elements = []; List<CountryCode> elements = [];
List<CountryCode> favoriteElements = []; List<CountryCode> favoriteElements = [];
_CountryCodePickerState(this.elements); CountryCodePickerState(this.elements);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Widget _widget; Widget _widget;
if (widget.builder != null) if (widget.builder != null)
_widget = InkWell( _widget = InkWell(
onTap: _showSelectionDialog, onTap: showCountryCodePickerDialog,
child: widget.builder(selectedItem.localize(context)), child: widget.builder(selectedItem.localize(context)),
); );
else { else {
_widget = FlatButton( _widget = FlatButton(
padding: widget.padding, padding: widget.padding,
onPressed: widget.enabled ? _showSelectionDialog : null, onPressed: widget.enabled ? showCountryCodePickerDialog : null,
child: Flex( child: Flex(
direction: Axis.horizontal, direction: Axis.horizontal,
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
@@ -205,7 +206,7 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
super.initState(); super.initState();
} }
void _showSelectionDialog() { void showCountryCodePickerDialog() {
showDialog( showDialog(
context: context, context: context,
builder: (_) => SelectionDialog( builder: (_) => SelectionDialog(