如何在TabPageSelector中删除边框圆?

时间:2019-04-25 20:36:10

标签: flutter flutter-layout

我想创建类似这样的东西 1] 但是当我尝试使用这样的代码时:

TabPageSelector(
  controller: _tabController,
  color: Color(0x21000000),
  selectedColor: Colors.white,
),

我得到结果: enter image description here

为获得准确的结果,我可以更改哪些内容,这是我在第一张照片中显示的吗?

2 个答案:

答案 0 :(得分:0)

我了解你想要什么;为此,实际上是ctrl并单击TabPageSelector,然后转到底部搜索TabPageSelectorIndicator(位于底部),然后将其替换为以下内容:

class TabPageSelectorIndicator extends StatelessWidget {
  /// Creates an indicator used by [TabPageSelector].
  ///
  /// The [backgroundColor], [borderColor], and [size] parameters must not be null.
  const TabPageSelectorIndicator({
    Key key,
    @required this.backgroundColor,
    @required this.size,
  }) : assert(backgroundColor != null),
       assert(size != null),
       super(key: key);

  /// The indicator circle's background color.
  final Color backgroundColor;

  /// The indicator circle's diameter.
  final double size;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: size,
      height: size,
      margin: const EdgeInsets.all(4.0),
      decoration: BoxDecoration(
        color: backgroundColor,
        shape: BoxShape.circle,
      ),
    );
  }
}

/// Displays a row of small circular indicators, one per tab. The selected
/// tab's indicator is highlighted. Often used in conjunction with a [TabBarView].
///
/// If a [TabController] is not provided, then there must be a [DefaultTabController]
/// ancestor.
class TabPageSelector extends StatelessWidget {
  /// Creates a compact widget that indicates which tab has been selected.
  const TabPageSelector({
    Key key,
    this.controller,
    this.indicatorSize = 12.0,
    this.color,
    this.selectedColor,
  }) : assert(indicatorSize != null && indicatorSize > 0.0),
       super(key: key);

  /// This widget's selection and animation state.
  ///
  /// If [TabController] is not provided, then the value of [DefaultTabController.of]
  /// will be used.
  final TabController controller;

  /// The indicator circle's diameter (the default value is 12.0).
  final double indicatorSize;

  /// The indicator circle's fill color for unselected pages.
  ///
  /// If this parameter is null then the indicator is filled with [Colors.transparent].
  final Color color;

  /// The indicator circle's fill color for selected pages and border color
  /// for all indicator circles.
  ///
  /// If this parameter is null then the indicator is filled with the theme's
  /// accent color, [ThemeData.accentColor].
  final Color selectedColor;

  Widget _buildTabIndicator(
    int tabIndex,
    TabController tabController,
    ColorTween selectedColorTween,
    ColorTween previousColorTween,
  ) {
    Color background;
    if (tabController.indexIsChanging) {
      // The selection's animation is animating from previousValue to value.
      final double t = 1.0 - _indexChangeProgress(tabController);
      if (tabController.index == tabIndex)
        background = selectedColorTween.lerp(t);
      else if (tabController.previousIndex == tabIndex)
        background = previousColorTween.lerp(t);
      else
        background = selectedColorTween.begin;
    } else {
      // The selection's offset reflects how far the TabBarView has / been dragged
      // to the previous page (-1.0 to 0.0) or the next page (0.0 to 1.0).
      final double offset = tabController.offset;
      if (tabController.index == tabIndex) {
        background = selectedColorTween.lerp(1.0 - offset.abs());
      } else if (tabController.index == tabIndex - 1 && offset > 0.0) {
        background = selectedColorTween.lerp(offset);
      } else if (tabController.index == tabIndex + 1 && offset < 0.0) {
        background = selectedColorTween.lerp(-offset);
      } else {
        background = selectedColorTween.begin;
      }
    }
    return TabPageSelectorIndicator(
      backgroundColor: background,
      size: indicatorSize,
    );
  }

  @override
  Widget build(BuildContext context) {
    final Color fixColor = color ?? Colors.transparent;
    final Color fixSelectedColor = selectedColor ?? Theme.of(context).accentColor;
    final ColorTween selectedColorTween = ColorTween(begin: fixColor, end: fixSelectedColor);
    final ColorTween previousColorTween = ColorTween(begin: fixSelectedColor, end: fixColor);
    final TabController tabController = controller ?? DefaultTabController.of(context);
    assert(() {
      if (tabController == null) {
        throw FlutterError(
          'No TabController for $runtimeType.\n'
          'When creating a $runtimeType, you must either provide an explicit TabController '
          'using the "controller" property, or you must ensure that there is a '
          'DefaultTabController above the $runtimeType.\n'
          'In this case, there was neither an explicit controller nor a default controller.'
        );
      }
      return true;
    }());
    final Animation<double> animation = CurvedAnimation(
      parent: tabController.animation,
      curve: Curves.fastOutSlowIn,
    );
    return AnimatedBuilder(
      animation: animation,
      builder: (BuildContext context, Widget child) {
        return Semantics(
          label: 'Page ${tabController.index + 1} of ${tabController.length}',
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: List<Widget>.generate(tabController.length, (int tabIndex) {
              return _buildTabIndicator(tabIndex, tabController, selectedColorTween, previousColorTween);
            }).toList(),
          ),
        );
      }
    );
  }
}

答案 1 :(得分:0)

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rutrum urna nec risus dapibus, nec sagittis risus vulputate. Nullam volutpat eros nec sagittis eleifend. Nam sit amet convallis sem, quis tempus quam. Cras egestas est at ipsum tincidunt maximus. Vivamus suscipit justo vel sapien blandit, sed vehicula ipsum euismod. Etiam faucibus tortor at augue ullamcorper, at feugiat lorem varius. Quisque at purus vestibulum, varius ex quis, viverra nibh. Nulla ac porta enim. Nulla egestas mauris a pretium rutrum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam lacinia augue nec augue dictum condimentum. Ut aliquam ac lacus a venenatis. Pellentesque egestas libero sed sem rutrum tempus. Duis posuere turpis at lacinia varius. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque vitae ligula mollis, vehicula mi at, venenatis justo. Nullam id imperdiet odio. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean arcu est, venenatis at sem commodo, condimentum feugiat arcu. Suspendisse quis malesuada ipsum. Quisque lobortis est in dapibus fermentum. Sed lacinia lacus leo, ac euismod metus faucibus vitae. Curabitur fermentum vulputate vestibulum. Nullam quis turpis ac tortor pharetra accumsan. Aenean finibus lacus quis orci accumsan, iaculis dignissim purus lacinia. Maecenas convallis risus in pulvinar pharetra. Curabitur eu maximus risus. Nunc convallis finibus venenatis. Phasellus nec posuere est, at blandit magna. Nulla rutrum mi id sapien vestibulum eleifend. Nullam sapien nunc, feugiat ut elementum vitae, ultrices in tortor. Integer massa leo, mollis vitae mattis vel, sagittis eu libero. Cras tellus lacus, sodales at nisi ut, dictum mattis risus. Curabitur at euismod purus, quis posuere nulla. Donec quam mauris, faucibus sed tellus sed, finibus convallis nulla. Ut vehicula consectetur ultricies. Morbi malesuada lorem eget cursus pharetra. Curabitur vitae convallis lacus, in aliquet arcu. Sed interdum ut mauris bibendum tincidunt. Proin consectetur elit diam, eget mattis augue euismod et. Nullam et iaculis dolor, ac scelerisque erat. In non volutpat velit, vitae vulputate augue. Quisque sollicitudin nunc erat, a cursus lorem faucibus sit amet. Suspendisse id augue non nisl congue tincidunt. Duis vulputate massa eu finibus semper. Integer tristique, massa a efficitur accumsan, ligula libero imperdiet enim, quis semper quam risus in sapien. Suspendisse potenti. Aliquam magna elit, euismod non egestas id, sollicitudin vitae metus. Cras enim magna, placerat sit amet vestibulum non, pellentesque ac lacus. Aenean ut eros auctor diam tincidunt egestas et ac enim. Nunc tempor lorem eget purus eleifend, facilisis efficitur dui feugiat. Sed viverra suscipit luctus. Donec dignissim lorem a urna elementum, id efficitur turpis vestibulum. Praesent laoreet faucibus orci, vel dictum neque ullamcorper in. 小部件不支持此小部件,但是您可以创建自己的小部件,我为您创建了一个小部件,检查代码并将文件添加到您的项目中:

https://gist.github.com/diegoveloper/9de80d0e22cac8a2ef4357a620b375a9

用法:

TabPageSelector