From d4bbca64051a5efa929274ebf3c01daa4b53de1d Mon Sep 17 00:00:00 2001 From: zontreck Date: Fri, 31 Jan 2025 14:23:15 -0700 Subject: [PATCH] Restructure some things --- lib/Alert.dart | 25 ++++++++++++++++ lib/Constants.dart | 2 +- lib/Prompt.dart | 39 +++++++++++++------------ lib/tests/alert.dart | 31 ++++++++++++++++++++ lib/tests/prompt.dart | 58 +++++++++++++++++++++++++++++++++++++ lib/tests/tester.dart | 21 ++++++++++++++ pubspec.yaml | 2 +- test/testAlert.dart | 11 ++++++++ test/testPrompt.dart | 66 ++++--------------------------------------- 9 files changed, 174 insertions(+), 81 deletions(-) create mode 100644 lib/Alert.dart create mode 100644 lib/tests/alert.dart create mode 100644 lib/tests/prompt.dart create mode 100644 lib/tests/tester.dart create mode 100644 test/testAlert.dart diff --git a/lib/Alert.dart b/lib/Alert.dart new file mode 100644 index 0000000..26eab4d --- /dev/null +++ b/lib/Alert.dart @@ -0,0 +1,25 @@ +import 'package:flutter/material.dart'; + +class Alert extends StatelessWidget { + String title; + String body; + Function()? dismissAction; + + Alert( + {required this.title, required this.body, super.key, this.dismissAction}); + + @override + Widget build(BuildContext context) { + return AlertDialog( + title: Text(title), + content: Text(body), + actions: [ + ElevatedButton( + onPressed: () { + if (dismissAction != null) dismissAction!.call(); + }, + child: Text("Dismiss")) + ], + ); + } +} diff --git a/lib/Constants.dart b/lib/Constants.dart index eb004f6..c4db5ef 100644 --- a/lib/Constants.dart +++ b/lib/Constants.dart @@ -2,5 +2,5 @@ import 'package:flutter/material.dart'; class LibACFlutterConstants { static const Color TITLEBAR_COLOR = Color.fromARGB(255, 99, 0, 0); - static const VERSION = "1.0.013125+1234"; + static const VERSION = "1.0.013125+1423"; } diff --git a/lib/Prompt.dart b/lib/Prompt.dart index 81fecc8..b942e1e 100644 --- a/lib/Prompt.dart +++ b/lib/Prompt.dart @@ -3,23 +3,35 @@ 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}); + 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); + title: title, + prompt: prompt, + currentValue: currentValue, + type: type, + successAction: successAction ?? (str) {}, + cancelAction: cancelAction ?? () {}); } } @@ -28,13 +40,17 @@ class InputPromptState extends State { 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.type, + required this.successAction, + required this.cancelAction}) { textField.text = currentValue; } @@ -63,11 +79,13 @@ class InputPromptState extends State { 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")) @@ -75,18 +93,3 @@ class InputPromptState extends State { ); } } - -class Alert extends StatelessWidget { - String title; - String body; - - Alert({required this.title, required this.body, super.key}); - - @override - Widget build(BuildContext context) { - return AlertDialog( - title: Text(title), - content: Text(body), - ); - } -} diff --git a/lib/tests/alert.dart b/lib/tests/alert.dart new file mode 100644 index 0000000..57a2e9b --- /dev/null +++ b/lib/tests/alert.dart @@ -0,0 +1,31 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:libacflutter/Alert.dart'; +import 'package:libacflutter/Constants.dart'; + +class TestAlert extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text("Alert Dialog Null Callback Test"), + backgroundColor: LibACFlutterConstants.TITLEBAR_COLOR), + body: Center( + child: ElevatedButton( + onPressed: () async { + await showAdaptiveDialog( + context: context, + builder: (builder) { + return Alert( + title: "Test Alert", + body: "This is a test that you can ignore"); + }); + + exit(0); + }, + child: Text("Test Null Alert")), + ), + ); + } +} diff --git a/lib/tests/prompt.dart b/lib/tests/prompt.dart new file mode 100644 index 0000000..aad8500 --- /dev/null +++ b/lib/tests/prompt.dart @@ -0,0 +1,58 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:libacflutter/Alert.dart'; +import 'package:libacflutter/Constants.dart'; +import 'package:libacflutter/Prompt.dart'; + +class TestPrompt extends StatefulWidget { + const TestPrompt({super.key}); + + @override + State createState() { + return TestPromptState(); + } +} + +class TestPromptState extends State { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text("Input Prompt Test"), + backgroundColor: LibACFlutterConstants.TITLEBAR_COLOR, + ), + body: Column( + children: [ + ElevatedButton( + onPressed: () async { + var reply = await showAdaptiveDialog( + context: context, + builder: (buildx) { + return InputPrompt( + title: "Test", + prompt: "Enter a value", + currentValue: "", + type: InputPromptType.Text, + ); + }); + + // Show a alert dialog with the reply content + showAdaptiveDialog( + context: context, + builder: (buildx) { + return Alert( + title: "Test Result", + body: reply, + dismissAction: () { + exit(0); + }, + ); + }); + }, + child: Text("Show prompt")) + ], + ), + ); + } +} diff --git a/lib/tests/tester.dart b/lib/tests/tester.dart new file mode 100644 index 0000000..5678dff --- /dev/null +++ b/lib/tests/tester.dart @@ -0,0 +1,21 @@ +import 'package:flutter/material.dart'; +import 'package:libacflutter/tests/alert.dart'; +import 'package:libacflutter/tests/prompt.dart'; + +class WidgetTest extends StatelessWidget { + String route; + + WidgetTest({super.key, required this.route}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData.dark(), + routes: { + "/testprompt": (context) => TestPrompt(), + "/testalert0": (context) => TestAlert() + }, + home: TestPrompt(), + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index a85c10b..9519acd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: libacflutter description: "A new Flutter package project." -version: 1.0.013125+1234 +version: 1.0.013125+1423 homepage: https://zontreck.com publish_to: https://git.zontreck.com/api/packages/AriasCreations/pub diff --git a/test/testAlert.dart b/test/testAlert.dart new file mode 100644 index 0000000..1604f36 --- /dev/null +++ b/test/testAlert.dart @@ -0,0 +1,11 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:libacflutter/tests/tester.dart'; + +void main() { + test("Test a null alert callback", () async { + runApp(WidgetTest(route: "/testalert0")); + }); +} diff --git a/test/testPrompt.dart b/test/testPrompt.dart index c128c8b..7b2496c 100644 --- a/test/testPrompt.dart +++ b/test/testPrompt.dart @@ -1,70 +1,14 @@ -import 'package:flutter/cupertino.dart'; +import 'dart:io'; + import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:libacflutter/Alert.dart'; import 'package:libacflutter/Constants.dart'; import 'package:libacflutter/Prompt.dart'; +import 'package:libacflutter/tests/tester.dart'; void main() { test("Test the input prompt", () async { - runApp(WidgetTester(route: "/testprompt")); + runApp(WidgetTest(route: "/testprompt")); }); } - -class WidgetTester extends StatelessWidget { - String route; - - WidgetTester({required this.route}); - - @override - Widget build(BuildContext context) { - return MaterialApp( - theme: ThemeData.dark(), - routes: {"/testprompt": (context) => TestPrompt()}, - home: TestPrompt(), - ); - } -} - -class TestPrompt extends StatefulWidget { - @override - State createState() { - return TestPromptState(); - } -} - -class TestPromptState extends State { - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text("Input Prompt Test"), - backgroundColor: LibACFlutterConstants.TITLEBAR_COLOR, - ), - body: Column( - children: [ - ElevatedButton( - onPressed: () async { - var reply = await showAdaptiveDialog( - context: context, - builder: (buildx) { - return InputPrompt( - title: "Test", - prompt: "Enter a value", - currentValue: "", - type: InputPromptType.Text, - ); - }); - - // Show a alert dialog with the reply content - showAdaptiveDialog( - context: context, - builder: (buildx) { - return Alert(title: "Test Result", body: reply); - }); - }, - child: Text("Show prompt")) - ], - ), - ); - } -}