Flutter:无法打开 CustomPainter class
Flutter: can´t open the CustomPainter class
我在实施 CustomPainter
class 时遇到问题,不知道我在每个教程中的错误,他们和我一样,希望有人知道解决方案。
class _MyHomepageState extends State<MyHomepage> {
var _name;
final nameCon = new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'App',
style: TextStyle(fontWeight: FontWeight.w900),
),
backgroundColor: Colors.brown[500],
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: nameCon,
decoration: InputDecoration(hintText: "Name"),
),
RaisedButton(
onPressed: () {
setState(() {
_name = nameCon.text;
});
},
child: Text("Los"),
),
Text("hier $_name "),
Expanded(
child: LayoutBuilder(
builder: (_, constraints) => Container(
width: constraints.widthConstraints().maxWidth,
height: constraints.heightConstraints().maxHeight,
color: Colors.yellow,
child: CustomPaint(painter: Thermometer()),
),
),
),
],
),
),
),
);
}
}
class Thermometer extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.brown
..style = PaintingStyle.stroke
..style=PaintingStyle.fill
..strokeWidth = 4;
canvas.drawRRect(
RRect.fromRectAndRadius(Rect.fromLTWH(-140, -60, 40, 290),
Radius.circular(20)),
paint,);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false ;
}
您可以复制粘贴 运行 完整代码
请将 left
从 -140
更改为 0
或其他值
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(0, -60, 40, 290), Radius.circular(20)),
paint,
);
工作演示
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomepage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomepage extends StatefulWidget {
MyHomepage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomepageState createState() => _MyHomepageState();
}
class _MyHomepageState extends State<MyHomepage> {
var _name;
final nameCon = new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'App',
style: TextStyle(fontWeight: FontWeight.w900),
),
backgroundColor: Colors.brown[500],
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: nameCon,
decoration: InputDecoration(hintText: "Name"),
),
RaisedButton(
onPressed: () {
setState(() {
_name = nameCon.text;
});
},
child: Text("Los"),
),
Text("hier $_name "),
Expanded(
child: LayoutBuilder(
builder: (_, constraints) => Container(
width: constraints.widthConstraints().maxWidth,
height: constraints.heightConstraints().maxHeight,
color: Colors.yellow,
child: CustomPaint(painter: Thermometer()),
),
),
),
],
),
),
),
);
}
}
class Thermometer extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.brown
..style = PaintingStyle.stroke
..style = PaintingStyle.fill
..strokeWidth = 4;
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(0, -60, 40, 290), Radius.circular(20)),
paint,
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
我在实施 CustomPainter
class 时遇到问题,不知道我在每个教程中的错误,他们和我一样,希望有人知道解决方案。
class _MyHomepageState extends State<MyHomepage> {
var _name;
final nameCon = new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'App',
style: TextStyle(fontWeight: FontWeight.w900),
),
backgroundColor: Colors.brown[500],
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: nameCon,
decoration: InputDecoration(hintText: "Name"),
),
RaisedButton(
onPressed: () {
setState(() {
_name = nameCon.text;
});
},
child: Text("Los"),
),
Text("hier $_name "),
Expanded(
child: LayoutBuilder(
builder: (_, constraints) => Container(
width: constraints.widthConstraints().maxWidth,
height: constraints.heightConstraints().maxHeight,
color: Colors.yellow,
child: CustomPaint(painter: Thermometer()),
),
),
),
],
),
),
),
);
}
}
class Thermometer extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.brown
..style = PaintingStyle.stroke
..style=PaintingStyle.fill
..strokeWidth = 4;
canvas.drawRRect(
RRect.fromRectAndRadius(Rect.fromLTWH(-140, -60, 40, 290),
Radius.circular(20)),
paint,);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false ;
}
您可以复制粘贴 运行 完整代码
请将 left
从 -140
更改为 0
或其他值
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(0, -60, 40, 290), Radius.circular(20)),
paint,
);
工作演示
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomepage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomepage extends StatefulWidget {
MyHomepage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomepageState createState() => _MyHomepageState();
}
class _MyHomepageState extends State<MyHomepage> {
var _name;
final nameCon = new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'App',
style: TextStyle(fontWeight: FontWeight.w900),
),
backgroundColor: Colors.brown[500],
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: nameCon,
decoration: InputDecoration(hintText: "Name"),
),
RaisedButton(
onPressed: () {
setState(() {
_name = nameCon.text;
});
},
child: Text("Los"),
),
Text("hier $_name "),
Expanded(
child: LayoutBuilder(
builder: (_, constraints) => Container(
width: constraints.widthConstraints().maxWidth,
height: constraints.heightConstraints().maxHeight,
color: Colors.yellow,
child: CustomPaint(painter: Thermometer()),
),
),
),
],
),
),
),
);
}
}
class Thermometer extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.brown
..style = PaintingStyle.stroke
..style = PaintingStyle.fill
..strokeWidth = 4;
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(0, -60, 40, 290), Radius.circular(20)),
paint,
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}