Created
July 30, 2018 10:45
-
-
Save long1eu/e88cecd87b2d6e5db5f05db4a38ec284 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(new MaterialApp(home: new BottomSheetColor())); | |
} | |
class BottomSheetColor extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: new AppBar(), | |
floatingActionButton: Theme( | |
data: Theme.of(context).copyWith(canvasColor: Colors.transparent), | |
child: Builder( | |
builder: (BuildContext context) { | |
return new FloatingActionButton( | |
onPressed: () { | |
showModalBottomSheet<void>( | |
context: context, | |
builder: (BuildContext context) { | |
final List<String> items = <String>[ | |
'Cancel', | |
'Item 1', | |
'Item 2', | |
'Item 3', | |
]; | |
return CupertinoActionSheet( | |
items: items, | |
onTap: (index) { | |
print(index); | |
}, | |
); | |
}); | |
}, | |
); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class CupertinoActionSheet extends StatelessWidget { | |
CupertinoActionSheet({Key key, @required this.items, @required this.onTap}) | |
: assert(items.length > 1), | |
super(key: key); | |
final List<String> items; | |
final void Function(int index) onTap; | |
@override | |
Widget build(BuildContext context) { | |
return SafeArea( | |
bottom: true, | |
child: Builder( | |
builder: (BuildContext context) { | |
final List<Widget> widgets = <Widget>[]; | |
final BoxDecoration boxDecoration = new BoxDecoration( | |
color: Colors.white.withOpacity(.9), | |
borderRadius: new BorderRadius.circular(8.0), | |
); | |
widgets.add(InkWell( | |
onTap: () { | |
onTap(0); | |
Navigator.pop(context); | |
}, | |
child: new Container( | |
width: double.infinity, | |
height: 48.0, | |
margin: const EdgeInsets.symmetric(horizontal: 16.0).copyWith(top: 8.0), | |
alignment: AlignmentDirectional.center, | |
decoration: boxDecoration, | |
child: new Text( | |
items[0], | |
style: const TextStyle( | |
fontWeight: FontWeight.w600, | |
fontSize: 18.0, | |
color: CupertinoColors.activeBlue, | |
), | |
), | |
), | |
)); | |
final List<String> remaining = items.sublist(1); | |
widgets.add(new Container( | |
width: double.infinity, | |
alignment: AlignmentDirectional.center, | |
margin: const EdgeInsets.symmetric(horizontal: 16.0), | |
decoration: boxDecoration, | |
child: new Column( | |
children: remaining.fold(<Widget>[], (List<Widget> list, String item) { | |
list.add(InkWell( | |
onTap: () { | |
onTap(items.indexOf(item)); | |
Navigator.pop(context); | |
}, | |
child: new Container( | |
height: 48.0, | |
alignment: AlignmentDirectional.center, | |
child: new Text( | |
item, | |
style: const TextStyle( | |
fontSize: 18.0, | |
color: CupertinoColors.activeBlue, | |
), | |
), | |
), | |
)); | |
if (item != remaining.last) { | |
list.add(new Container( | |
height: 0.5, | |
color: Colors.grey, | |
)); | |
} | |
return list; | |
}), | |
), | |
)); | |
return new Column( | |
mainAxisSize: MainAxisSize.min, | |
children: widgets.reversed.toList(), | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment