Flutter形状转换插件shape_to_go的使用

Flutter形状转换插件shape_to_go的使用

通过 shape_to_go 插件,现在可以轻松创建基本形状。

示例

ClipPath(
    clipper: Potter(points: [
        const TopLeftEdge(),
        const PointOnShap(xDivider: 2, yDivider: 3),
        const TopRightEdge(),
        const PointOnShap(xDivider: 3 / 2, yDivider: 2),
        const BottomRightEdge(),
        const PointOnShap(xDivider: 2, yDivider: 3 / 2),
        const BottomLeftEdge(),
        const PointOnShap(xDivider: 3, yDivider: 2),
    ]),
    child: Container(
        color: Colors.green,
        width: 342,
        height: 357,
    ),
),

完整示例

以下是使用 shape_to_go 创建多种形状的完整示例代码:

import 'package:flutter/material.dart';
import 'package:shape_to_go/shape_to_go.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Example',
      theme: ThemeData.dark(),
      home: const Example()
    );
  }
}

class Example extends StatelessWidget {
  const Example({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            const SizedBox(height: 40,),
            SizedBox(
              width: double.maxFinite,
              height: 357,
              child: Stack(
                children: [
                  Positioned(
                    left: (MediaQuery.of(context).size.width - 342) / 2,
                    child: ClipPath(
                      clipper: Potter(points: [
                        const TopLeftCorner(),
                        const TopRightCorner(),
                        const PointOnRightSide(divider: 1.5),
                        const BottomCenterCorner(),
                        const PointOnLeftSide(divider: 1.5),
                      ],
                      radius: const Radius.circular(10)
                      ),
                      child: Container(
                        color: Colors.purple,
                        width: 342,
                        height: 200,
                      ),
                    ),
                  ),
                  Positioned(
                    left: (MediaQuery.of(context).size.width - 342) / 2,
                    bottom: 0,
                    child: ClipPath(
                      clipper: Potter(points: [
                        const TopLeftCorner(radius: Radius.circular(30)),
                        const PointOnRightSide(divider: 2.5),
                        const BottomRightCorner(),
                        const BottomLeftCorner(),
                      ],
                      radius: const Radius.circular(10)
                      ),
                      child: Container(
                        color: Colors.cyan,
                        width: 163,
                        height: 200,
                      ),
                    ),
                  ),
                  Positioned(
                    right: (MediaQuery.of(context).size.width - 342) / 2,
                    bottom: 0,
                    child: ClipPath(
                      clipper: Potter(points: [
                        const PointOnLeftSide(divider: 2.5),
                        const TopRightCorner(radius: Radius.circular(30)),
                        const BottomRightCorner(),
                        const BottomLeftCorner(),
                      ],
                      radius: const Radius.circular(10)
                      ),
                      child: Container(
                        color: Colors.deepPurple,
                        width: 163,
                        height: 200,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 20,),
            ClipPath( 
              clipper: Potter(points: [
                const TopCenterCorner(),
                const PointOnShap(xDivider: (8 / 5), yDivider: (8 / 3)),
                const CenterRightCorner(),
                const PointOnShap(xDivider: (8 / 5), yDivider: (8 / 5)),
                const BottomCenterCorner(),
                const PointOnShap(xDivider: (8 / 3), yDivider: (8 / 5)),
                const CenterLeftCorner(),
                const PointOnShap(xDivider: (8 / 3), yDivider: (8 / 3)),
              ]
              ),
              child: Container(
                color: Colors.blue,
                width: 342,
                height: 357,
              ),
            ),
            const SizedBox(height: 20,),
            ClipPath( 
              clipper: Potter(points: [
                const TopLeftCorner(),
                const CenterPoint(),
                const TopRightCorner(),
                const BottomRightCorner(),
                const BottomLeftCorner(),
              ]
              ),
              child: Container(
                color: Colors.red,
                width: 342,
                height: 357,
              ),
            ),
            const SizedBox(height: 20,),
            ClipPath( 
              clipper: Potter(points: [
                const TopLeftCorner(),
                const PointOnShap(xDivider: 2, yDivider: 3),
                const TopRightCorner(),
                const PointOnShap(xDivider: 3 / 2, yDivider: 2),
                const BottomRightCorner(),
                const PointOnShap(xDivider: 2, yDivider: 3 / 2),
                const BottomLeftCorner(),
                const PointOnShap(xDivider: 3, yDivider: 2),
              ]
              ),
              child: Container(
                color: Colors.green,
                width: 342,
                height: 357,
              ),
            ),
            const SizedBox(height: 20,),
            ClipPath( 
              clipper: Potter.messageShapeRight(radius: const Radius.circular(20)),
              child: Container(
                color: Colors.green,
                width: 342,
                height: 100,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter形状转换插件shape_to_go的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter形状转换插件shape_to_go的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter中使用shape_to_go插件进行形状转换的一个简单示例。shape_to_go插件允许开发者在Flutter应用中创建和转换各种形状。为了演示其使用,我们首先需要确保已经在pubspec.yaml文件中添加了依赖项:

dependencies:
  flutter:
    sdk: flutter
  shape_to_go: ^最新版本号 # 请替换为实际的最新版本号

然后运行flutter pub get来安装依赖。

以下是一个简单的示例代码,展示如何使用shape_to_go插件来创建一个从圆形渐变到矩形的动画效果:

import 'package:flutter/material.dart';
import 'package:shape_to_go/shape_to_go.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Shape Transition with shape_to_go'),
        ),
        body: Center(
          child: ShapeTransitionExample(),
        ),
      ),
    );
  }
}

class ShapeTransitionExample extends StatefulWidget {
  @override
  _ShapeTransitionExampleState createState() => _ShapeTransitionExampleState();
}

class _ShapeTransitionExampleState extends State<ShapeTransitionExample> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    )..repeat(reverse: true);

    _animation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      child: Container(
        color: Colors.transparent,
      ),
      builder: (context, child) {
        return ShapeBorderTween(
          begin: CircleBorder(),
          end: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(0.0),
          ),
        ).animate(_animation).build(context) == null
            ? child
            : Container(
                decoration: ShapeDecoration(
                  shape: ShapeBorderTween(
                    begin: CircleBorder(),
                    end: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(0.0),
                    ),
                  ).animate(_animation).build(context) as ShapeBorder,
                  color: Colors.blue,
                ),
                width: 200,
                height: 200,
              );
      },
    );
  }
}

在这个示例中,我们做了以下几件事:

  1. 引入必要的包flutter/material.dartshape_to_go/shape_to_go.dart
  2. 设置应用结构:创建一个简单的Flutter应用,其中包含一个主屏幕。
  3. 创建动画控制:使用AnimationControllerTween来控制动画。在这个例子中,动画将在2秒内从0.0到1.0反复进行。
  4. 使用ShapeBorderTween:这是shape_to_go插件提供的一个类,用于在形状之间创建动画过渡。在这个例子中,它从CircleBorder(圆形)过渡到RoundedRectangleBorder(矩形,但边角半径为0,因此看起来像一个普通的矩形)。
  5. 使用AnimatedBuilder:用于构建动画效果,它监听动画的变化并相应地更新UI。

请注意,shape_to_go插件的API可能会随着版本更新而变化,因此请查阅最新的文档以获取最准确的信息。此外,由于shape_to_go插件的具体实现可能有所不同,上述代码可能需要根据插件的实际API进行调整。

回到顶部