I need to take a screenshot of the current screen or widget and I need to write it into a file.
This answer applies if you want to take a screenshot of the widget outside of your dart code.
To take a screenshot of your widget, make sure your dart file has a void main() async { function.
From the terminal, go to your app folder, and run your widget, like this:
flutter run lib/my_test.dart
Or if you want to run your entire app, just run:
flutter run
Then your widget will start.
Press the s key to print a screenshot.
You will see a message like this:
Taking screenshot for SM S757BL... 1,580ms Screenshot written to flutter_01.png (38kB).
The image is saved in your app folder.