import 'package:country_code_picker/country_code.dart'; import 'package:flutter/material.dart'; /// selection dialog used for selection of the country code class SelectionDialog extends StatefulWidget { final List elements; final bool showCountryOnly; final InputDecoration searchDecoration; final TextStyle searchStyle; final TextStyle textStyle; final BoxDecoration boxDecoration; final WidgetBuilder emptySearchBuilder; final bool showFlag; final double flagWidth; final Size size; final bool hideSearch; final Icon closeIcon; /// Background color of SelectionDialog final Color backgroundColor; /// elements passed as favorite final List favoriteElements; SelectionDialog( this.elements, this.favoriteElements, { Key key, this.showCountryOnly, this.emptySearchBuilder, InputDecoration searchDecoration = const InputDecoration(), this.searchStyle, this.textStyle, this.boxDecoration, this.showFlag, this.flagWidth = 32, this.size, this.backgroundColor, this.hideSearch = false, this.closeIcon, }) : assert(searchDecoration != null, 'searchDecoration must not be null!'), this.searchDecoration = searchDecoration.prefixIcon == null ? searchDecoration.copyWith(prefixIcon: Icon(Icons.search)) : searchDecoration, super(key: key); @override State createState() => _SelectionDialogState(); } class _SelectionDialogState extends State { /// this is useful for filtering purpose List filteredElements; @override Widget build(BuildContext context) => Padding( padding: const EdgeInsets.all(16.0), child: Container( clipBehavior: Clip.hardEdge, width: widget.size?.width ?? MediaQuery.of(context).size.width, height: widget.size?.height ?? MediaQuery.of(context).size.height * 0.85, decoration: widget.boxDecoration ?? BoxDecoration( color: widget.backgroundColor ?? Colors.white, borderRadius: BorderRadius.all(Radius.circular(25.0)), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(1), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3), // changes position of shadow ), ], ), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.end, children: [ IconButton( padding: const EdgeInsets.all(0), iconSize: 20, icon: widget.closeIcon, onPressed: () => Navigator.pop(context), ), if (!widget.hideSearch) Padding( padding: const EdgeInsets.symmetric(horizontal: 24), child: TextField( style: widget.searchStyle, decoration: widget.searchDecoration, onChanged: _filterElements, ), ), Expanded( child: ListView( children: [ widget.favoriteElements.isEmpty ? const DecoratedBox(decoration: BoxDecoration()) : Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ ...widget.favoriteElements.map( (f) => SimpleDialogOption( child: _buildOption(f), onPressed: () { _selectItem(f); }, ), ), const Divider(), ], ), if (filteredElements.isEmpty) _buildEmptySearchWidget(context) else ...filteredElements.map( (e) => SimpleDialogOption( child: _buildOption(e), onPressed: () { _selectItem(e); }, ), ), ], ), ), ], ), ), ); Widget _buildOption(CountryCode e) { return Container( width: 400, child: Flex( direction: Axis.horizontal, children: [ if (widget.showFlag) Flexible( child: Padding( padding: const EdgeInsets.only(right: 16.0), child: Image.asset( e.flagUri, package: 'country_code_picker', width: widget.flagWidth, ), ), ), Expanded( flex: 4, child: Text( widget.showCountryOnly ? e.toCountryStringOnly() : e.toLongString(), overflow: TextOverflow.fade, style: widget.textStyle, ), ), ], ), ); } Widget _buildEmptySearchWidget(BuildContext context) { if (widget.emptySearchBuilder != null) { return widget.emptySearchBuilder(context); } return Center( child: Text('No country found'), ); } @override void initState() { filteredElements = widget.elements; super.initState(); } void _filterElements(String s) { s = s.toUpperCase(); setState(() { filteredElements = widget.elements .where((e) => e.code.contains(s) || e.dialCode.contains(s) || e.name.toUpperCase().contains(s)) .toList(); }); } void _selectItem(CountryCode e) { Navigator.pop(context, e); } }