Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw the Shape of Text?

Am trying to make an animated foreground for a title text in flutter or to more accurate i want mast the text with GIF something like this enter image description here Am not even sure how to do it but i think if i managed to make stack filled with a GIF then make the last layer a CustomClipper<Path> to fill the entire space but the Text shape then it will look like it, the problem is i don't know how to make the text shape !plus i don't know how to make a path that just fill the entire size except the the text shape i will provide , please any help will be appreciated or if you have any ideas that will do it but in a different way am also interested and thanks in advance .

like image 430
OverLoadedBurden Avatar asked Dec 07 '25 08:12

OverLoadedBurden


2 Answers

ah, I'm late..ok, let it be

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: const Text('Title')),
        body: Stack(
          children: [
            FakeAnimatedBackground(),
            ShaderMask(
              blendMode: BlendMode.srcOut,
              shaderCallback: (bounds) => LinearGradient(colors: [Colors.black], stops: [0.0]).createShader(bounds),
              child: SizedBox.expand(
                child: Container(
                  color: Colors.transparent,
                  alignment: Alignment.center,
                  child: const Text('SOME TEXT', style: TextStyle(fontSize: 60, fontWeight: FontWeight.bold)),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class FakeAnimatedBackground extends StatefulWidget {
  @override
  _FakeAnimatedBackgroundState createState() => _FakeAnimatedBackgroundState();
}

class _FakeAnimatedBackgroundState extends State<FakeAnimatedBackground> with TickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(duration: const Duration(milliseconds: 5000), vsync: this)..repeat();
  }

  @override
  Widget build(BuildContext context) {
    return RotationTransition(
      alignment: Alignment.center,
      turns: Tween(begin: 0.0, end: 1.0).animate(_controller),
      child: Container(
        decoration: BoxDecoration(
          gradient: SweepGradient(colors: [Colors.red, Colors.green, Colors.blue, Colors.red]),
        ),
      ),
    );
  }
}

FakeAnimationBackground class doesn't matter, it's just simulate background moving

enter image description here

you can use ImageShader, ShaderMask and StreamBuilder ( for gif )


import 'dart:async';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;

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

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

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

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

class _MyHomePageState extends State<MyHomePage> {

  Stream<ui.Image> _image;

  @override
  initState() {
    super.initState();
    _image = _getImage();
  }

  Stream<ui.Image> _getImage() {
    var _controller = new StreamController<ui.Image>();
    new AssetImage('assets/b.gif')
        .resolve(new ImageConfiguration())
        .addListener(ImageStreamListener(
            (info, _) {
              _controller.add(info.image);
            }
    ));
    return _controller.stream;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
          child: StreamBuilder<ui.Image>(
            stream: _image,
            builder: (context, data) {
              if (data.data == null)
                return Text('loading');
              return ShaderMask(
                child: Text("Gif !!", style: TextStyle(fontSize: 50),),
                blendMode: BlendMode.srcATop,
                shaderCallback: (bounds) {
                  return ui.ImageShader(
                    data.data,
                    TileMode.repeated,
                    TileMode.repeated,
                    Matrix4.identity().storage,
                  );
                },
              );
            },
          ),
        ));
  }


}

like image 35
Sahandevs Avatar answered Dec 09 '25 15:12

Sahandevs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!