zoukankan      html  css  js  c++  java
  • Flutter 将TextField平滑过渡到Text

    textField_2_Text.gif

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: HomePage(),
        );
      }
    }
    
    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage>
        with SingleTickerProviderStateMixin {
      AnimationController controller;
    
      @override
      void initState() {
        super.initState();
        controller = AnimationController(
          duration: Duration(seconds: 1),
          vsync: this,
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: AnimatedTextField(
                controller: controller,
              ),
            ),
          ),
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.opacity),
            onPressed: () {
              var status = controller.status;
              if (status == AnimationStatus.completed) {
                controller.reverse();
              } else if (status == AnimationStatus.dismissed) {
                controller.forward();
              }
            },
          ),
        );
      }
    }
    
    class AnimatedTextField extends StatefulWidget {
      final AnimationController controller;
      final Animation<double> opacity;
      final Animation<double> left;
    
      AnimatedTextField({
        Key key,
        @required this.controller,
      })  : opacity = Tween<double>(
              begin: 1.0,
              end: 0.0,
            ).animate(
              CurvedAnimation(
                parent: controller,
                curve: Interval(
                  0.0,
                  0.5,
                  curve: Curves.easeInOut,
                ),
              ),
            ),
            left = Tween<double>(
              begin: 20.0,
              end: 0.0,
            ).animate(
              CurvedAnimation(
                parent: controller,
                curve: Interval(
                  0.0,
                  1.0,
                  curve: Curves.easeIn,
                ),
              ),
            ),
            super(key: key);
    
      @override
      _AnimatedTextFieldState createState() => _AnimatedTextFieldState();
    }
    
    class _AnimatedTextFieldState extends State<AnimatedTextField> {
      TextEditingController _textEditingController =
          TextEditingController(text: 'hello fluttr');
      @override
      Widget build(BuildContext context) {
        return AnimatedBuilder(
          animation: widget.controller,
          builder: (context, child) {
            var theme = Theme.of(context);
            var o = widget.opacity.value;
            var _child = o > 0.0
                ? TextField(
                    controller: _textEditingController,
                    style: theme.textTheme.body2,
                    decoration: InputDecoration(
                      focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Color.fromRGBO(0, 0, 0, 1)),
                      ),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Color.fromRGBO(0, 0, 0, o)),
                      ),
                    ),
                  )
                : Opacity(
                    opacity: (o - 1).abs(),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        _textEditingController.text,
                        style: theme.textTheme.body2,
                      ),
                    ),
                  );
            return Padding(
              padding: EdgeInsets.only(
                left: widget.left.value,
                top: 20,
                right: 20,
                bottom: 20,
              ),
              child: _child,
            );
          },
        );
      }
    }
    
  • 相关阅读:
    ASP.NET的最新安全漏洞Important: ASP.NET Security Vulnerability
    Sql常用日期格式
    倒计时 服务器时间 .NET js javascript
    “备份集中的数据库备份与现有的数据库不同”解决方法
    2010年最佳jQuery插件
    jQuery1.4与json格式兼容问题
    .NET结束外部进程 C#结束外部进程
    十步优化SQL Server中的数据访问
    SQL游标的使用与语法
    SQL2005、SQL2008如何压缩日志文件(log) 如何清除日志
  • 原文地址:https://www.cnblogs.com/ajanuw/p/11672865.html
Copyright © 2011-2022 走看看