Skip to content

Commit

Permalink
document the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
avenwu committed Feb 24, 2020
1 parent 4a7d302 commit 9b3018e
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 47 deletions.
17 changes: 16 additions & 1 deletion lib/src/bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ const double BAR_HEIGHT = 50;
/// Default distance that the child's top edge is inset from the top of the stack.
const double CURVE_TOP = -25;

/// Default size for active tab
const double ACTION_LAYOUT_SIZE = 60;

/// Default size for active icon in tab
const double ACTION_INNER_BUTTON_SIZE = 40;
const int CURVE_INDEX = -1;

/// default elevation of [ConvexAppBar]
const double ELEVATION = 2;

/// Tab style which supported internal.
enum TabStyle {
/// convex shape fixed center, see [FixedTabStyle]
///
Expand Down Expand Up @@ -75,6 +80,7 @@ class ConvexAppBar extends StatefulWidget {
/// TAB item builder
final DelegateBuilder itemBuilder;

/// Badge chip builder
final ChipBuilder chipBuilder;

/// Tab Click handler
Expand Down Expand Up @@ -285,6 +291,7 @@ abstract class DelegateBuilder {
}
}

/// State of [ConvexAppBar]
class ConvexAppBarState extends State<ConvexAppBar>
with TickerProviderStateMixin {
int _currentIndex;
Expand All @@ -309,6 +316,7 @@ class ConvexAppBarState extends State<ConvexAppBar>
}
}

/// change active tab index; can be used with [PageView]
Future<void> animateTo(int index) async {
_initAnimation(from: _currentIndex, to: index);
_controller?.forward();
Expand Down Expand Up @@ -429,6 +437,7 @@ class ConvexAppBarState extends State<ConvexAppBar>
);
}

/// whether the tab shape are fixed or not
bool isFixed() => widget.itemBuilder.fixed();

Widget _barContent(double height, double paddingBottom, int curveTabIndex) {
Expand Down Expand Up @@ -476,6 +485,12 @@ class ConvexAppBarState extends State<ConvexAppBar>
}
}

/// Tab callback, [index] are tab index which is being clicked
typedef GestureTapIndexCallback = void Function(int index);

/// Tab builder;
/// * [context] BuildContent instance
/// * [index] index of tab
/// * [active] active state for tab index
typedef CustomTabBuilder = Widget Function(
BuildContext context, int index, bool active);
21 changes: 21 additions & 0 deletions lib/src/chip_builder.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

/// Interface to apply any custom badge chip
abstract class ChipBuilder {
/// Construct a new widget which represent the tab item with custom badge.
///
/// * [context] BuildContext instance
/// * [child] the tab item Widget
/// * [index] index of the tab item
/// * [active] active state for the index
Widget build(BuildContext context, Widget child, int index, bool active);
}

/// Simple badge with num inside
class DefaultChipBuilder extends ChipBuilder {
/// key-value map, stands for the badge data
final Map<int, dynamic> chips;

/// Color of badge text
final Color textColor;

/// Color of the badge chip
final Color badgeColor;

/// Padding for badge
final EdgeInsets padding;

/// Radius corner for badge
final double borderRadius;

DefaultChipBuilder(
Expand All @@ -33,6 +49,11 @@ class DefaultChipBuilder extends ChipBuilder {
);
}

/// factory method which convert a chip data into [Widget];
///
/// * [chip] String, return a [Text] badge;
/// * [chip] IconData, return a [Icon] badge;
/// * [chip] Widget, return a [Widget] badge;
Widget asBadge(dynamic chip) {
if (chip is String) {
return Positioned.fill(
Expand Down
9 changes: 9 additions & 0 deletions lib/src/convex_shape.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/painting.dart';
import 'dart:math' as math;

/// A convex shape which implemented [NotchedShape].
///
/// It's used to draw a convex shape for [ConvexAppBar], If you are interested about
/// the math calculation, please refer to [CircularNotchedRectangle], it's base
/// on Bezier curve;
///
/// See also:
///
/// * [CircularNotchedRectangle], a rectangle with a smooth circular notch.
class ConvexNotchedRectangle extends NotchedShape {
const ConvexNotchedRectangle();

Expand Down
2 changes: 2 additions & 0 deletions lib/src/item.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/cupertino.dart';

/// Tab item used for [ConvexAppBar]
class TabItem<T> {
/// tab text
final String title;

/// IconData or Image
Expand Down
9 changes: 9 additions & 0 deletions lib/src/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ import 'package:flutter/material.dart';
import 'convex_shape.dart';
import 'reused_gradient.dart';

/// Custom painter to draw the [ConvexNotchedRectangle] into canvas;
class ConvexPainter extends CustomPainter {
final _paint = Paint();
final _shadowPaint = Paint();
final _shape = ConvexNotchedRectangle();
final ReusedGradient _gradient = ReusedGradient();

/// width of the convex shape
final double width;

/// height of the convex shape
final double height;

/// position in vertical which describe the offset of shape
final double top;

/// position in horizontal which describe the offset of shape
final Animation<double> leftPercent;

ConvexPainter({
Expand Down
4 changes: 4 additions & 0 deletions lib/src/reused_gradient.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/cupertino.dart';

/// Wrapper for [Gradient], we don't want to re-create instance frequently in
/// hot method, such as paint.
class ReusedGradient {
Gradient _gradient;

Expand All @@ -8,11 +10,13 @@ class ReusedGradient {

ReusedGradient();

/// setter to reset the [Gradient] instance
set gradient(Gradient gradient) {
_gradient = gradient;
_size = null;
}

/// update the paint with provided size
updateWith(Paint paint, {Rect size}) {
if (size == _size) {
return;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/style/blend_image_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ class BlendImageIcon<T> extends StatelessWidget {
'image must be IconData or Widget'),
super(key: key);

/// color used for Icon and gradient
final Color color;

/// child image
final T image;

/// size of icon
final double size;

@override
Expand Down
20 changes: 12 additions & 8 deletions lib/src/style/fixed_circle_tab_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import 'package:flutter/material.dart';

import '../../convex_bottom_bar.dart';
import 'blend_image_icon.dart';
import 'inner_builder.dart';

/// Convex shape is fixed center with circle
class FixedCircleTabStyle extends DelegateBuilder {
final List<TabItem> items;
final Color activeColor;
final Color color;
class FixedCircleTabStyle extends InnerBuilder {
/// color used as background of appbar and circle icon
final Color backgroundColor;

/// index of the centered convex shape
final int convexIndex;

/// margin of tab
final margin = (ACTION_LAYOUT_SIZE - ACTION_INNER_BUTTON_SIZE) / 4;

FixedCircleTabStyle(
{this.items,
this.activeColor,
this.color,
{List<TabItem> items,
Color activeColor,
Color color,
this.backgroundColor,
this.convexIndex});
this.convexIndex})
: super(items: items, activeColor: activeColor, color: color);

@override
Widget build(BuildContext context, int index, bool active) {
Expand Down
16 changes: 9 additions & 7 deletions lib/src/style/fixed_tab_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import 'package:flutter/material.dart';

import '../../convex_bottom_bar.dart';
import 'blend_image_icon.dart';
import 'inner_builder.dart';

/// Convex shape is fixed center
class FixedTabStyle extends DelegateBuilder {
final List<TabItem> items;
final Color activeColor;
final Color color;
class FixedTabStyle extends InnerBuilder {
/// index of the centered convex shape
final int convexIndex;
DelegateBuilder tabBuilder;
DelegateBuilder convexBuilder;

FixedTabStyle({this.items, this.activeColor, this.color, this.convexIndex});
FixedTabStyle({
List<TabItem> items,
Color activeColor,
Color color,
this.convexIndex,
}) : super(items: items, activeColor: activeColor, color: color);

@override
Widget build(BuildContext context, int index, bool active) {
Expand Down
15 changes: 10 additions & 5 deletions lib/src/style/flip_tab_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import 'package:flutter/material.dart';

import '../../convex_bottom_bar.dart';
import 'blend_image_icon.dart';
import 'inner_builder.dart';
import 'transition_container.dart';

class FlipTabStyle extends DelegateBuilder {
final List<TabItem> items;
final Color activeColor;
final Color color;
/// Tab item are flipped when click
class FlipTabStyle extends InnerBuilder {
/// curve for flip transition
final Curve curve;

FlipTabStyle({this.items, this.activeColor, this.color, this.curve});
FlipTabStyle({
List<TabItem> items,
Color activeColor,
Color color,
this.curve,
}) : super(items: items, activeColor: activeColor, color: color);

@override
Widget build(BuildContext context, int index, bool active) {
Expand Down
17 changes: 17 additions & 0 deletions lib/src/style/inner_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dart:ui';

import '../../convex_bottom_bar.dart';

/// Simple builder which extend [DelegateBuilder] to provide some necessary config;
abstract class InnerBuilder extends DelegateBuilder {
/// List of [TabItem] stands for tabs
final List<TabItem> items;

/// color used when tab is active
final Color activeColor;

/// color used for tab
final Color color;

InnerBuilder({this.items, this.activeColor, this.color});
}
19 changes: 11 additions & 8 deletions lib/src/style/react_circle_tab_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ import 'package:flutter/material.dart';

import '../../convex_bottom_bar.dart';
import 'blend_image_icon.dart';
import 'inner_builder.dart';
import 'transition_container.dart';

/// Convex shape is moved after selection
class ReactCircleTabStyle extends DelegateBuilder {
final List<TabItem> items;
final Color activeColor;
final Color color;
class ReactCircleTabStyle extends InnerBuilder {
/// color used as background of appbar and circle icon
final Color backgroundColor;

/// curve for tab transition
final Curve curve;

/// margin of tab
final margin = (ACTION_LAYOUT_SIZE - ACTION_INNER_BUTTON_SIZE) / 4;

ReactCircleTabStyle({
this.items,
this.activeColor,
this.color,
List<TabItem> items,
Color activeColor,
Color color,
this.backgroundColor,
this.curve,
});
}) : super(items: items, activeColor: activeColor, color: color);

@override
Widget build(BuildContext context, int index, bool active) {
Expand Down
14 changes: 9 additions & 5 deletions lib/src/style/react_tab_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import 'package:flutter/material.dart';

import '../../convex_bottom_bar.dart';
import 'blend_image_icon.dart';
import 'inner_builder.dart';
import 'transition_container.dart';

/// Convex shape is moved after selection
class ReactTabStyle extends DelegateBuilder {
final List<TabItem> items;
final Color activeColor;
final Color color;
class ReactTabStyle extends InnerBuilder {
/// curve for tab transition
final Curve curve;

ReactTabStyle({this.items, this.activeColor, this.color, this.curve});
ReactTabStyle({
List<TabItem> items,
Color activeColor,
Color color,
this.curve,
}) : super(items: items, activeColor: activeColor, color: color);

@override
Widget build(BuildContext context, int index, bool active) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/style/styles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'react_tab_style.dart';
import 'textin_tab_style.dart';
import 'titled_tab_style.dart';

/// factory method to return the [DelegateBuilder] for each [TabStyle]
DelegateBuilder supportedStyle(
TabStyle style, {
@required List<TabItem> items,
Expand Down
14 changes: 9 additions & 5 deletions lib/src/style/textin_tab_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import 'package:flutter/material.dart';

import '../../convex_bottom_bar.dart';
import 'blend_image_icon.dart';
import 'inner_builder.dart';
import 'transition_container.dart';

/// tab icon, text animated with pop transition
class TextInTabStyle extends DelegateBuilder {
final List<TabItem> items;
final Color activeColor;
final Color color;
class TextInTabStyle extends InnerBuilder {
/// curve for tab transition
final Curve curve;

TextInTabStyle({this.items, this.activeColor, this.color, this.curve});
TextInTabStyle({
List<TabItem> items,
Color activeColor,
Color color,
this.curve,
}) : super(items: items, activeColor: activeColor, color: color);

@override
Widget build(BuildContext context, int index, bool active) {
Expand Down

0 comments on commit 9b3018e

Please sign in to comment.