dart 边界Flutter过渡

8ehkhllq  于 2023-06-03  发布在  Flutter
关注(0)|答案(3)|浏览(103)

我有一个TextField,当我聚焦到输入边框时,它会直接改变。但我想让它更光滑。我该怎么做?

Container(decoration: isFocused ? focusedBorder : blurBorder) //isFocused triggers when i focus to input.

我的边界:

final focusedBorder = BoxDecoration(
      borderRadius: BorderRadius.circular(8),
      gradient: LinearGradient(
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
          colors: [
            Color.fromARGB(255, 141, 56, 211),
            Color.fromARGB(255, 156, 90, 209),
            Color.fromARGB(255, 93, 53, 213),
          ],
          stops: [
            0.0,
            0.5,
            0.7
          ]));
  final blurBorder = BoxDecoration(
      borderRadius: BorderRadius.circular(8),
      border:
          Border.all(width: 1.5, color: Color.fromARGB(255, 238, 241, 246)));
laawzig2

laawzig21#

使用AnimatedContainer代替Container。然后,您可以更改容器内的任何属性,它将自动设置更改的动画。您甚至可以设置一个duration为相同的

41ik7eoe

41ik7eoe2#

这里有一个关于如何做到这一点的例子,做同样的事情,但使用AnimatedContainer和duration属性将帮助您为动画设置特定的持续时间:

class _MyHomePageState extends State<MyHomePage> {

  final blurBorder = BoxDecoration(
      borderRadius: BorderRadius.circular(8),
      border:
      Border.all(width: 1.5, color: const Color.fromARGB(255, 238, 241, 246)));

  final focusedBorder = BoxDecoration(
      borderRadius: BorderRadius.circular(8),
      gradient: const LinearGradient(
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
          colors: [
            Color.fromARGB(255, 141, 56, 211),
            Color.fromARGB(255, 156, 90, 209),
            Color.fromARGB(255, 93, 53, 213),
          ],
          stops: [
            0.0,
            0.5,
            0.7
          ]));

  bool isFocused = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
          children: [

            TextButton(onPressed: () {

              setState(() {
                isFocused = !isFocused;
              });

            }, child: const Text('Action', style: TextStyle( color: Colors.black ),)),

            AnimatedContainer(
                width: 100,
                height: 100,
                duration: const Duration( milliseconds: 900 ),
                decoration: isFocused ? focusedBorder : blurBorder,
            )

          ],
        ),

    );
  }

}
vsikbqxv

vsikbqxv3#

Border和BoxDecoration的颜色好像Opacity不起作用,可以这样使用AnimatedBuilderwithOpacity

AnimatedBuilder(
  animation: animation,
  builder: (BuildContext context, Widget? child) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey.withOpacity(animation.value),width: 1,)
      ),
    )
  })

相关问题