Giter Club home page Giter Club logo

Comments (2)

AmosHuKe avatar AmosHuKe commented on June 28, 2024 1

Thanks for your feature suggestions! 😀

Now that a new version has been released.
It provides the TiltStreamController.

# pubspec.yaml

dependencies:
  flutter_tilt: ^2.4.0

Here is an example for you.

TiltStreamController

You can use it through animation or otherwise.

Code

Click to view code
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/material.dart';

import 'package:flutter_tilt/flutter_tilt.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Scaffold(body: TiltStreamControllerDemo()),
    );
  }
}

class TiltStreamControllerDemo extends StatefulWidget {
  const TiltStreamControllerDemo({super.key});

  @override
  State<TiltStreamControllerDemo> createState() =>
      _TiltStreamControllerDemoState();
}

class _TiltStreamControllerDemoState extends State<TiltStreamControllerDemo>
    with TickerProviderStateMixin {
  final MaterialStateProperty<Icon?> thumbIcon =
      MaterialStateProperty.resolveWith<Icon?>(
    (Set<MaterialState> states) {
      return states.contains(MaterialState.selected)
          ? const Icon(Icons.link)
          : const Icon(Icons.link_off);
    },
  );
  late StreamController<TiltStreamModel> tiltStreamController;
  bool controllerBind = true;
  AnimationController? animationSelected;

  /// AnimationController around
  late AnimationController aroundAnimationController;

  /// AnimationController leftTop -> rightBottom
  late AnimationController ltrbAnimationController;

  /// AnimationController rightTop -> leftBottom
  late AnimationController rtlbAnimationController;

  @override
  void initState() {
    super.initState();
    tiltStreamController = StreamController.broadcast();

    aroundAnimation();
    ltrbAnimation();
    rtlbAnimation();
    animationSelected = aroundAnimationController;
    animationSelected?.repeat(reverse: false);
  }

  @override
  void dispose() {
    tiltStreamController.close();
    aroundAnimationController.dispose();
    ltrbAnimationController.dispose();
    rtlbAnimationController.dispose();
    super.dispose();
  }

  void stopAllAnimation() {
    aroundAnimationController.stop();
    ltrbAnimationController.stop();
    rtlbAnimationController.stop();
  }

  /// Animation around
  void aroundAnimation() {
    aroundAnimationController = AnimationController(
      lowerBound: 0.0,
      upperBound: 1.0,
      duration: const Duration(milliseconds: 3000),
      vsync: this,
    );
    aroundAnimationController.addListener(() {
      final double radian =
          aroundAnimationController.value * 360 * math.pi / 180;
      const double r = 75;
      // P = (cosθ, sinθ)
      final double x = math.cos(radian) * r + 150;
      final double y = math.sin(radian) * r + 75;
      tiltStreamController.add(TiltStreamModel(position: Offset(x, y)));
    });
  }

  /// Animation leftTop -> rightBottom
  void ltrbAnimation() {
    ltrbAnimationController = AnimationController(
      lowerBound: 0.0,
      upperBound: 1.0,
      duration: const Duration(milliseconds: 3000),
      vsync: this,
    );
    ltrbAnimationController.addListener(() {
      final double x = ltrbAnimationController.value * 300;
      final double y = ltrbAnimationController.value * 150;
      tiltStreamController.add(TiltStreamModel(position: Offset(x, y)));
    });
  }

  /// Animation rightTop -> leftBottom
  void rtlbAnimation() {
    rtlbAnimationController = AnimationController(
      lowerBound: 0.0,
      upperBound: 1.0,
      duration: const Duration(milliseconds: 3000),
      vsync: this,
    );
    rtlbAnimationController.addListener(() {
      final double x = (1 - rtlbAnimationController.value) * 300;
      final double y = (rtlbAnimationController.value) * 150;
      tiltStreamController.add(TiltStreamModel(position: Offset(x, y)));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        /// Tilt here
        Tilt(
          tiltStreamController: controllerBind ? tiltStreamController : null,
          borderRadius: BorderRadius.circular(30),
          tiltConfig: const TiltConfig(enableGestureSensors: false),
          childLayout: const ChildLayout(
            outer: [
              Positioned(
                child: TiltParallax(
                  size: Offset(10, 10),
                  child: Text(
                    'Flutter Tilt 1 ✨',
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ],
          ),
          child: Container(
            width: 300,
            height: 150,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topRight,
                end: Alignment.bottomLeft,
                colors: [Color(0xFF80d0c7), Color(0xFF13547a)],
              ),
            ),
          ),
        ),

        /// bind
        Padding(
          padding: const EdgeInsets.all(20),
          child: Switch(
            thumbIcon: thumbIcon,
            value: controllerBind,
            onChanged: (value) {
              setState(() {
                controllerBind = value;
              });
            },
          ),
        ),

        /// Tilt here
        Tilt(
          tiltStreamController: controllerBind ? tiltStreamController : null,
          borderRadius: BorderRadius.circular(30),
          tiltConfig: const TiltConfig(enableGestureSensors: false),
          childLayout: const ChildLayout(
            outer: [
              Positioned(
                child: TiltParallax(
                  size: Offset(10, 10),
                  child: Text(
                    'Flutter Tilt 2 ✨',
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ],
          ),
          child: Container(
            width: 300,
            height: 150,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [Color(0xFF8989ba), Color(0xFFa7a6cb)],
              ),
            ),
          ),
        ),

        /// Tools
        /// Animation
        const Text(
          "Animation",
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 12),
        Wrap(
          spacing: 12,
          runSpacing: 12,
          children: [
            FilterChip(
              label: const Text('none'),
              selected: animationSelected == null,
              onSelected: (bool value) {
                setState(() {
                  if (value) {
                    stopAllAnimation();
                    animationSelected = null;
                  }
                });
              },
            ),
            FilterChip(
              label: const Text('around'),
              selected: animationSelected == aroundAnimationController,
              onSelected: (bool value) {
                setState(() {
                  if (value) {
                    animationSelected = aroundAnimationController
                      ..repeat(reverse: false);
                  } else {
                    stopAllAnimation();
                  }
                });
              },
            ),
            FilterChip(
              label: const Text('leftTop -> rightBottom'),
              selected: animationSelected == ltrbAnimationController,
              onSelected: (bool value) {
                setState(() {
                  if (value) {
                    animationSelected = ltrbAnimationController
                      ..repeat(reverse: true);
                  } else {
                    stopAllAnimation();
                  }
                });
              },
            ),
            FilterChip(
              label: const Text('rightTop -> leftBottom'),
              selected: animationSelected == rtlbAnimationController,
              onSelected: (bool value) {
                setState(() {
                  if (value) {
                    animationSelected = rtlbAnimationController
                      ..repeat(reverse: true);
                  } else {
                    stopAllAnimation();
                  }
                });
              },
            ),
          ],
        ),
      ],
    );
  }
}

Video

TiltStreamController.mp4

About Depth

The current package mainly applies the relevant effects in 2 dimensions and does not consider 3 dimensions for the time being.

For example, transforming from a card to a box.

from flutter_tilt.

potikorn avatar potikorn commented on June 28, 2024 1

@AmosHuKe That's awesome. Thanks for your effort. 😄👍

from flutter_tilt.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.