icon_btn.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * @Author : WuWei
  3. * @LastEditors : WuWei
  4. * @Date : 2022-08-17 23:04:42
  5. * @LastEditTime : 2022-08-17 23:06:27
  6. * @Description : Do not edit
  7. */
  8. import 'package:flutter/cupertino.dart';
  9. import 'package:flutter/material.dart';
  10. /// Icon button widget built different
  11. /// depends on [MaterialApp] or [CupertinoApp] ancestor.
  12. class IconBtn extends StatelessWidget {
  13. /// Widget to use inside button.
  14. ///
  15. /// Typically [Icon] widget.
  16. final Widget? icon;
  17. /// Function called when user tap on the button.
  18. ///
  19. /// Can be null. In this case button will be disabled.
  20. final VoidCallback? onTap;
  21. /// Tooltip for button.
  22. ///
  23. /// Applied only for material style buttons.
  24. /// It means only if widget has [MaterialApp] ancestor.
  25. final String? tooltip;
  26. /// Creates button with [icon] different
  27. /// depends on [MaterialApp] or [CupertinoApp] ancestor.
  28. const IconBtn({Key? key, required this.icon, this.onTap, this.tooltip})
  29. : super(key: key);
  30. @override
  31. Widget build(BuildContext context) {
  32. // ignore: unnecessary_null_comparison
  33. bool isMaterial = Material.of(context) != null;
  34. return isMaterial ? _materialBtn() : _cupertinoBtn();
  35. }
  36. Widget _cupertinoBtn() => CupertinoButton(
  37. padding: const EdgeInsets.all(0.0),
  38. child: icon!,
  39. onPressed: onTap,
  40. );
  41. Widget _materialBtn() => IconButton(
  42. icon: icon!,
  43. tooltip: tooltip!,
  44. onPressed: onTap,
  45. );
  46. }