Add Cupertino to gallery and add CupertinoButton and many yak friends (#8411)

* Add cupertino to gallery and add CupertinoButto

* Use single quotes

* Add disabled state

* Some review notes

* Make button animation more responsive and tweak timing

* Renamed things Cupertino

* Button with background, move cupertino demos, move material demos

* Move 2 level list too

* Refactor various demo route names

* Some review notes

* More reviews and add test

* Linter as

* Move private constant up
This commit is contained in:
xster
2017-03-01 16:06:48 -08:00
committed by GitHub
parent 9dec5f9016
commit a76c352d57
42 changed files with 770 additions and 115 deletions

View File

@@ -8,6 +8,7 @@
library cupertino;
export 'src/cupertino/activity_indicator.dart';
export 'src/cupertino/button.dart';
export 'src/cupertino/dialog.dart';
export 'src/cupertino/slider.dart';
export 'src/cupertino/switch.dart';

View File

@@ -0,0 +1,174 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
const TextStyle _kButtonTextStyle = const TextStyle(
fontFamily: '.SF UI Text',
inherit: false,
fontSize: 15.0,
fontWeight: FontWeight.normal,
color: CupertinoButton.kBlue,
textBaseline: TextBaseline.alphabetic,
);
final TextStyle _kDisabledButtonTextStyle = _kButtonTextStyle.copyWith(
color: CupertinoButton.kDisabledForeground,
);
final TextStyle _kBackgroundButtonTextStyle = _kButtonTextStyle.copyWith(
color: CupertinoButton.kWhite,
);
const EdgeInsets _kButtonPadding = const EdgeInsets.all(16.0);
const EdgeInsets _kBackgroundButtonPadding =
const EdgeInsets.symmetric(vertical: 16.0, horizontal: 64.0);
/// An iOS style button.
///
/// Takes in a text or an icon that fades out and in on touch. May optionally have a
/// background.
///
/// See also:
///
/// * <https://developer.apple.com/ios/human-interface-guidelines/ui-controls/buttons/>
class CupertinoButton extends StatefulWidget {
// TODO(xster): move this to a common Cupertino color palatte with the next yak.
static const Color kBlue = const Color(0xFF007AFF);
static const Color kWhite = const Color(0xFFFFFFFF);
static const Color kDisabledBackground = const Color(0xFFA9A9A9);
static const Color kDisabledForeground = const Color(0xFFC4C4C4);
CupertinoButton({
@required this.child,
this.padding,
this.color,
@required this.onPressed,
});
/// The widget below this widget in the tree.
///
/// Typically a [Text] widget.
final Widget child;
/// The amount of space to surround the child inside the bounds of the button.
///
/// Defaults to 16.0 pixels.
final EdgeInsets padding;
/// The color of the button's background.
///
/// Defaults to null which produces a button with no background or border.
final Color color;
/// The callback that is called when the button is tapped or otherwise activated.
///
/// If this is set to null, the button will be disabled.
final VoidCallback onPressed;
/// Whether the button is enabled or disabled. Buttons are disabled by default. To
/// enable a button, set its [onPressed] property to a non-null value.
bool get enabled => onPressed != null;
@override
_CupertinoButtonState createState() => new _CupertinoButtonState();
@override
void debugFillDescription(List<String> description) {
super.debugFillDescription(description);
if (!enabled)
description.add('disabled');
}
}
class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProviderStateMixin {
// Eyeballed values. Feel free to tweak.
static const Duration kFadeOutDuration = const Duration(milliseconds: 10);
static const Duration kFadeInDuration = const Duration(milliseconds: 350);
AnimationController _animationController;
@override
void initState() {
super.initState();
_animationController = new AnimationController(
duration: const Duration(milliseconds: 200),
value: 1.0,
vsync: this,
);
}
@override
void dispose() {
_animationController.dispose();
_animationController = null;
super.dispose();
}
void _handleTapDown(PointerDownEvent event) {
_animationController.animateTo(0.1, duration: kFadeOutDuration);
}
void _handleTapUp(PointerUpEvent event) {
_animationController.animateTo(1.0, duration: kFadeInDuration);
}
void _handleTapCancel(PointerCancelEvent event) {
_animationController.animateTo(1.0, duration: kFadeInDuration);
}
@override
Widget build(BuildContext context) {
final bool enabled = config.enabled;
final Color backgroundColor = config.color;
return new Listener(
onPointerDown: enabled ? _handleTapDown : null,
onPointerUp: enabled ? _handleTapUp : null,
onPointerCancel: enabled ? _handleTapCancel : null,
child: new GestureDetector(
onTap: config.onPressed,
child: new ConstrainedBox(
constraints: const BoxConstraints(minWidth: 48.0, minHeight: 48.0),
child: new FadeTransition(
opacity: new CurvedAnimation(
parent: _animationController,
curve: Curves.decelerate,
),
child: new DecoratedBox(
decoration: new BoxDecoration(
borderRadius: const BorderRadius.all(const Radius.circular(8.0)),
backgroundColor: backgroundColor != null && !enabled
? CupertinoButton.kDisabledBackground
: backgroundColor,
),
child: new Padding(
padding: config.padding != null
? config.padding
: backgroundColor != null
? _kBackgroundButtonPadding
: _kButtonPadding,
child: new Center(
widthFactor: 1.0,
heightFactor: 1.0,
child: new DefaultTextStyle(
style: backgroundColor != null
? _kBackgroundButtonTextStyle
: enabled
? _kButtonTextStyle
: _kDisabledButtonTextStyle,
child: config.child,
),
),
),
),
),
),
),
);
}
}

View File

@@ -10,8 +10,8 @@ import 'package:meta/meta.dart';
const TextStyle _kCupertinoDialogTitleStyle = const TextStyle(
fontFamily: '.SF UI Display',
inherit: false,
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontSize: 17.0,
fontWeight: FontWeight.w600,
color: const Color(0xFF000000),
height: 1.35,
textBaseline: TextBaseline.alphabetic,
@@ -127,7 +127,7 @@ class CupertinoAlertDialog extends StatelessWidget {
if (title != null) {
children.add(new Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
padding: const EdgeInsets.only(left: 20.0, right: 20.0, bottom: 12.0),
child: new DefaultTextStyle(
style: _kCupertinoDialogTitleStyle,
textAlign: TextAlign.center,

View File

@@ -0,0 +1,101 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/cupertino.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Layout minimum size', (WidgetTester tester) async {
await tester.pumpWidget(
new Center(child: new CupertinoButton(child: new Text(' '), onPressed: null))
);
RenderBox buttonBox = tester.renderObject(find.byType(CupertinoButton));
expect(
buttonBox.size,
greaterThanOrEqualTo(const Size.square(48.0)),
);
expect(
buttonBox.size,
lessThan(const Size.square(100.0)),
);
});
testWidgets('Size grows with text', (WidgetTester tester) async {
await tester.pumpWidget(
new Center(child: new CupertinoButton(child: new Text('blah blah blah'), onPressed: null))
);
RenderBox buttonBox = tester.renderObject(find.byType(CupertinoButton));
expect(
buttonBox.size.width,
greaterThan(48.0),
);
});
testWidgets('Button with background is wider', (WidgetTester tester) async {
await tester.pumpWidget(new Center(child: new CupertinoButton(
child: new Text(' '),
onPressed: null,
color: new Color(0xFFFFFFFF),
)));
RenderBox buttonBox = tester.renderObject(find.byType(CupertinoButton));
expect(
buttonBox.size.width,
greaterThan(120.0),
);
});
testWidgets('Custom padding', (WidgetTester tester) async {
await tester.pumpWidget(new Center(child: new CupertinoButton(
child: new Text(' '),
onPressed: null,
padding: new EdgeInsets.all(100.0),
)));
RenderBox buttonBox = tester.renderObject(find.byType(CupertinoButton));
expect(
buttonBox.size,
greaterThan(const Size.square(100.0)),
);
});
testWidgets('Button takes taps', (WidgetTester tester) async {
bool value = false;
await tester.pumpWidget(
new StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return new Center(
child: new CupertinoButton(
child: new Text('Tap me'),
onPressed: () {
setState(() {
value = true;
});
},
),
);
},
),
);
expect(value, isFalse);
// No animating by default.
expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
await tester.tap(find.byType(CupertinoButton));
expect(value, isTrue);
// Animates.
expect(SchedulerBinding.instance.transientCallbackCount, equals(1));
});
testWidgets('Disabled button doesn\'t animate', (WidgetTester tester) async {
await tester.pumpWidget(new Center(child: new CupertinoButton(
child: new Text('Tap me'),
onPressed: null,
)));
expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
await tester.tap(find.byType(CupertinoButton));
// Still doesn't animate.
expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
});
}