Skip to content

Instantly share code, notes, and snippets.

@DiegoCarvalho75
Forked from maheshmnj/torch.md
Created August 29, 2021 15:18
Show Gist options
  • Save DiegoCarvalho75/9b45aab1ca91f21e1095532454845e01 to your computer and use it in GitHub Desktop.
Save DiegoCarvalho75/9b45aab1ca91f21e1095532454845e01 to your computer and use it in GitHub Desktop.
Torch Effect flutter
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Torch Effect'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  void _updateLocation(PointerEvent details) {
    _offsetNotifier.value = Offset(details.position.dx, details.position.dy);
  }

  final _offsetNotifier = ValueNotifier<Offset>(Offset(100, 100));

  @override
  void dispose() {
    // TODO: implement dispose
    _offsetNotifier.dispose();
    super.dispose();
  }

  String image1 =
      'https://mymodernmet.com/wp/wp-content/uploads/2021/04/Nature-Sounds-For-Well-Being-03.jpg';
  String image2 = 'https://scx2.b-cdn.net/gfx/news/hires/2019/2-nature.jpg';

  @override
  Widget build(BuildContext context) {
    _image(x) => Image.network(
          '$x',
          fit: BoxFit.fitHeight,
        );
    return Scaffold(
      backgroundColor: Colors.black.withOpacity(0.9),
      body: ValueListenableBuilder<Offset>(
          valueListenable: _offsetNotifier,
          builder: (
            BuildContext context,
            Offset offset,
            Widget? child,
          ) {
            return MouseRegion(
                cursor: SystemMouseCursors.click,
                onHover: _updateLocation,
                child: Stack(
                  alignment: Alignment.center,
                  fit: StackFit.expand,
                  children: [
                    const Opacity(opacity: 0.2, child: Counter()),
                    ClipPath(
                        clipper: CircleRevealClipper(
                          center: Offset(offset.dx, offset.dy),
                          radius: 100,
                        ),
                        child: _image(image2)),
                  ],
                ));
          }),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Counter App'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '100',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class CircleRevealClipper extends CustomClipper<Path> {
  final Offset center;
  final double radius;

  CircleRevealClipper({required this.center, required this.radius});

  @override
  Path getClip(Size size) {
    return Path()..addOval(Rect.fromCircle(radius: radius, center: center));
  }

  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
    return true;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment