Skip to content

Instantly share code, notes, and snippets.

@arthurdenner
Created April 27, 2020 18:48
Show Gist options
  • Save arthurdenner/4ce96797ee9d256d3b28c9aa0a02562d to your computer and use it in GitHub Desktop.
Save arthurdenner/4ce96797ee9d256d3b28c9aa0a02562d to your computer and use it in GitHub Desktop.
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) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Basic AppBar'),
actions: <Widget>[
PopupMenuButton<Choice>(
onSelected: _select,
initialValue: _selectedChoice,
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(BasicAppBarSample());
}
import 'package:flutter/material.dart';
class BasicAppBarSample extends StatefulWidget {
@override
_BasicAppBarSampleState createState() => _BasicAppBarSampleState();
}
class _BasicAppBarSampleState extends State<BasicAppBarSample> {
int _selectedValue;
void _select(int value) {
setState(() {
_selectedValue = value;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Basic AppBar'),
actions: <Widget>[
PopupMenuButton<int>(
onSelected: _select,
initialValue: _selectedValue,
itemBuilder: (BuildContext context) {
return List.generate(100, (int value) {
return PopupMenuItem<int>(
value: value,
child: Text('Item $value'),
);
}).toList();
},
),
],
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Text('Item $_selectedValue'),
),
),
),
);
}
}
void main() {
runApp(BasicAppBarSample());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment