使用Flutter进行自定义绘制

huangapple go评论72阅读模式
英文:

Custom paint with flutter

问题

我有一个任务设计,这个图像在Figma文件中。

我想自定义绘制与相同的图像,但我不知道如何自定义绘制,因为我没有理想。请帮助我,我想变得更好,完成分配的工作。

谢谢你的帮助。

英文:

使用Flutter进行自定义绘制

I have a task design, this image in figma file.

I want to custom paint as the same image, but I don't an ideal because I don't know custom paint. Please help me, I want to become better and I want to complete the assigned work.

Thank you for your help.

答案1

得分: 2

以下是您要翻译的代码部分:

import 'package:flutter/material.dart';

void main() {
  runApp(const 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(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final textEditingController = TextEditingController();
  final key = GlobalKey<State<TextField>>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: SizedBox.square(
          dimension: 400,
          child: CustomPaint(
            painter: CustomObjectPainter(
              const LinearGradient(
                  colors: [Color(0xFF9DCF45), Color(0xFF729930)],
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter),
            ),
          ),
        ),
      ),
    );
  }
}

class CustomObjectPainter extends CustomPainter {
  final Gradient gradient;

  CustomObjectPainter(this gradient);

  @override
  void paint(Canvas canvas, Size size) {
    canvas.save();
    final radius = size.width * 0.1;
    final paint = Paint();
    final rect = Rect.fromPoints(size.topLeft(Offset.zero),
        size.bottomRight(Offset(-size.width * 0.1, 0)));
    paint.shader = gradient.createShader(rect);
    final rrect = RRect.fromRectAndCorners(rect,
        bottomLeft: Radius.circular(radius),
        bottomRight: Radius.circular(radius));
    canvas.drawRRect(rrect, paint);

    final width = size.width * 0.2;
    final height = size.height * 0.2;

    final topMargin = size.height * 0.1;
    final smallRect = Rect.fromCenter(
        center: Offset(size.width * 0.9, topMargin + height / 2),
        width: width,
        height: height);
    final smallRRect =
        RRect.fromRectAndRadius(smallRect, Radius.circular(radius / 4));
    canvas.drawRRect(smallRRect, paint);
    paint.style = PaintingStyle.stroke;
    paint.shader = null;
    paint.color = Colors.white;
    paint.strokeWidth = 5;
    canvas.drawRRect(smallRRect, paint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return !(oldDelegate is CustomObjectPainter &&
        oldDelegate.gradient == gradient);
  }
}

输出:
使用Flutter进行自定义绘制

英文:

You can use CustomPainter in following way

import &#39;package:flutter/material.dart&#39;;

void main() {
  runApp(const 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(
      title: &#39;Flutter Demo&#39;,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  final textEditingController = TextEditingController();
  final key = GlobalKey&lt;State&lt;TextField&gt;&gt;();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: SizedBox.square(
          dimension: 400,
          child: CustomPaint(
            painter: CustomObjectPainter(
              const LinearGradient(
                  colors: [Color(0xFF9DCF45), Color(0xFF729930)],
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter),
            ),
          ),
        ),
      ),
    );
  }
}

class CustomObjectPainter extends CustomPainter {
  final Gradient gradient;

  CustomObjectPainter(this.gradient);

  @override
  void paint(Canvas canvas, Size size) {
    canvas.save();
    final radius = size.width * 0.1;
    final paint = Paint();
    final rect = Rect.fromPoints(size.topLeft(Offset.zero),
        size.bottomRight(Offset(-size.width * 0.1, 0)));
    paint.shader = gradient.createShader(rect);
    final rrect = RRect.fromRectAndCorners(rect,
        bottomLeft: Radius.circular(radius),
        bottomRight: Radius.circular(radius));
    canvas.drawRRect(rrect, paint);

    final width = size.width * 0.2;
    final height = size.height * 0.2;

    final topMargin = size.height * 0.1;
    final smallRect = Rect.fromCenter(
        center: Offset(size.width * 0.9, topMargin + height / 2),
        width: width,
        height: height);
    final smallRRect =
        RRect.fromRectAndRadius(smallRect, Radius.circular(radius / 4));
    canvas.drawRRect(smallRRect, paint);
    paint.style = PaintingStyle.stroke;
    paint.shader = null;
    paint.color = Colors.white;
    paint.strokeWidth = 5;
    canvas.drawRRect(smallRRect, paint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return !(oldDelegate is CustomObjectPainter &amp;&amp;
        oldDelegate.gradient == gradient);
  }
}

Output:
使用Flutter进行自定义绘制

huangapple
  • 本文由 发表于 2023年3月7日 16:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定