Flutter-->自定义floatingActionButto

2019-05-14  本文已影响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: () {},
      ),
    );
  }
}

上一篇下一篇

猜你喜欢

热点阅读