Instantly share code, notes, and snippets.
Last active
August 18, 2023 15:12
-
Save rileyhawk1417/b3808f6062a2b86a830209e16820d29c to your computer and use it in GitHub Desktop.
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
class UploadImageMenu extends StatefulWidget { | |
const UploadImageMenu({ | |
Key? key, | |
this.backgroundColor = Colors.white, | |
this.headerColor = Colors.black, | |
this.width = 300, | |
required this.onSubmitted, | |
required this.onUpload, | |
}) : super(key: key); | |
final Color backgroundColor; | |
final Color headerColor; | |
final double width; | |
final void Function(String text) onSubmitted; | |
final void Function(String text) onUpload; | |
@override | |
State<UploadImageMenu> createState() => _UploadImageMenuState(); | |
} | |
class _UploadImageMenuState extends State<UploadImageMenu> { | |
//This is the global value I need to share between two classes | |
String? _localImagePath; | |
@override | |
void initState() { | |
super.initState(); | |
_focusNode.requestFocus(); | |
} | |
@override | |
void dispose() { | |
_focusNode.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: widget.width, | |
height: 300, | |
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 10.0), | |
decoration: BoxDecoration( | |
color: widget.backgroundColor, | |
boxShadow: [ | |
BoxShadow( | |
blurRadius: 5, | |
spreadRadius: 1, | |
color: Colors.black.withOpacity(0.1), | |
), | |
], | |
// borderRadius: BorderRadius.circular(6.0), | |
), | |
child: DefaultTabController( | |
length: 2, | |
child: Column( | |
children: [ | |
Align( | |
alignment: Alignment.centerLeft, | |
child: SizedBox( | |
width: 300, | |
child: TabBar( | |
tabs: const [ | |
Tab(text: 'Upload Image'), | |
Tab(text: 'URL Image'), | |
], | |
labelColor: widget.headerColor, | |
unselectedLabelColor: Colors.grey, | |
indicatorColor: const Color(0xff00BCF0), | |
dividerColor: Colors.transparent, | |
onTap: (value) { | |
if (value == 1) { | |
_focusNode.requestFocus(); | |
} else { | |
_focusNode.unfocus(); | |
} | |
}, | |
), | |
), | |
), | |
Expanded( | |
child: TabBarView( | |
physics: const NeverScrollableScrollPhysics(), | |
children: [ | |
_buildFileTab(context), | |
_buildUrlTab(context), | |
], | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
Widget _buildFileTab(BuildContext context) { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
const SizedBox(height: 16.0), | |
//NOTE: The file uploader class has to set the image path | |
BuildFileUploadContainer( | |
context, | |
filePicker: _filePicker, | |
//NOTE: Not sure on how to pass values between classes | |
), | |
// _buildFileUploadContainer(context), | |
const SizedBox(height: 18.0), | |
//TODO: Share value to button | |
Align( | |
alignment: Alignment.centerRight, | |
//NOTE: The submit button has to receive that value | |
child: BuildUploadButton( | |
context, | |
localImagePath: _localImagePath, | |
onUpload: widget.onUpload, | |
textEditingController: _textEditingController, | |
), | |
), | |
], | |
); | |
} | |
class BuildFileTab extends StatefulWidget { | |
const BuildFileTab( | |
{required this.filePicker, | |
required this.onUpload, | |
required this.textEditingController, | |
super.key}); | |
final void Function(String text) onUpload; | |
final FilePicker filePicker; | |
final TextEditingController textEditingController; | |
@override | |
State<BuildFileTab> createState() => _BuildFileTab(); | |
} | |
class _BuildFileTab extends State<BuildFileTab> { | |
@override | |
Widget build(context) { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
const SizedBox(height: 16.0), | |
BuildFileUploadContainer( | |
context, | |
filePicker: widget.filePicker, | |
), | |
// _buildFileUploadContainer(context), | |
const SizedBox(height: 18.0), | |
Align( | |
alignment: Alignment.centerRight, | |
child: BuildUploadButton( | |
context, | |
localImagePath: BuildFileTab.sampleFile, | |
onUpload: widget.onUpload, | |
textEditingController: widget.textEditingController, | |
), | |
), | |
], | |
); | |
} | |
} | |
class BuildFileUploadContainer extends StatefulWidget { | |
BuildFileUploadContainer( | |
BuildContext context, { | |
required this.filePicker, | |
required this.importedFile, | |
super.key, | |
}); | |
final FilePicker filePicker; | |
String? importedFile; | |
@override | |
State<BuildFileUploadContainer> createState() => _BuildFileUploadContainer(); | |
} | |
class _BuildFileUploadContainer extends State<BuildFileUploadContainer> { | |
final allowedExtensions = ['jpg', 'png', 'jpeg']; | |
@override | |
Widget build(context) { | |
return Expanded( | |
child: MouseRegion( | |
cursor: SystemMouseCursors.click, | |
child: GestureDetector( | |
onTap: () async { | |
final result = await widget.filePicker.pickFiles( | |
dialogTitle: '', | |
allowMultiple: false, | |
type: fp.FileType.image, | |
allowedExtensions: allowedExtensions, | |
); | |
if (result != null && result.files.isNotEmpty) { | |
setState(() { | |
localImagePath = result.files.first.path; | |
}); | |
} | |
}, | |
child: Container( | |
height: 80, | |
margin: const EdgeInsets.all(10.0), | |
decoration: BoxDecoration( | |
border: Border.all(color: const Color(0xff00BCF0)), | |
borderRadius: BorderRadius.circular(12.0), | |
), | |
child: localImagePath != null | |
? FileBox( | |
localImagePath: localImagePath!, | |
) | |
: const BuildImageSelectorBox(), | |
), | |
), | |
), | |
); | |
} | |
} | |
class BuildUploadButton extends StatefulWidget { | |
const BuildUploadButton( | |
BuildContext context, { | |
required this.localImagePath, | |
required this.onUpload, | |
required this.textEditingController, | |
super.key, | |
}); | |
final String? localImagePath; | |
final void Function(String text) onUpload; | |
final TextEditingController textEditingController; | |
@override | |
State<BuildUploadButton> createState() => _BuildUploadButton(); | |
} | |
class _BuildUploadButton extends State<BuildUploadButton> { | |
@override | |
Widget build(context) { | |
return SizedBox( | |
width: 170, | |
height: 48, | |
child: TextButton( | |
style: ButtonStyle( | |
backgroundColor: MaterialStateProperty.all(const Color(0xFF00BCF0)), | |
shape: MaterialStateProperty.all<RoundedRectangleBorder>( | |
RoundedRectangleBorder( | |
borderRadius: BorderRadius.circular(12.0), | |
), | |
), | |
), | |
onPressed: () async { | |
if (widget.localImagePath != null) { | |
widget.onUpload( | |
widget.localImagePath!, | |
); | |
} else if (validateUrl(widget.textEditingController.text)) { | |
widget.onUpload( | |
widget.textEditingController.text, | |
); | |
} else { | |
setState(() { | |
isUrlValid = false; | |
}); | |
} | |
}, | |
child: Text( | |
'Upload', | |
style: TextStyle( | |
color: Theme.of(context).colorScheme.onPrimary, | |
fontSize: 14.0, | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment