Adding multicolumn cupertino picker to the Gallery. (#19284)
* Add sample of multicolumn picker to Flutter Gallery * Modify CupertinoPicker to be able to be rendered off-center.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import 'cupertino_navigation_demo.dart' show coolColorNames;
|
||||
|
||||
@@ -17,9 +18,12 @@ class CupertinoPickerDemo extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
|
||||
int _selectedItemIndex = 0;
|
||||
int _selectedColorIndex = 0;
|
||||
|
||||
Widget _buildMenu() {
|
||||
int _selectedHour = 0;
|
||||
int _selectedMinute = 0;
|
||||
|
||||
Widget _buildMenu(List<Widget> children) {
|
||||
return new Container(
|
||||
decoration: const BoxDecoration(
|
||||
color: CupertinoColors.white,
|
||||
@@ -42,13 +46,7 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
|
||||
),
|
||||
child: new Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: <Widget>[
|
||||
const Text('Favorite Color'),
|
||||
new Text(
|
||||
coolColorNames[_selectedItemIndex],
|
||||
style: const TextStyle(color: CupertinoColors.inactiveGray),
|
||||
),
|
||||
],
|
||||
children: children,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -56,10 +54,82 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBottomPicker() {
|
||||
Widget _buildColorPicker() {
|
||||
final FixedExtentScrollController scrollController =
|
||||
new FixedExtentScrollController(initialItem: _selectedItemIndex);
|
||||
new FixedExtentScrollController(initialItem: _selectedColorIndex);
|
||||
return new CupertinoPicker(
|
||||
scrollController: scrollController,
|
||||
itemExtent: _kPickerItemHeight,
|
||||
backgroundColor: CupertinoColors.white,
|
||||
onSelectedItemChanged: (int index) {
|
||||
setState(() {
|
||||
_selectedColorIndex = index;
|
||||
});
|
||||
},
|
||||
children: new List<Widget>.generate(coolColorNames.length, (int index) {
|
||||
return new Center(child:
|
||||
new Text(coolColorNames[index]),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAlarmPicker() {
|
||||
return new Row(
|
||||
children: <Widget>[
|
||||
new Expanded(
|
||||
child: new CupertinoPicker(
|
||||
scrollController: new FixedExtentScrollController(
|
||||
initialItem: _selectedHour,
|
||||
),
|
||||
offAxisFraction: -0.5,
|
||||
useMagnifier: true,
|
||||
magnification: 1.1,
|
||||
itemExtent: _kPickerItemHeight,
|
||||
backgroundColor: CupertinoColors.white,
|
||||
onSelectedItemChanged: (int index) {
|
||||
setState(() {
|
||||
_selectedHour = index;
|
||||
});
|
||||
},
|
||||
children: new List<Widget>.generate(24, (int index) {
|
||||
return new Container(
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 32.0),
|
||||
child: new Text(index.toString()),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
new Expanded(
|
||||
child: new CupertinoPicker(
|
||||
scrollController: new FixedExtentScrollController(
|
||||
initialItem: _selectedMinute,
|
||||
),
|
||||
offAxisFraction: 0.5,
|
||||
useMagnifier: true,
|
||||
magnification: 1.1,
|
||||
itemExtent: _kPickerItemHeight,
|
||||
backgroundColor: CupertinoColors.white,
|
||||
onSelectedItemChanged: (int index) {
|
||||
setState(() {
|
||||
_selectedMinute = index;
|
||||
});
|
||||
},
|
||||
children: new List<Widget>.generate(60, (int index) {
|
||||
return new Container(
|
||||
alignment: Alignment.centerLeft,
|
||||
padding: const EdgeInsets.only(left: 32.0),
|
||||
child: new Text(index.toString()),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBottomPicker(Widget picker) {
|
||||
return new Container(
|
||||
height: _kPickerSheetHeight,
|
||||
color: CupertinoColors.white,
|
||||
@@ -72,21 +142,7 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
|
||||
// Blocks taps from propagating to the modal sheet and popping.
|
||||
onTap: () {},
|
||||
child: new SafeArea(
|
||||
child: new CupertinoPicker(
|
||||
scrollController: scrollController,
|
||||
itemExtent: _kPickerItemHeight,
|
||||
backgroundColor: CupertinoColors.white,
|
||||
onSelectedItemChanged: (int index) {
|
||||
setState(() {
|
||||
_selectedItemIndex = index;
|
||||
});
|
||||
},
|
||||
children: new List<Widget>.generate(coolColorNames.length, (int index) {
|
||||
return new Center(child:
|
||||
new Text(coolColorNames[index]),
|
||||
);
|
||||
}),
|
||||
),
|
||||
child: picker,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -95,6 +151,7 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final String time = new DateFormat.Hm().format(new DateTime(2018, 1, 1, _selectedHour, _selectedMinute));
|
||||
return new Scaffold(
|
||||
appBar: new AppBar(
|
||||
title: const Text('Cupertino Picker'),
|
||||
@@ -115,11 +172,42 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
|
||||
await showModalBottomSheet<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return _buildBottomPicker();
|
||||
return _buildBottomPicker(_buildColorPicker());
|
||||
},
|
||||
);
|
||||
},
|
||||
child: _buildMenu(),
|
||||
child: _buildMenu(
|
||||
<Widget>[
|
||||
const Text('Favorite Color'),
|
||||
new Text(
|
||||
coolColorNames[_selectedColorIndex],
|
||||
style: const TextStyle(
|
||||
color: CupertinoColors.inactiveGray
|
||||
),
|
||||
),
|
||||
]
|
||||
),
|
||||
),
|
||||
new GestureDetector(
|
||||
onTap: () async {
|
||||
await showModalBottomSheet<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return _buildBottomPicker(_buildAlarmPicker());
|
||||
},
|
||||
);
|
||||
},
|
||||
child: _buildMenu(
|
||||
<Widget>[
|
||||
const Text('Alarm'),
|
||||
new Text(
|
||||
time,
|
||||
style: const TextStyle(
|
||||
color: CupertinoColors.inactiveGray
|
||||
),
|
||||
),
|
||||
]
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user