-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add camera state to help with camera preview orientation + fix
- Loading branch information
Showing
3 changed files
with
139 additions
and
15 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
part of smashlibs; | ||
|
||
/// Helper class in cases of advanced camera use. | ||
/// Make sue yourt app initialises the init of thsi as follows at app startup: | ||
/// | ||
// class HomeWidgetState extends State<WelcomeWidget> | ||
// with WidgetsBindingObserver { | ||
// CameraState? cameraState; | ||
// | ||
// @override | ||
// void initState() { | ||
// cameraState = Provider.of<CameraState>(context, listen: false); | ||
// | ||
// super.initState(); | ||
// WidgetsBinding.instance.addObserver(this); | ||
// | ||
// ... | ||
// } | ||
// | ||
// @override | ||
// void didChangeDependencies() { | ||
// super.didChangeDependencies(); | ||
// if (cameraState != null) { | ||
// cameraState!.init(context); | ||
// } | ||
// } | ||
// | ||
// @override | ||
// void dispose() { | ||
// WidgetsBinding.instance.removeObserver(this); | ||
// super.dispose(); | ||
// } | ||
// ... | ||
// | ||
class CameraState extends ChangeNotifierPlus { | ||
DeviceOrientation? startupOrientation; | ||
|
||
void init(BuildContext context) { | ||
if (startupOrientation != null) { | ||
return; | ||
} | ||
startupOrientation = getOrientation(context); | ||
} | ||
|
||
void onChanged() { | ||
notifyListenersMsg("camera state changed"); | ||
} | ||
|
||
DeviceOrientation getOrientation(BuildContext context, | ||
{bool setPreferred = false}) { | ||
DeviceOrientation initialOrientation; | ||
final orientation = MediaQuery.of(context).orientation; | ||
if (orientation == Orientation.portrait) { | ||
if (setPreferred) { | ||
SystemChrome.setPreferredOrientations( | ||
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); | ||
} | ||
var view = View.of(context); | ||
view.viewInsets.bottom == 0 | ||
? initialOrientation = DeviceOrientation.portraitUp | ||
: initialOrientation = DeviceOrientation.portraitDown; | ||
} else { | ||
if (setPreferred) { | ||
SystemChrome.setPreferredOrientations([ | ||
DeviceOrientation.landscapeLeft, | ||
DeviceOrientation.landscapeRight | ||
]); | ||
} | ||
|
||
initialOrientation = | ||
MediaQuery.of(context).size.width > MediaQuery.of(context).size.height | ||
? DeviceOrientation.landscapeLeft | ||
: DeviceOrientation.landscapeRight; | ||
} | ||
return initialOrientation; | ||
} | ||
} |
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