Flutter自定义背景插件funky_backgrounds的使用

Flutter自定义背景插件funky_backgrounds的使用

Funky Backgrounds 是一个使用 CustomPainter 创建的背景集合,非常有趣。

如何使用

使用 funky_backgrounds 插件非常简单。只需要将视图或小部件包裹在 FunkyBackground 中,并选择一个可用的背景绘制器即可。

return Scaffold(
  body: const FunkyBackground(
    painter: FunkyStretchedTriangles(Colors.cyan),
    child: MyPage(),
  ),
);

如果你想在一个定位布局(例如 Stack)中使用 funky_backgrounds,可以使用 PositionedFunkyBackground 小部件。

特性

以下是一些可用的背景绘制器的截图。

Conic Curve Rotating Lines Bezier Lines Horizontal Triangles
![conic curve] ![rotating lines] ![bezier lines] ![horizontal triangles]
Shifted Triangle Lateral Triangles Stretched Triangles Square Grid
![shifted triangle] ![lateral triangles] ![stretched triangles] ![square grid]

示例代码

下面是一个完整的示例代码,展示了如何使用 funky_backgrounds 插件来创建不同的背景效果。

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

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

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

  // 这是你的应用的根小部件。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Funky Backgrounds',
      theme: ThemeData(
        primarySwatch: Colors.pink,
      ),
      home: const MyHomePage(title: 'Funky Backgrounds'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    super.key,
    required this.title,
  });

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool hideContents = false;
  bool hideButtons = false;
  FunkyBackgroundPainter painter = NotFunkyBackgroundPainter();

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: [
          IconButton(
            onPressed: () {
              setState(() {
                hideButtons = !hideButtons;
              });
            },
            icon: Icon(
              hideButtons ? Icons.radio_button_off : Icons.radio_button_checked,
            ),
          ),
          IconButton(
            onPressed: () {
              setState(() {
                hideContents = !hideContents;
              });
            },
            icon: Icon(
              hideContents ? Icons.visibility_off : Icons.visibility,
            ),
          ),
        ],
      ),
      body: Center(
        child: FunkyBackground(
          painter: painter,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Expanded(
                child: ListView.separated(
                  itemCount: 100,
                  itemBuilder: (context, index) {
                    return Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16.0),
                      child: hideContents
                          ? const SizedBox(height: 64)
                          : Card(
                              child: Padding(
                                padding: const EdgeInsets.all(32.0),
                                child: Center(child: Text('Item $index')),
                              ),
                            ),
                    );
                  },
                  separatorBuilder: (_, __) => const SizedBox(height: 32),
                ),
              ),
              if (!hideButtons)
                SizedBox(
                  height: 50,
                  child: ListView(
                    scrollDirection: Axis.horizontal,
                    children: [
                      const SizedBox(width: 16),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            painter = FunkyConicCurve(Colors.red[200]!);
                          });
                        },
                        child: const Text('FunkyConicCurve'),
                      ),
                      const SizedBox(width: 16),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            painter = const FunkyRotatingLines(Colors.blue);
                          });
                        },
                        child: const Text('FunkyRotatingLines'),
                      ),
                      const SizedBox(width: 16),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            painter = const FunkySquareGrid(Colors.deepOrange);
                          });
                        },
                        child: const Text('FunkySquareGrid'),
                      ),
                      const SizedBox(width: 16),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            painter = FunkyBezierLines(Colors.pink[100]!);
                          });
                        },
                        child: const Text('FunkyBezierLines'),
                      ),
                      const SizedBox(width: 16),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            painter = FunkyHorizontalTriangles(
                              Colors.green[200]!,
                            );
                          });
                        },
                        child: const Text('FunkyHorizontalTriangles'),
                      ),
                      const SizedBox(width: 16),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            painter = FunkyShiftedTriangle(Colors.purple[200]!);
                          });
                        },
                        child: const Text('FunkyShiftedTriangle'),
                      ),
                      const SizedBox(width: 16),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            painter = FunkyLateralTriangles(Colors.yellow[200]!);
                          });
                        },
                        child: const Text('FunkyLateralTriangles'),
                      ),
                      const SizedBox(width: 16),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            painter = const FunkyStretchedTriangles(Colors.cyan);
                          });
                        },
                        child: const Text('FunkyStretchedTriangles'),
                      ),
                      const SizedBox(width: 16),
                    ],
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}

// 自定义背景绘制器
class NotFunkyBackgroundPainter extends FunkyBackgroundPainter {
  [@override](/user/override)
  void paint(Canvas canvas, Size size) {}
}

更多关于Flutter自定义背景插件funky_backgrounds的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义背景插件funky_backgrounds的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


funky_backgrounds 是一个 Flutter 插件,用于在应用中创建自定义的、动态的背景效果。它提供了多种背景动画和效果,可以帮助你为应用增添一些视觉上的吸引力。

安装 funky_backgrounds 插件

首先,你需要在 pubspec.yaml 文件中添加 funky_backgrounds 依赖:

dependencies:
  flutter:
    sdk: flutter
  funky_backgrounds: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来安装插件。

使用 funky_backgrounds

funky_backgrounds 提供了一个 FunkyBackground 小部件,你可以将它添加到你的应用中的任何地方。以下是一个简单的示例,展示如何使用 FunkyBackground

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Funky Backgrounds Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Funky Backgrounds'),
      ),
      body: FunkyBackground(
        child: Center(
          child: Text(
            'Hello, Funky Backgrounds!',
            style: TextStyle(
              fontSize: 24,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}

FunkyBackground 参数

FunkyBackground 小部件有几个可选的参数,你可以根据需要自定义背景效果:

  • animationType: 背景动画的类型,可以是 AnimationType.colorTransitionAnimationType.particlesAnimationType.wave 等。
  • colors: 背景颜色列表,用于颜色过渡动画。
  • particleCount: 粒子动画中的粒子数量。
  • waveSpeed: 波浪动画的速度。
  • waveHeight: 波浪动画的高度。
  • blur: 背景的模糊程度。

例如,你可以这样自定义背景:

FunkyBackground(
  animationType: AnimationType.wave,
  colors: [Colors.blue, Colors.purple, Colors.red],
  waveSpeed: 0.5,
  waveHeight: 0.2,
  child: Center(
    child: Text(
      'Custom Wave Background!',
      style: TextStyle(
        fontSize: 24,
        color: Colors.white,
      ),
    ),
  ),
)
回到顶部