main.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import 'package:flutter/material.dart';
  2. void main() => runApp(const MyApp());
  3. class MyApp extends StatelessWidget {
  4. const MyApp({super.key});
  5. @override
  6. Widget build(BuildContext context) {
  7. Widget titleSection = Container(
  8. padding: const EdgeInsets.all(32),
  9. child: Row(
  10. children: [
  11. Expanded(
  12. /*1*/
  13. child: Column(
  14. crossAxisAlignment: CrossAxisAlignment.start,
  15. children: [
  16. /*2*/
  17. Container(
  18. padding: const EdgeInsets.only(bottom: 8),
  19. child: const Text(
  20. 'Oeschinen Lake Campground',
  21. style: TextStyle(
  22. fontWeight: FontWeight.bold,
  23. ),
  24. ),
  25. ),
  26. Text(
  27. 'Kandersteg, Switzerland',
  28. style: TextStyle(
  29. color: Colors.grey[500],
  30. ),
  31. ),
  32. ],
  33. ),
  34. ),
  35. const FavoriteWidget(),
  36. ],
  37. ),
  38. );
  39. Color color = Theme.of(context).primaryColor;
  40. Widget buttonSection = SizedBox(
  41. child: Row(
  42. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  43. children: [
  44. _buildButtonColumn(color, Icons.call, 'CALL'),
  45. _buildButtonColumn(color, Icons.near_me, 'ROUTE'),
  46. _buildButtonColumn(color, Icons.share, 'SHARE'),
  47. ],
  48. ),
  49. );
  50. Widget textSection = Container(
  51. padding: const EdgeInsets.all(32),
  52. child: const Text(
  53. 'Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese '
  54. 'Alps. Situated 1,578 meters above sea level, it is one of the '
  55. 'larger Alpine Lakes. A gondola ride from Kandersteg, followed by a '
  56. 'half-hour walk through pastures and pine forest, leads you to the '
  57. 'lake, which warms to 20 degrees Celsius in the summer. Activities '
  58. 'enjoyed here include rowing, and riding the summer toboggan run.',
  59. softWrap: true,
  60. ),
  61. );
  62. return MaterialApp(
  63. title: 'Flutter layout demo',
  64. home: Scaffold(
  65. appBar: AppBar(
  66. title: const Text('Flutter layout demo'),
  67. ),
  68. body: ListView(
  69. children: [
  70. Image.asset(
  71. 'images/lake.jpg',
  72. width: 600,
  73. height: 240,
  74. fit: BoxFit.cover,
  75. ),
  76. titleSection,
  77. buttonSection,
  78. textSection,
  79. ],
  80. ),
  81. ),
  82. );
  83. }
  84. Column _buildButtonColumn(Color color, IconData icon, String label) {
  85. return Column(
  86. mainAxisSize: MainAxisSize.min,
  87. mainAxisAlignment: MainAxisAlignment.center,
  88. children: [
  89. Icon(icon, color: color),
  90. Container(
  91. margin: const EdgeInsets.only(top: 8),
  92. child: Text(
  93. label,
  94. style: TextStyle(
  95. fontSize: 12,
  96. fontWeight: FontWeight.w400,
  97. color: color,
  98. ),
  99. ),
  100. ),
  101. ],
  102. );
  103. }
  104. }
  105. // #docregion FavoriteWidget
  106. class FavoriteWidget extends StatefulWidget {
  107. const FavoriteWidget({super.key});
  108. @override
  109. State<FavoriteWidget> createState() => _FavoriteWidgetState();
  110. }
  111. // #enddocregion FavoriteWidget
  112. // #docregion _FavoriteWidgetState, _FavoriteWidgetState-fields, _FavoriteWidgetState-build
  113. class _FavoriteWidgetState extends State<FavoriteWidget> {
  114. // #enddocregion _FavoriteWidgetState-build
  115. bool _isFavorited = true;
  116. int _favoriteCount = 41;
  117. // #enddocregion _FavoriteWidgetState-fields
  118. // #docregion _toggleFavorite
  119. void _toggleFavorite() {
  120. setState(() {
  121. if (_isFavorited) {
  122. _favoriteCount -= 1;
  123. _isFavorited = false;
  124. } else {
  125. _favoriteCount += 1;
  126. _isFavorited = true;
  127. }
  128. });
  129. }
  130. // #enddocregion _toggleFavorite
  131. // #docregion _FavoriteWidgetState-build
  132. @override
  133. Widget build(BuildContext context) {
  134. return Row(
  135. mainAxisSize: MainAxisSize.min,
  136. children: [
  137. Container(
  138. padding: const EdgeInsets.all(0),
  139. child: IconButton(
  140. padding: const EdgeInsets.all(0),
  141. alignment: Alignment.centerRight,
  142. icon: (_isFavorited
  143. ? const Icon(Icons.star)
  144. : const Icon(Icons.star_border)),
  145. color: Colors.red[500],
  146. onPressed: _toggleFavorite,
  147. ),
  148. ),
  149. SizedBox(
  150. width: 18,
  151. child: SizedBox(
  152. child: Text('$_favoriteCount'),
  153. ),
  154. ),
  155. ],
  156. );
  157. }
  158. // #docregion _FavoriteWidgetState-fields
  159. }