1_自定义控件
1.1_组合控件
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: CombinationWidget(Colors.blue,200.0,200.0),
);
}
}
class CombinationWidget extends StatefulWidget {
@required
Color color;
@required
double width;
@required
double height;
CombinationWidget(this.color, this.width, this.height);
@override
State createState() {
return _CombinationWidgetState(color, width, height);
}
}
class _CombinationWidgetState extends State {
Color _color;
double _width;
double _height;
var _startCount = 0;
_CombinationWidgetState(this._color, this._width, this._height);
@override
Widget build(BuildContext context) {
return Center(
child: ClipOval(
child: Container(
color: _color,
width: _width,
height: _height,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: const Icon(
Icons.thumb_up,
color: Colors.white,
),
onPressed: () {
setState(() {
_startCount += 1;
});
}),
Text(
_startCount.toString(),
style: const TextStyle(fontSize: 25, color: Colors.red),
)
],
),
),
),
);
}
}
1.2_自绘控件
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: const WheelWidget(),
);
}
}
class WheelWidget extends StatelessWidget {
const WheelWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomPaint(
size: const Size(200, 200),
painter: WheelPaint(),
);
}
}
class WheelPaint extends CustomPainter {
Paint getColorPaint(Color color) {
Paint paint = Paint();
paint.color = color;
return paint;
}
@override
void paint(Canvas canvas, Size size) {
double wheelSize = min(size.width, size.height) / 2;
double nbElem = 6;
double radius = (2 * pi) / nbElem;
Rect boundingRect = Rect.fromCircle(center: Offset(wheelSize, wheelSize), radius: wheelSize);
canvas.drawArc(boundingRect, 0, radius, true, getColorPaint(Colors.orange));
canvas.drawArc(boundingRect, radius, radius, true, getColorPaint(Colors.black38));
canvas.drawArc(boundingRect, radius * 2, radius, true, getColorPaint(Colors.green));
canvas.drawArc(boundingRect, radius * 3, radius, true, getColorPaint(Colors.red));
canvas.drawArc(boundingRect, radius * 4, radius, true, getColorPaint(Colors.blue));
canvas.drawArc(boundingRect, radius * 5, radius, true, getColorPaint(Colors.pink));
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => oldDelegate != this;
}
2_自定义插件