基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

84 lines
2.2 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/rendering.dart';
  3. const double _kTabHeight = 46.0;
  4. const double _kTextAndIconTabHeight = 40.0;
  5. /// A material design [TabBar] tab.
  6. ///
  7. /// If both [icon] and [text] are provided, the text is displayed below
  8. /// the icon.
  9. ///
  10. /// See also:
  11. ///
  12. /// * [TabBar], which displays a row of tabs.
  13. /// * [TabBarView], which displays a widget for the currently selected tab.
  14. /// * [TabController], which coordinates tab selection between a [TabBar] and a [TabBarView].
  15. /// * <https://material.io/design/components/tabs.html>
  16. class MyTab extends StatelessWidget {
  17. /// Creates a material design [TabBar] tab.
  18. ///
  19. /// At least one of [text], [icon], and [child] must be non-null. The [text]
  20. /// and [child] arguments must not be used at the same time.
  21. const MyTab({
  22. Key key,
  23. this.text,
  24. this.icon,
  25. this.child,
  26. }) : assert(text != null || child != null || icon != null),
  27. assert(!(text != null && null != child)),
  28. // TODO(goderbauer): https://github.com/dart-lang/sdk/issues/34180
  29. super(key: key);
  30. /// The text to display as the tab's label.
  31. ///
  32. /// Must not be used in combination with [child].
  33. final String text;
  34. /// The widget to be used as the tab's label.
  35. ///
  36. /// Usually a [Text] widget, possibly wrapped in a [Semantics] widget.
  37. ///
  38. /// Must not be used in combination with [text].
  39. final Widget child;
  40. /// An icon to display as the tab's label.
  41. final Widget icon;
  42. Widget _buildLabelText() {
  43. return child ?? Text(text, softWrap: false, overflow: TextOverflow.fade);
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. assert(debugCheckHasMaterial(context));
  48. double height;
  49. Widget label;
  50. if (icon == null) {
  51. height = _kTabHeight;
  52. label = _buildLabelText();
  53. } else if (text == null && child == null) {
  54. height = _kTabHeight;
  55. label = icon;
  56. } else {
  57. height = _kTextAndIconTabHeight;
  58. label = Row(
  59. children: <Widget>[
  60. Container(child: icon, margin: const EdgeInsets.only(right: 5),),
  61. _buildLabelText(),
  62. ],
  63. );
  64. }
  65. return SizedBox(
  66. height: height,
  67. child: Center(
  68. child: label,
  69. widthFactor: 1.0,
  70. ),
  71. );
  72. }
  73. }