Flutter AnimatedDefaultTextStyle Example

AnimatedDefaultTextStyle is an animated version of DefaultTextStyle. The widget automatically changes the default text style whenever the given style changes.

In the example below, we try changing fontWeight, size, and color on Floating Button pressed.

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _isFirst = true;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimatedDefaultTextStyle'),
      ),
      body: SafeArea(
          child: Container(
              color: Colors.grey[100],
              child: Center(
                  child: AnimatedDefaultTextStyle(
                    duration: const Duration(milliseconds: 1000),
                    style: TextStyle(
                      fontSize: _isFirst ? 48 : 24,
                      color: _isFirst ? Colors.red : Colors.blue,
                      fontWeight: _isFirst ? FontWeight.normal : FontWeight.bold,
                    ),
                    child: Text('TL Dev Tech'),
                  ),
              ))),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _isFirst = !_isFirst;
          });

        },
        child: Icon(Icons.play_arrow, color: Colors.white),
      ),
    );
  }
}

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close