Flutter UI #97: Fun with Parallax Rain in Flutter


Flutter UI #97: Fun with Parallax Rain in Flutter

Tutorial and code of Parallax Rain in Flutter. Copy and paste the below code as per your requirements.

parallax_rain: ^1.1.0

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Parallax Rain',
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: Colors.black),
      home: const MyHomePage(),
    );
  }
}

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

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final Key parallaxOne = GlobalKey();
  final Key parallaxTwo = GlobalKey();
  final Key parallaxThree = GlobalKey();
  final Key parallaxFour = GlobalKey();
  int counter = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Center(child: Text("Parallax Rain"),),),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: [
          Expanded(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Expanded(
                  child: ParallaxRain(
                    key: parallaxOne,
                    dropColors: const [
                      Colors.red,
                      Colors.green,
                      Colors.blue,
                      Colors.yellow,
                      Colors.brown,
                      Colors.blueGrey
                    ],
                    child: Center(
                      child: TextButton(
                        child: Text(
                          "Multicolor with setState (press here): $counter",
                          style: const TextStyle(color: Colors.white),
                        ),
                        onPressed: () {
                          setState(() {
                            counter++;
                          });
                        },
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: ParallaxRain(
                    key: parallaxTwo,
                    dropColors: const[
                      Colors.red,
                      Colors.green,
                      Colors.blue,
                      Colors.yellow,
                      Colors.brown,
                      Colors.blueGrey
                    ],
                    trail: true,
                    child: const Center(
                      child: Text(
                        "Multicolor Trail",
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Expanded(
                  child: ParallaxRain(
                    key: parallaxThree,
                    dropColors: const [Colors.blueGrey],
                    trail: true,
                    child: const Center(
                      child: Text(
                        "BlueGrey Trail",
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: ParallaxRain(
                    key: parallaxFour,
                    dropColors: const[Colors.blueGrey],
                    trail: true,
                    dropFallSpeed: 5,
                    child: const Center(
                      child: Text(
                        "BlueGrey Trail Fast",
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

Leave a Reply

Your email address will not be published.