flutter灵动岛效果


import 'package:flutter/material.dart';
import '../controllers/island_controller.dart';
import 'package:get/get.dart';

class IslandView extends GetView<IslandController> {
  const IslandView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: Text("灵动岛功能实现"),
      ),
      body: ListView(
        children: [
          Center(
            child: AnimatedBuilder(
              animation: controller.animation,
              builder: (context, child) {
                return Container(
                  margin:const EdgeInsets.only(top:50),
                  width: 150 * controller.animation.value,
                  height: 40,
                  decoration: BoxDecoration(
                    color: Colors.black,
                    borderRadius: BorderRadius.circular(40),
                  ),
                  child:const Center(
                    child: Text(
                      "前程出行",
                      style: TextStyle(color: Colors.white,fontSize: 16),
                    ),
                  ),
                );
              },
            ),
          ),
          Center(
            child: Container(
              margin:const EdgeInsets.only(top:50),
              width: 120,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  primary: Colors.blue, // 设置背景颜色
                  onPrimary: Colors.white, // 设置字体颜色
                ),
                onPressed: () {
                  // 按钮被点击时执行的代码
                  controller.toggleWidth();
                },
                child:const Text("点击效果") ,
              ),

            ),
          )
        ],
      ),
    );
  }
}



///-----------------Controller页面------------------------

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class IslandController extends GetxController with GetTickerProviderStateMixin {
  late AnimationController animationController;
  late Animation<double> animation;
  @override
  void onInit() {
    super.onInit();
    initialization();
  }
  ///初始化
  void initialization(){
    animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 500),
    );
    animation = Tween<double>(begin:1.2, end:0.8).animate(animationController);
    animationController.forward();//小
  }
  ///点击按钮实现效果
  void toggleWidth() {
    if (animationController.isCompleted) {
      animationController.reverse();//大
      Timer.periodic(const Duration(milliseconds:500), (Timer timer) {
        animationController.forward();//小
        timer.cancel(); // 停止计时器
      });
    } else {
      animationController.forward();//小
    }
  }
  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    animationController.dispose();
    super.onClose();
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值