在鼠标悬停时,Flutter 提升按钮的颜色滑入滑出过渡动画

huangapple go评论99阅读模式
英文:

Flutter elevated button color slide in slide out transition animation on hover

问题

如何在Flutter中实现悬浮按钮颜色的鼠标悬停滑入滑出过渡动画

我尝试使用Inkwell的鼠标悬停属性来进行滑动过渡位置,但未能实现。

英文:

How to achieve Flutter elevated button color slide in slide out transition animation on hover

I tried with Slide transition position using on hover property of Inkwell but not able to achieve it

答案1

得分: 0

****更新(09-03-23**

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/src/gestures/events.dart';

class DummyWidget extends StatefulWidget {
  const DummyWidget({super.key});

  @override
  State<DummyWidget> createState() => _DummyWidgetState();
}

class _DummyWidgetState extends State<DummyWidget> with SingleTickerProviderStateMixin {
  StreamController<double> animationState = StreamController<double>();

  double width = 150, height = 70;

  @override
  void initState() {
    super.initState();
    animationState.add(0);
  }

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

  @override
  Widget build(BuildContext context) => SizedBox(
    height: height,
    width: width,
    child: Padding(
      padding: const EdgeInsets.all(8.0),
      child: InkWell(
        onTap: () {},
        child: MouseRegion(
          onEnter: (PointerEnterEvent s) {
            animationState.sink.add(width);
          },
          onExit: (PointerExitEvent s) {
            animationState.sink.add(0);
          },
          child: Stack(
            children: [
              Card(
                color: Colors.white,
                elevation: 10,
                child: SizedBox(
                  width: width,
                  height: height,
                ),
              ),
              StreamBuilder<double>(
                stream: animationState.stream,
                builder: (
                  BuildContext context,
                  AsyncSnapshot<double> snapshot,
                ) {
                  debugPrint('Width : ${snapshot.data}');

                  return AnimatedContainer(
                    duration: const Duration(milliseconds: 500),
                    curve: Curves.easeInOut,
                    width: snapshot.data,
                    height: height,
                    decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.circular(5),
                    ),
                    margin: const EdgeInsets.all(5),
                    clipBehavior: Clip.hardEdge,
                  );

                  // return AnimatedSize(
                  //   duration: const Duration(milliseconds: 500),
                  //   curve: Curves.easeInOut,
                  //   child: ColoredBox(
                  //     color: Colors.blue,
                  //     child: SizedBox(
                  //       width: snapshot.data,
                  //       height: height,
                  //     ),
                  //   ),
                  // );
                },
              ),
              const Align(
                child: Text(
                  'Something',
                  style: TextStyle(color: Colors.black),
                  textAlign: TextAlign.center,
                ),
              )
            ],
          ),
        ),
      ),
    ),
  );
}
英文:

**Updated (09-03-23)

import &#39;dart:async&#39;;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter/src/gestures/events.dart&#39;;
class DummyWidget extends StatefulWidget {
const DummyWidget({super.key});
@override
State&lt;DummyWidget&gt; createState() =&gt; _DummyWidgetState();
}
class _DummyWidgetState extends State&lt;DummyWidget&gt;
with SingleTickerProviderStateMixin {
StreamController&lt;double&gt; animationState = StreamController&lt;double&gt;();
double width = 150, height = 70;
@override
void initState() {
super.initState();
animationState.add(0);
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) =&gt; SizedBox(
height: height,
width: width,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {},
child: MouseRegion(
onEnter: (PointerEnterEvent s) {
animationState.sink.add(width);
},
onExit: (PointerExitEvent s) {
animationState.sink.add(0);
},
child: Stack(
children: [
Card(
color: Colors.white,
elevation: 10,
child: SizedBox(
width: width,
height: height,
),
),
StreamBuilder&lt;double&gt;(
stream: animationState.stream,
builder: (
BuildContext context,
AsyncSnapshot&lt;double&gt; snapshot,
) {
debugPrint(&#39;Width : ${snapshot.data}&#39;);
return AnimatedContainer(
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut,
width: snapshot.data,
height: height,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(5),
),
margin: const EdgeInsets.all(5),
clipBehavior: Clip.hardEdge,
);
// return AnimatedSize(
//   duration: const Duration(milliseconds: 500),
//   curve: Curves.easeInOut,
//   child: ColoredBox(
//     color: Colors.blue,
//     child: SizedBox(
//       width: snapshot.data,
//       height: height,
//     ),
//   ),
// );
},
),
const Align(
child: Text(
&#39;Something&#39;,
style: TextStyle(color: Colors.black),
textAlign: TextAlign.center,
),
)
],
),
),
),
),
);
}
enter code here

huangapple
  • 本文由 发表于 2023年3月7日 16:44:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定