I'm trying to create a camera screen in my app that resembles a scanner apps screen. The final design should look something like this:
I was able to create the camera+layout screen by using CameraPreview widget and providing a layout child:
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: GestureDetector(
onLongPress: () async {
// some logic
},
child: Column(children: [
Stack(children: [
Text("Center your camera over the indicator ", style: TextStyle(color: Colors.white)),
Padding(
padding: EdgeInsets.only(top: 70.0),
child: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return CameraPreview(_controller,
child: MaterialPreviewOverlay(
overlayColor: overlayColor,
));
} else {
// Otherwise, display a loading indicator.
return const Center(child: CircularProgressIndicator());
}
},
),
)
])
])),
);
}
The MaterialPreviewOverlay doesn't have too much logic, mainly it calls the painter that looks like this:
@override
void paint(Canvas canvas, Size size) {
final screenRect = Rect.fromLTWH(0, 0, size.width, size.height);
final cutOutWidth = screenRect.width - 80;
final cutOutHeight = cutOutWidth * 1.7;
final cutOut = RRect.fromRectAndCorners(
Rect.fromCenter(
center: screenRect.center,
width: cutOutWidth,
height: cutOutHeight),
bottomLeft: const Radius.circular(40),
topRight: const Radius.circular(40),
topLeft: const Radius.circular(40)
);
final cutOutPath = Path.combine(PathOperation.difference,
Path()..addRect(screenRect), Path()..addRRect(cutOut));
final borderPaint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 5 // strokeWidth is painted 50/50 outwards and inwards.
..color = overlayColor;//.withAlpha(160);
canvas
..drawPath(cutOutPath, backgroundPaint)
..drawRRect(cutOut.deflate(borderPaint.strokeWidth / 2), borderPaint);
}
For some reason the Text widget isn't shown at all. I tried with Stack (like in the provided code example) and without. If I put the text below the FutureBuilder widget the text is shown (both with and without using Stack) and if I add an appBar to the Scaffold the text is also shown beneath the app bar. But I do not need the app bar, why wouldn't the text appear in my example?
