|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:introduction_screen/introduction_screen.dart'; |
| 3 | +import 'package:font_awesome_flutter/font_awesome_flutter.dart'; |
| 4 | +import 'package:file_picker/file_picker.dart'; |
| 5 | +import '../components/global.dart'; |
| 6 | + |
| 7 | +class Add extends StatefulWidget { |
| 8 | + const Add({super.key}); |
| 9 | + |
| 10 | + @override |
| 11 | + AddScreen createState() => AddScreen(); |
| 12 | + |
| 13 | +} |
| 14 | +class AddScreen extends State<Add> { |
| 15 | + String name = ''; |
| 16 | + String gamepath = ''; |
| 17 | + int screenValue = 0; |
| 18 | + late TextEditingController nameController; |
| 19 | + |
| 20 | + @override |
| 21 | + void initState() { |
| 22 | + super.initState(); |
| 23 | + nameController = TextEditingController(text: name); |
| 24 | + } |
| 25 | + |
| 26 | + List<PageViewModel> getPages() { |
| 27 | + return [ |
| 28 | + PageViewModel( |
| 29 | + title: "Adding a game", |
| 30 | + body: "First off, type the game's name.", |
| 31 | + footer: Container ( |
| 32 | + margin: const EdgeInsets.only(left: 200.0, right: 200.0, top: 25.0), |
| 33 | + child: Column( |
| 34 | + children: [ |
| 35 | + TextField( |
| 36 | + controller: nameController, |
| 37 | + onChanged: (value) { |
| 38 | + setState(() { |
| 39 | + name = value; |
| 40 | + }); |
| 41 | + }, |
| 42 | + decoration: const InputDecoration( |
| 43 | + prefixIcon: Icon(FontAwesomeIcons.gamepad), |
| 44 | + filled: false, |
| 45 | + labelText: "Game name", |
| 46 | + hintText: "Write the game's name here.", |
| 47 | + ), |
| 48 | + ), |
| 49 | + ], |
| 50 | + ), |
| 51 | + ), |
| 52 | + image: const Center(child: Icon(FontAwesomeIcons.edit, size: 50.0)), |
| 53 | + ), |
| 54 | + PageViewModel( |
| 55 | + title: "Adding a game", |
| 56 | + body: "Please specify the game's executable's path.", |
| 57 | + footer: Container ( |
| 58 | + margin: const EdgeInsets.only(left: 200.0, right: 200.0, top: 25.0), |
| 59 | + child: Column( |
| 60 | + children: [ |
| 61 | + ElevatedButton( |
| 62 | + onPressed: () async { |
| 63 | + // Open file picker to select 8 Bit Fiesta's nw.exe |
| 64 | + FilePickerResult? result = await FilePicker.platform.pickFiles( |
| 65 | + type: FileType.custom, |
| 66 | + allowedExtensions: ['exe'], |
| 67 | + ); |
| 68 | + if (result != null) { |
| 69 | + setState(() { |
| 70 | + gamepath = result.files.single.path!; |
| 71 | + // Replace all backslashes with forward slashes for compatibility |
| 72 | + gamepath = gamepath.replaceAll('\\', '/'); |
| 73 | + }); |
| 74 | + } |
| 75 | + }, |
| 76 | + style: ElevatedButton.styleFrom( |
| 77 | + fixedSize: const Size(200, 50), |
| 78 | + ), |
| 79 | + child: const Text("Find the executable.", style: TextStyle(fontSize: 15.0)), |
| 80 | + ), |
| 81 | + Container( |
| 82 | + margin: const EdgeInsets.only(top: 10.0), |
| 83 | + child: Text(gamepath, style: const TextStyle(fontSize: 10.0)), |
| 84 | + ), |
| 85 | + ], |
| 86 | + ), |
| 87 | + ), |
| 88 | + image: const Center(child: Icon(FontAwesomeIcons.edit, size: 50.0)), |
| 89 | + ), |
| 90 | + PageViewModel( |
| 91 | + title: "And that's it!", |
| 92 | + body: "Let's get started!", |
| 93 | + image: const Center(child: Icon(FontAwesomeIcons.checkCircle, size: 100.0)), |
| 94 | + ), |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + Widget build(BuildContext context) { |
| 100 | + return IntroductionScreen( |
| 101 | + pages: getPages(), |
| 102 | + onDone: () async { |
| 103 | + |
| 104 | + if(name != '' && gamepath != ''){ |
| 105 | + testAdd(name, gamepath.toString()); |
| 106 | + GlobalVariables.addGame(name, gamepath.toString()); |
| 107 | + } |
| 108 | + Navigator.pushNamedAndRemoveUntil(context, '/main', (route) => false); |
| 109 | + ScaffoldMessenger.of(context).showSnackBar( |
| 110 | + SnackBar( |
| 111 | + content: Text('Game $name added!'), |
| 112 | + animation: CurvedAnimation(parent: const AlwaysStoppedAnimation(1), curve: Curves.easeInOut), |
| 113 | + behavior: SnackBarBehavior.floating, |
| 114 | + ), |
| 115 | + ); |
| 116 | + }, |
| 117 | + showSkipButton: screenValue == 0 ? true : false, |
| 118 | + showNextButton: true, |
| 119 | + showBackButton: screenValue == 0 ? false : true, |
| 120 | + back: const Text("Go Back"), |
| 121 | + overrideSkip: screenValue == 0 ? ElevatedButton( |
| 122 | + onPressed: () { |
| 123 | + Navigator.pushNamedAndRemoveUntil(context, '/main', (route) => false); |
| 124 | + }, |
| 125 | + style: ElevatedButton.styleFrom( |
| 126 | + fixedSize: const Size(100, 25), |
| 127 | + ), |
| 128 | + // If screenValue is 0, the skip button will be "Skip", else it will be "Cancel" |
| 129 | + child: Text(screenValue == 0 ? "Cancel" : "Go Back"), |
| 130 | + ) : null, |
| 131 | + onChange: (value) { |
| 132 | + setState(() { |
| 133 | + screenValue = value; |
| 134 | + }); |
| 135 | + }, |
| 136 | + next: const Text("Next"), |
| 137 | + done: const Text("Done!", style: TextStyle(fontWeight: FontWeight.w600)), |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + @override |
| 142 | + void dispose() { |
| 143 | + // Dispose of the TextEditingController when the widget is disposed |
| 144 | + nameController.dispose(); |
| 145 | + super.dispose(); |
| 146 | + } |
| 147 | +} |
0 commit comments