Flutter AnimatedPhysicalModel Example

PhysicalModel properties can be animated with its animated widget version, AnimatedPhysicalModel. The widget can animate borderRadius, elevation, and color (if the Color property is set).

This post will introduce an AnimatedPhysicalModel example of animating all values which are possible to be animated.

import 'package:flutter/material.dart';

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

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

class _HomepageState extends State<Homepage> {
  bool _play = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: AnimatedPhysicalModel(
          duration: Duration(seconds: 1),
          color: _play ? Colors.red : Colors.blue,
          elevation: _play ? 9 : 3,
          shape: BoxShape.rectangle,
          shadowColor: Colors.deepOrangeAccent,
          borderRadius: _play
              ? const BorderRadius.all(Radius.circular(0))
              : const BorderRadius.all(Radius.circular(24)),
          child: Container(
            width: 240,
            height: 240,
            child: Center(
                child: Text(
              'AnimatedPhysicalModel',
              style: TextStyle(fontSize: 18.0, color: Colors.white),
            )),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _play = !_play;
          });
        },
        child: Icon(Icons.play_arrow),
      ),
    );
  }
}

Leave a Comment

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