美文网首页
Flutter-->自定义floatingActionButto

Flutter-->自定义floatingActionButto

作者: 谢尔顿 | 来源:发表于2019-05-14 11:08 被阅读0次

    效果图:


    效果图不是很清晰

    自定义fancy_button.dart如下:

    import 'package:flutter/material.dart';
    
    class FancyButton extends StatelessWidget {
      final GestureTapCallback onPressed;
      FancyButton({@required this.onPressed});
      @override
      Widget build(BuildContext context) {
        return RawMaterialButton(
          fillColor: Colors.deepOrange,
          splashColor: Colors.orange,
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 20.0),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: const <Widget>[
                RotatedBox(
                  //将icon进行了旋转
                  quarterTurns: 1,
                  child: Icon(
                    Icons.explore,
                    color: Colors.amber,
                  ),
                ),
                SizedBox(
                  width: 8.0,
                ),
                PulseAnimator(
                  //动画效果
                  child: Text(
                    'PURCHASE',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ],
            ),
          ),
          onPressed: onPressed, //点击事件
          shape: const StadiumBorder(), //添加圆角
        );
      }
    }
    
    class PulseAnimator extends StatefulWidget {
      final Widget child;
      const PulseAnimator({Key key, this.child}) : super(key: key);
      @override
      _PulseAnimatorState createState() => _PulseAnimatorState();
    }
    
    class _PulseAnimatorState extends State<PulseAnimator>
        with SingleTickerProviderStateMixin {
      AnimationController _controller;
      @override
      void initState() {
        super.initState();
        _controller = AnimationController(
            vsync: this, duration: const Duration(milliseconds: 1200))
          ..repeat();
      }
    
      @override
      Widget build(BuildContext context) {
        return FadeTransition(
          opacity: Tween(begin: 0.5, end: 1.0).animate(_controller),
          child: widget.child,
        );
      }
    }
    

    入口中使用:

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:practice_demo/fancy_button.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key}) : super(key: key);
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(),
          floatingActionButton: FancyButton(
            onPressed: () {},
          ),
        );
      }
    }
    
    

    相关文章

      网友评论

          本文标题:Flutter-->自定义floatingActionButto

          本文链接:https://www.haomeiwen.com/subject/pmujaqtx.html