|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// Flutter code sample for [AxisDirection]s. |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | + |
| 9 | +void main() => runApp(const ExampleApp()); |
| 10 | + |
| 11 | +class ExampleApp extends StatelessWidget { |
| 12 | + const ExampleApp({super.key}); |
| 13 | + |
| 14 | + static const String _title = 'Flutter Code Sample'; |
| 15 | + |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return const MaterialApp( |
| 19 | + title: _title, |
| 20 | + home: MyWidget(), |
| 21 | + ); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class MyWidget extends StatefulWidget { |
| 26 | + const MyWidget({ super.key }); |
| 27 | + |
| 28 | + @override |
| 29 | + State<MyWidget> createState() => _MyWidgetState(); |
| 30 | +} |
| 31 | + |
| 32 | +class _MyWidgetState extends State<MyWidget> { |
| 33 | + final List<String> _alphabet = <String>[ |
| 34 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
| 35 | + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 36 | + ]; |
| 37 | + final Widget _spacer = const SizedBox.square(dimension: 10); |
| 38 | + AxisDirection _axisDirection = AxisDirection.down; |
| 39 | + |
| 40 | + Widget _getArrows() { |
| 41 | + final Widget arrow; |
| 42 | + switch(_axisDirection) { |
| 43 | + case AxisDirection.up: |
| 44 | + arrow = const Icon(Icons.arrow_upward_rounded); |
| 45 | + return Row( |
| 46 | + mainAxisAlignment: MainAxisAlignment.center, |
| 47 | + children: <Widget>[ arrow, arrow ], |
| 48 | + ); |
| 49 | + case AxisDirection.down: |
| 50 | + arrow = const Icon(Icons.arrow_downward_rounded); |
| 51 | + return Row( |
| 52 | + mainAxisAlignment: MainAxisAlignment.center, |
| 53 | + children: <Widget>[ arrow, arrow ], |
| 54 | + ); |
| 55 | + case AxisDirection.left: |
| 56 | + arrow = const Icon(Icons.arrow_back_rounded); |
| 57 | + return Column( |
| 58 | + mainAxisAlignment: MainAxisAlignment.center, |
| 59 | + children: <Widget>[ arrow, arrow ], |
| 60 | + ); |
| 61 | + case AxisDirection.right: |
| 62 | + arrow = const Icon(Icons.arrow_forward_rounded); |
| 63 | + return Column( |
| 64 | + mainAxisAlignment: MainAxisAlignment.center, |
| 65 | + children: <Widget>[ arrow, arrow ], |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + void _onAxisDirectionChanged(AxisDirection? axisDirection) { |
| 71 | + if (axisDirection != null && axisDirection != _axisDirection) { |
| 72 | + setState(() { |
| 73 | + // Respond to change in axis direction. |
| 74 | + _axisDirection = axisDirection; |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Widget _getLeading() { |
| 80 | + return Container( |
| 81 | + color: Colors.blue[100], |
| 82 | + padding: const EdgeInsets.all(8.0), |
| 83 | + child: Column( |
| 84 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 85 | + children: <Widget>[ |
| 86 | + Text(axisDirectionToAxis(_axisDirection).toString()), |
| 87 | + _spacer, |
| 88 | + Text(_axisDirection.toString()), |
| 89 | + _spacer, |
| 90 | + const Text('GrowthDirection.forward'), |
| 91 | + _spacer, |
| 92 | + _getArrows(), |
| 93 | + ], |
| 94 | + ), |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + Widget _getRadioRow() { |
| 99 | + return DefaultTextStyle( |
| 100 | + style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.white), |
| 101 | + child: RadioTheme( |
| 102 | + data: RadioThemeData( |
| 103 | + fillColor: MaterialStateProperty.all<Color>(Colors.white), |
| 104 | + ), |
| 105 | + child: Padding( |
| 106 | + padding: const EdgeInsets.all(8.0), |
| 107 | + child: Row( |
| 108 | + mainAxisAlignment: MainAxisAlignment.spaceAround, |
| 109 | + children: <Widget>[ |
| 110 | + Radio<AxisDirection>( |
| 111 | + value: AxisDirection.up, |
| 112 | + groupValue: _axisDirection, |
| 113 | + onChanged: _onAxisDirectionChanged, |
| 114 | + ), |
| 115 | + const Text('up'), |
| 116 | + _spacer, |
| 117 | + Radio<AxisDirection>( |
| 118 | + value: AxisDirection.down, |
| 119 | + groupValue: _axisDirection, |
| 120 | + onChanged: _onAxisDirectionChanged, |
| 121 | + ), |
| 122 | + const Text('down'), |
| 123 | + _spacer, |
| 124 | + Radio<AxisDirection>( |
| 125 | + value: AxisDirection.left, |
| 126 | + groupValue: _axisDirection, |
| 127 | + onChanged: _onAxisDirectionChanged, |
| 128 | + ), |
| 129 | + const Text('left'), |
| 130 | + _spacer, |
| 131 | + Radio<AxisDirection>( |
| 132 | + value: AxisDirection.right, |
| 133 | + groupValue: _axisDirection, |
| 134 | + onChanged: _onAxisDirectionChanged, |
| 135 | + ), |
| 136 | + const Text('right'), |
| 137 | + _spacer, |
| 138 | + ], |
| 139 | + ), |
| 140 | + ), |
| 141 | + ), |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + @override |
| 146 | + Widget build(BuildContext context) { |
| 147 | + return Scaffold( |
| 148 | + appBar: AppBar( |
| 149 | + title: const Text('AxisDirections'), |
| 150 | + bottom: PreferredSize( |
| 151 | + preferredSize: const Size.fromHeight(50), |
| 152 | + child: Padding( |
| 153 | + padding: const EdgeInsets.all(8.0), |
| 154 | + child: _getRadioRow(), |
| 155 | + ), |
| 156 | + ), |
| 157 | + ), |
| 158 | + // Also works for ListView.builder, which creates a SliverList for itself. |
| 159 | + // A CustomScrollView allows multiple slivers to be composed together. |
| 160 | + body: CustomScrollView( |
| 161 | + // This method is available to conveniently determine if an scroll |
| 162 | + // view is reversed by its AxisDirection. |
| 163 | + reverse: axisDirectionIsReversed(_axisDirection), |
| 164 | + // This method is available to conveniently convert an AxisDirection |
| 165 | + // into its Axis. |
| 166 | + scrollDirection: axisDirectionToAxis(_axisDirection), |
| 167 | + slivers: <Widget>[ |
| 168 | + SliverList.builder( |
| 169 | + itemCount: 27, |
| 170 | + itemBuilder: (BuildContext context, int index) { |
| 171 | + final Widget child; |
| 172 | + if (index == 0) { |
| 173 | + child = _getLeading(); |
| 174 | + } else { |
| 175 | + child = Container( |
| 176 | + color: index.isEven ? Colors.amber[100] : Colors.amberAccent, |
| 177 | + padding: const EdgeInsets.all(8.0), |
| 178 | + child: Center(child: Text(_alphabet[index - 1])), |
| 179 | + ); |
| 180 | + } |
| 181 | + return Padding( |
| 182 | + padding: const EdgeInsets.all(8.0), |
| 183 | + child: child, |
| 184 | + ); |
| 185 | + } |
| 186 | + ), |
| 187 | + ], |
| 188 | + ), |
| 189 | + ); |
| 190 | + } |
| 191 | +} |
0 commit comments