-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
143 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class OptionsPopup extends StatefulWidget { | ||
final String title; | ||
final Function callback; | ||
final List<String> options; | ||
|
||
const OptionsPopup({ | ||
super.key, | ||
required this.title, | ||
required this.callback, | ||
required this.options, | ||
}); | ||
|
||
@override | ||
State<OptionsPopup> createState() => _OptionsPopupState(); | ||
} | ||
|
||
class _OptionsPopupState extends State<OptionsPopup> { | ||
late List<String> filteredOptions; | ||
late TextEditingController searchController; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
filteredOptions = widget.options; | ||
searchController = TextEditingController(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return AlertDialog( | ||
title: Text(widget.title), | ||
content: Container( | ||
width: double.maxFinite, | ||
child: ListView.builder( | ||
shrinkWrap: true, | ||
itemCount: filteredOptions.length + 1, | ||
itemBuilder: (BuildContext context, int index) { | ||
if (index == 0) { | ||
return widget.options.length > 10 | ||
? TextField( | ||
onChanged: _filterOptions, | ||
decoration: InputDecoration( | ||
icon: Icon(Icons.search_rounded), | ||
hintText: "Search...", | ||
border: OutlineInputBorder( | ||
borderRadius: BorderRadius.circular(8.0), | ||
), | ||
), | ||
) | ||
: const SizedBox.shrink(); | ||
} | ||
return ListTile( | ||
title: Text(filteredOptions[index - 1]), | ||
onTap: () { | ||
widget.callback(filteredOptions[index - 1]); | ||
Navigator.of(context).pop(); | ||
}, | ||
); | ||
}, | ||
), | ||
), | ||
); | ||
} | ||
|
||
void _filterOptions(String query) { | ||
setState(() { | ||
if (query.isEmpty) { | ||
filteredOptions = widget.options; | ||
} else { | ||
filteredOptions = widget.options | ||
.where((entry) => entry.toLowerCase().contains(query.toLowerCase())) | ||
.toList(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
void showPopup( | ||
BuildContext context, | ||
String title, | ||
Function callback, | ||
List<String> options, | ||
) { | ||
showDialog( | ||
context: context, | ||
builder: (BuildContext context) { | ||
return OptionsPopup( | ||
title: title, | ||
callback: callback, | ||
options: options, | ||
); | ||
}, | ||
); | ||
} |