Created
August 20, 2020 10:09
-
-
Save JuYeong0413/d9f10702e85604de377e315a6ef724f5 to your computer and use it in GitHub Desktop.
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/material.dart'; | |
class BasicAppBarSample extends StatefulWidget { | |
@override | |
_BasicAppBarSampleState createState() => _BasicAppBarSampleState(); | |
} | |
class _BasicAppBarSampleState extends State<BasicAppBarSample> { | |
Choice _selectedChoice = choices[0]; | |
void _select(Choice choice) { | |
setState(() { | |
_selectedChoice = choice; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
const double windowPaddingTop = 24; | |
double mediaQueryTop; | |
return MediaQuery( | |
data: const MediaQueryData( | |
padding: EdgeInsets.only(top: windowPaddingTop), | |
), | |
child: Builder(builder: (BuildContext context) { | |
mediaQueryTop = MediaQuery.of(context).padding.top; | |
print(mediaQueryTop); | |
return MaterialApp( | |
home: Padding( | |
padding: EdgeInsets.only(top: mediaQueryTop), | |
child: Scaffold( | |
appBar: AppBar( | |
title: const Text('Basic AppBar'), | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon(choices[0].icon), | |
onPressed: () { | |
_select(choices[0]); | |
}, | |
), | |
IconButton( | |
icon: Icon(choices[1].icon), | |
onPressed: () { | |
_select(choices[1]); | |
}, | |
), | |
PopupMenuButton<Choice>( | |
onSelected: _select, | |
initialValue: _selectedChoice, // initialValue added | |
itemBuilder: (BuildContext context) { | |
return choices | |
.skip(2) | |
.map<PopupMenuItem<Choice>>((Choice choice) { | |
return PopupMenuItem<Choice>( | |
value: choice, | |
child: Text(choice.title), | |
); | |
}).toList(); | |
}, | |
), | |
], | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: ChoiceCard(choice: _selectedChoice), | |
), | |
), | |
), | |
); | |
}), | |
); | |
} | |
} | |
class Choice { | |
const Choice({this.title, this.icon}); | |
final String title; | |
final IconData icon; | |
} | |
const List<Choice> choices = <Choice>[ | |
Choice(title: 'Car', icon: Icons.directions_car), | |
Choice(title: 'Bicycle', icon: Icons.directions_bike), | |
Choice(title: 'Boat', icon: Icons.directions_boat), | |
Choice(title: 'Bus', icon: Icons.directions_bus), | |
Choice(title: 'Train', icon: Icons.directions_railway), | |
Choice(title: 'Walk', icon: Icons.directions_walk), | |
]; | |
class ChoiceCard extends StatelessWidget { | |
const ChoiceCard({Key key, this.choice}) : super(key: key); | |
final Choice choice; | |
@override | |
Widget build(BuildContext context) { | |
final TextStyle textStyle = Theme.of(context).textTheme.headline4; | |
return Card( | |
color: Colors.white, | |
child: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: <Widget>[ | |
Icon(choice.icon, size: 128.0, color: textStyle.color), | |
Text(choice.title, style: textStyle), | |
], | |
), | |
), | |
); | |
} | |
} | |
void main() { | |
runApp(MaterialApp(home: BasicAppBarSample())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment