Flutter隱式(全自動)動畫-TweenAnimationBuilder

示例1

  • AnimatedOpacity
AnimatedOpacity(
    duration: const Duration(milliseconds: 500),
    opacity: 1.0,
    child: Container(
      width: 300,
      height: 300,
      color: Colors.blue,
    ),
  ),
),
  • TweenAnimationBuilder
TweenAnimationBuilder(
  duration: const Duration(milliseconds: 1000),
  tween: Tween<double>(begin: 0.0, end: 1.0),
  builder: (BuildContext context, double value, Widget? child) {
    print('---------value:$value');
    // flutter: ---------value:0.0
    // flutter: ---------value:0.016666
    // ...
    // flutter: ---------value:0.990012
    // flutter: ---------value:1.0
    return Opacity(
      opacity: value,
      child: child,
    );
  },
  child: Container(
    width: 300,
    height: 300,
    color: Colors.blue,
  ),
)
TweenAnimationBuilder(
  duration: const Duration(milliseconds: 1000),
  tween: Tween<double>(begin: 1.0, end: 1.2),
  builder: (BuildContext context, double value, Widget? child) {
    return Container(
      width: 300,
      height: 300,
      color: Colors.blue,
      child: Center(
        child: Transform.scale(
          scale: value,
          child: const Text(
            'HI',
            style: TextStyle(fontSize: 72),
          ),
        ),
      ),
    );
  },
)
TweenAnimationBuilder(
  duration: const Duration(milliseconds: 1000),
  tween: Tween<Offset>(begin: const Offset(0, 0), end: const Offset(100, 100)),
  builder: (BuildContext context, Offset value, Widget? child) {
    return Container(
      width: 300,
      height: 300,
      color: Colors.blue,
      child: Transform.translate(
        offset: value,
        child: const Text(
          'HI',
          style: TextStyle(fontSize: 72),
        ),
      ),
    );
  },
)
TweenAnimationBuilder(
  duration: const Duration(milliseconds: 1000),
  tween: Tween<double>(begin: 0.0, end: 6.28),
  builder: (BuildContext context, double value, Widget? child) {
    return Container(
      width: 300,
      height: 300,
      color: Colors.blue,
      child: Center(
        child: Transform.rotate(
          // 旋轉(zhuǎn)一圈
          angle: value,
          child: const Text(
            'HI',
            style: TextStyle(fontSize: 72),
          ),
        ),
      ),
    );
  },
)

示例2

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          width: 300,
          height: 120,
          color: Colors.blue,
          child: AnimatedCounter(
            duration: const Duration(milliseconds: 500),
            counter: _counter,
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _counter += 1;
          });
        },
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class AnimatedCounter extends StatelessWidget {
  const AnimatedCounter({
    super.key,
    required Duration duration,
    required int counter,
  })  : _counter = counter,
        _duration = duration;

  final int _counter;
  final Duration _duration;

  @override
  Widget build(BuildContext context) {
    return TweenAnimationBuilder(
      duration: _duration,
      // tween就是在起始值和終止值之間插入若干個數(shù)據(jù)
      tween: Tween<double>(end: _counter.toDouble()),
      builder: (BuildContext context, double value, Widget? child) {
        print('--------value:$value');
        // 取整
        final whole = value ~/ 1;
        // 小數(shù)部分
        final decimal = value - whole;
        return Stack(
          children: [
            PositionedDirectional(
              top: -100 * decimal, // 0 -> -100
              child: Opacity(
                opacity: 1.0 - decimal, // 1.0 -> 0.0
                child: Text(
                  '$whole',
                  style: const TextStyle(fontSize: 100),
                ),
              ),
            ),
            PositionedDirectional(
              top: 100 - 100 * decimal, // 100 -> 0
              child: Opacity(
                opacity: decimal, // 0.0 -> 1.0
                child: Text(
                  '${whole + 1}',
                  style: const TextStyle(fontSize: 100),
                ),
              ),
            ),
          ],
        );
      },
    );
  }
}

參考資料

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末费彼,一起剝皮案震驚了整個濱河市韧掩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機肴捉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叔收,“玉大人齿穗,你說我怎么就攤上這事〗嚷桑” “怎么了缤灵?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蓝晒。 經(jīng)常有香客問我腮出,道長,這世上最難降的妖魔是什么芝薇? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任胚嘲,我火速辦了婚禮,結(jié)果婚禮上洛二,老公的妹妹穿的比我還像新娘馋劈。我一直安慰自己,他們只是感情好晾嘶,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布妓雾。 她就那樣靜靜地躺著,像睡著了一般垒迂。 火紅的嫁衣襯著肌膚如雪械姻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天机断,我揣著相機與錄音楷拳,去河邊找鬼绣夺。 笑死,一個胖子當(dāng)著我的面吹牛欢揖,可吹牛的內(nèi)容都是我干的陶耍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼她混,長吁一口氣:“原來是場噩夢啊……” “哼烈钞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起坤按,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤毯欣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后晋涣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡沉桌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年谢鹊,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片留凭。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡佃扼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蔼夜,到底是詐尸還是另有隱情兼耀,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布求冷,位于F島的核電站瘤运,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏匠题。R本人自食惡果不足惜拯坟,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望韭山。 院中可真熱鬧郁季,春花似錦、人聲如沸钱磅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽盖淡。三九已至年柠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間褪迟,已是汗流浹背彪杉。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工毅往, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人派近。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓攀唯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親渴丸。 傳聞我的和親對象是個殘疾皇子侯嘀,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容