import 'package:flutter/material.dart'; enum InputPromptType { Text, Number } /// Prompt which enables the input of data /// /// An input prompt can be for numbers or for text. Though the input type only affects platforms that display a keyboard. class InputPrompt extends StatefulWidget { String title; String prompt; String currentValue; InputPromptType type; Function()? cancelAction; Function(String)? successAction; InputPrompt( {super.key, required this.title, required this.prompt, this.currentValue = "", required this.type, this.cancelAction, this.successAction}); @override State createState() { // ignore: no_logic_in_create_state return InputPromptState( title: title, prompt: prompt, currentValue: currentValue, type: type, successAction: successAction ?? (str) {}, cancelAction: cancelAction ?? () {}); } } class InputPromptState extends State { String title; String prompt; String currentValue; InputPromptType type; Function() cancelAction; Function(String) successAction; TextEditingController textField = TextEditingController(); InputPromptState( {required this.title, required this.prompt, required this.currentValue, required this.type, required this.successAction, required this.cancelAction}) { textField.text = currentValue; } @override Widget build(BuildContext context) { return AlertDialog( title: Text(title), content: SizedBox( width: 200, height: 100, child: Column( children: [ Text(prompt), if (type == InputPromptType.Text) TextField( controller: textField, ), if (type == InputPromptType.Number) TextField( controller: textField, keyboardType: TextInputType.number, ) ], ), ), actions: [ ElevatedButton( onPressed: () { successAction.call(textField.text); Navigator.pop(context, textField.text); }, child: Text("Submit")), ElevatedButton( onPressed: () { cancelAction.call(); Navigator.pop(context, ""); }, child: Text("Cancel")) ], ); } }