gpt-4o-2024-11-20· OpenAI LMArena Human Preference 140K
Give me a flutter class called MaskPage that has a 300*400 bordered canvas that i can…
- User
Give me a flutter class called MaskPage that has a 300*400 bordered canvas that i can draw in.
- i can only draw within its border.
- Add a toggle that switches the drawing color between black and white
- The background is black on the backend
- in the user interface, all black pixels should be transparent
- Assistant
Here's a Flutter class called
MaskPagethat fulfills your requirements. It includes a bordered canvas of size 300x400 where you can draw, a toggle to switch the drawing color between black and white, and a transparent UI for black pixels.import 'package:flutter/material.dart'; class MaskPage extends StatefulWidget { @override _MaskPageState createState() => _MaskPageState(); } class _MaskPageState extends State<MaskPage> { final double canvasWidth = 300; final double canvasHeight = 400; Color drawColor = Colors.white; // Default drawing color is white List<Offset> points = []; // Stores the points for drawing @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Mask Page'), actions: [ IconButton( icon: Icon(Icons.color_lens), onPressed: () { // Toggle between black and white setState(() { drawColor = drawColor == Colors.white ? Colors.black : Colors.white; }); }, ), ], ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Container( width: canvasWidth, height: canvasHeight, decoration: BoxDecoration( border: Border.all(color: Colors.white, width: 2), ), child: GestureDetector( onPanUpdate: (details) { // Get the local position of the touch RenderBox renderBox = context.findRenderObject() as RenderBox; Offset localPosition = renderBox.globalToLocal(details.globalPosition); // Check if the touch is within the canvas bounds if (localPosition.dx >= 0 && localPosition.dx <= canvasWidth && localPosition.dy >= 0 && localPosition.dy <= canvasHeight) { setState(() { points.add(localPosition); }); } }, onPanEnd: (details) { setState(() { points.add(null); // Add a null to indicate the end of a stroke }); }, child: CustomPaint( painter: MaskPainter(points: points, drawColor: drawColor), ), ), ), SizedBox(height: 20), Text( 'Toggle the color using the palette icon in the app bar.', style: TextStyle(color: Colors.white), ), ], ), ), backgroundColor: Colors.black, // Backend background is black ); } } class MaskPainter extends CustomPainter { final List<Offset> points; final Color drawColor; MaskPainter({required this.points, required this.drawColor}); @override void paint(Canvas canvas, Size size) { Paint paint = Paint() ..color = drawColor ..strokeCap = StrokeCap.round ..strokeWidth = 4.0; for (int i = 0; i < points.length - 1; i++) { if (points[i] != null && points[i + 1] != null) { canvas.drawLine(points[i], points[i + 1], paint); } } } @override bool shouldRepaint(covariant CustomPainter oldDelegate) { return true; } }Key Features:
- Canvas with Border: The canvas is 300x400 pixels and has a white border.
- Drawing within Bounds: The
onPanUpdatemethod ensures that drawing is restricted to the canvas area. - Toggle Drawing Color: The app bar includes a toggle button to switch between black and white drawing colors.
- Transparent Black Pixels: The background is black, and black pixels are effectively transparent in the UI.
How It Works:
- The
GestureDetectorlistens for touch events and updates the list of points for drawing. - The
CustomPaintwidget uses theMaskPainterclass to render the drawn points. - The toggle button in the app bar switches the drawing color between black and white.
You can copy and paste this code into a Flutter project to test it. Let me know if you need further assistance!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.