Created
March 23, 2020 16:33
-
-
Save Andrious/2035e0a28ae8cf7b2349f339a86b8e4d to your computer and use it in GitHub Desktop.
RandomWordsApple class for the 'Write Your First App' example.
This file contains 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' | |
show | |
Brightness, | |
BuildContext, | |
Column, | |
CrossAxisAlignment, | |
CupertinoColors, | |
CupertinoPageScaffold, | |
CupertinoSliverNavigationBar, | |
CustomScrollView, | |
EdgeInsets, | |
GestureDetector, | |
Key, | |
MainAxisAlignment, | |
MediaQuery, | |
Row, | |
SizedBox, | |
SliverChildBuilderDelegate, | |
SliverList, | |
SliverSafeArea, | |
State, | |
StatefulWidget, | |
Text, | |
TextStyle, | |
Widget; | |
import 'package:flutter/material.dart' show Divider; | |
import '../home.dart' show RandomWords; | |
import '../../../controller/home/controller.dart' show Controller; | |
class RandomWordsApple extends State<RandomWords> { | |
RandomWordsApple() { | |
controller = Controller(this); | |
} | |
Controller controller; | |
@override | |
Widget build(BuildContext context) { | |
return CupertinoPageScaffold( | |
child: CustomScrollView( | |
slivers: <Widget>[ | |
const CupertinoSliverNavigationBar( | |
largeTitle: Text('Startup Name Generator'), | |
), | |
SliverSafeArea( | |
top: false, | |
minimum: const EdgeInsets.only(top: 8), | |
sliver: SliverList( | |
delegate: SliverChildBuilderDelegate( | |
(context, i) { | |
if (i.isOdd) return Divider(); | |
controller.build(i); | |
return CupertinoListTile( | |
title: controller.data, | |
trailing: controller.icon, | |
onTap: () { | |
controller.onTap(i); | |
}, | |
); | |
}, | |
), | |
), | |
) | |
], | |
), | |
); | |
} | |
} | |
/// Cupertino needs a ListTile equivalent | |
/// https://github.com/flutter/flutter/issues/50668#issuecomment-593060684 | |
class CupertinoListTile extends StatefulWidget { | |
const CupertinoListTile({ | |
Key key, | |
this.leading, | |
this.title, | |
this.subtitle, | |
this.trailing, | |
this.onTap, | |
}) : super(key: key); | |
final Widget leading; | |
final String title; | |
final String subtitle; | |
final Widget trailing; | |
final Function onTap; | |
@override | |
_StatefulStateCupertino createState() => _StatefulStateCupertino(); | |
} | |
class _StatefulStateCupertino extends State<CupertinoListTile> { | |
@override | |
Widget build(BuildContext context) { | |
Widget leading; | |
if (widget.leading == null) { | |
leading = const SizedBox(); | |
} else { | |
leading = widget.leading; | |
} | |
Widget trailing; | |
if (widget.trailing == null) { | |
trailing = const SizedBox(); | |
} else { | |
trailing = widget.trailing; | |
} | |
return GestureDetector( | |
onTap: () { | |
if (widget.onTap != null) widget.onTap(); | |
}, | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: <Widget>[ | |
Row( | |
children: <Widget>[ | |
leading, | |
SizedBox(width: 10), | |
Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: columnChildren(context), | |
), | |
], | |
), | |
trailing, | |
], | |
), | |
); | |
} | |
List<Widget> columnChildren(BuildContext context) { | |
List<Widget> children = List(); | |
bool isDark = MediaQuery.of(context).platformBrightness == Brightness.dark; | |
Widget title = widget.title != null | |
? Text( | |
widget.title, | |
style: TextStyle( | |
fontSize: 25.0, | |
color: isDark ? CupertinoColors.white : CupertinoColors.black), | |
) | |
: const SizedBox(); | |
children.add(title); | |
if (widget.subtitle != null) | |
children.add(Text(widget.subtitle, | |
style: TextStyle(color: CupertinoColors.systemGrey))); | |
return children; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment