'Flutter Choose text on image
Thank you for your comment.
Actually my code as following.
import 'package:flutter/material.dart';
class Myapp1 extends StatefulWidget {
Myapp1({Key key}) : super(key: key);
@override
_Myapp1State createState() => _Myapp1State();
}
class _Myapp1State extends State<Myapp1> {
@override
Widget build(BuildContext context) {
return Stack(
children: [
Center(
child: Material(
child: InkWell(
onTap: () async {},
child: FittedBox(
child: SizedBox(child: Image.asset("assets/test.png"))),
),
),
),
CustomPaint(
size: MediaQuery.of(context).size,
painter: PathPainter(),
),
],
);
}
}
class PathPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.red
..style = PaintingStyle.stroke
..strokeWidth = 4.0;
Path path = Path();
path.addRect(
Rect.fromLTWH(size.width / 10, 305, size.width / 6, size.height / 20));
path.close();
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
When I run the code, the rectangle image is drawn hard on it.
When I click on A, I want to enclose option A in a rectangle, and when I click on B, I want to enclose option B in a rectangle.
Solution 1:[1]
List<String?> ischecked = [null, null, null, null];
Manage the state
Widget
List<String?> ischecked = [null, null, null, null];
class Page1 extends StatefulWidget {
Page1({Key? key}) : super(key: key);
@override
State<Page1> createState() => _Page1State();
}
class _Page1State extends State<Page1> {
var _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Column(
// direction: Axis.vertical,
children: [
CustomRadiobutton(
value: "Violet",
title: "Violet",
groupValue: ischecked[0],
onChanged: (v) {
setState(() {
ischecked[0] = "Violet";
ischecked[1] = null;
ischecked[2] = null;
});
}),
CustomRadiobutton(
value: "Red",
title: "Red",
groupValue: ischecked[1],
onChanged: (v) {
setState(() {
ischecked[0] = null;
ischecked[1] = "Red";
ischecked[2] = null;
});
}),
CustomRadiobutton(
value: "Yellow",
title: "Yellow",
groupValue: ischecked[2],
onChanged: (v) {
setState(() {
ischecked[0] = null;
ischecked[1] = null;
ischecked[2] = "Yellow";
});
}),
],
),
],
);
}
}
class CustomRadiobutton extends StatefulWidget {
var value;
var groupValue;
var onChanged;
var title;
CustomRadiobutton(
{Key? key, this.value, this.groupValue, this.title, this.onChanged})
: super(key: key);
@override
State<CustomRadiobutton> createState() => _CustomRadiobuttonState();
}
class _CustomRadiobuttonState extends State<CustomRadiobutton> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: Container(
decoration: widget.groupValue == null
? null
: BoxDecoration(
shape: BoxShape.rectangle,
border: Border.all(
color: Colors.red,
width: 2,
),
),
child: Wrap(
children: [
Radio(
// title: Text(widget.title),
value: widget.value,
groupValue: widget.groupValue,
onChanged: widget.onChanged),
Padding(
padding: const EdgeInsets.only(top: 8.0,right: 8.0),
child: Text(widget.title),
)
],
),
),
);
}
}
SAmpleCode Dartpad Live code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(title: 'Scanner'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
int myvalue = 0;
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
void initState() {}
Future<int> functions() async {
// do something here
return Future.value();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Scanner"),
backgroundColor: Colors.green,
),
body: Page1(),
);
}
}
List<String?> ischecked = [null, null, null, null];
class Page1 extends StatefulWidget {
Page1({Key? key}) : super(key: key);
@override
State<Page1> createState() => _Page1State();
}
class _Page1State extends State<Page1> {
var _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Column(
// direction: Axis.vertical,
children: [
CustomRadiobutton(
value: "Violet",
title: "Violet",
groupValue: ischecked[0],
onChanged: (v) {
setState(() {
ischecked[0] = "Violet";
ischecked[1] = null;
ischecked[2] = null;
});
}),
CustomRadiobutton(
value: "Red",
title: "Red",
groupValue: ischecked[1],
onChanged: (v) {
setState(() {
ischecked[0] = null;
ischecked[1] = "Red";
ischecked[2] = null;
});
}),
CustomRadiobutton(
value: "Yellow",
title: "Yellow",
groupValue: ischecked[2],
onChanged: (v) {
setState(() {
ischecked[0] = null;
ischecked[1] = null;
ischecked[2] = "Yellow";
});
}),
],
),
],
);
}
}
class CustomRadiobutton extends StatefulWidget {
var value;
var groupValue;
var onChanged;
var title;
CustomRadiobutton(
{Key? key, this.value, this.groupValue, this.title, this.onChanged})
: super(key: key);
@override
State<CustomRadiobutton> createState() => _CustomRadiobuttonState();
}
class _CustomRadiobuttonState extends State<CustomRadiobutton> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: Container(
decoration: widget.groupValue == null
? null
: BoxDecoration(
shape: BoxShape.rectangle,
border: Border.all(
color: Colors.red,
width: 2,
),
),
child: Wrap(
children: [
Radio(
// title: Text(widget.title),
value: widget.value,
groupValue: widget.groupValue,
onChanged: widget.onChanged),
Padding(
padding: const EdgeInsets.only(top: 8.0,right: 8.0),
child: Text(widget.title),
)
],
),
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lava |

