首页 > 建站教程 > dart >  dart笔记5:重构override+画圆正文

dart笔记5:重构override+画圆

dart笔记5:重构override+画圆
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'override重构',
      home: Scaffold(
        appBar: AppBar(
          title: Text('override重构'),
        ),
        body: Center(
          child: SizedBox(
            width: 500.0,
            height: 500.0,
            child: CustomPaint(
              painter: CirclePainter(),
              child: Center(
                child: Text(
                  '绘制圆',
                  style: TextStyle(
                    fontSize: 38.0,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class CirclePainter extends CustomPainter {
  Paint _paint = Paint()
    ..color = Colors.grey
    ..strokeCap = StrokeCap.square
    ..isAntiAlias = true
    ..strokeWidth = 3.0
    ..style = PaintingStyle.stroke;

  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawCircle(Offset(200.0, 150.0), 150.0, _paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}