|
| 1 | +### FittedBox |
| 2 | + |
| 3 | +``` dart |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +
|
| 6 | +class Index extends StatefulWidget { |
| 7 | + @override |
| 8 | + _IndexState createState() => _IndexState(); |
| 9 | +} |
| 10 | +
|
| 11 | +class _IndexState extends State<Index> { |
| 12 | + List fit = [ |
| 13 | + BoxFit.contain, |
| 14 | + BoxFit.cover, |
| 15 | + BoxFit.fill, |
| 16 | + BoxFit.fitHeight, |
| 17 | + BoxFit.fitWidth, |
| 18 | + BoxFit.none, |
| 19 | + BoxFit.scaleDown |
| 20 | + ]; |
| 21 | + List fitValue1 = ['contain', 'cover', 'fill']; |
| 22 | + List fitValue2 = ['fitHeight', 'fitWidth']; |
| 23 | + List fitValue3 = ['none', 'scaleDown']; |
| 24 | + int fitIndex = 0; |
| 25 | +
|
| 26 | + List alignment = [ |
| 27 | + Alignment.topLeft, |
| 28 | + Alignment.topCenter, |
| 29 | + Alignment.topRight, |
| 30 | + Alignment.centerLeft, |
| 31 | + Alignment.center, |
| 32 | + Alignment.centerRight, |
| 33 | + Alignment.bottomLeft, |
| 34 | + Alignment.bottomCenter, |
| 35 | + Alignment.bottomRight |
| 36 | + ]; |
| 37 | + List alignmentValue1 = ['topLeft', 'topCenter', 'topRight']; |
| 38 | + List alignmentValue2 = ['centerLeft', 'center', 'centerRight']; |
| 39 | + List alignmentValue3 = ['bottomLeft', 'bottomCenter', 'bottomRight']; |
| 40 | + int alignmentIndex = 0; |
| 41 | +
|
| 42 | + @override |
| 43 | + Widget build(BuildContext context) { |
| 44 | + return Scaffold( |
| 45 | + appBar: AppBar(title: Text('FittedBox Demo'),), |
| 46 | + body: ListView( |
| 47 | + children: <Widget>[ |
| 48 | + SizedBox( |
| 49 | + child: Text('修改fit的值'), |
| 50 | + ), |
| 51 | + Row( |
| 52 | + children: List.generate(3, (index) { |
| 53 | + return FlatButton( |
| 54 | + child: Text(fitValue1[index]), |
| 55 | + onPressed: (){ |
| 56 | + setState(() { |
| 57 | + fitIndex = index; |
| 58 | + }); |
| 59 | + }, |
| 60 | + ); |
| 61 | + }), |
| 62 | + ), |
| 63 | + Row( |
| 64 | + children: List.generate(2, (index) { |
| 65 | + return FlatButton( |
| 66 | + child: Text(fitValue2[index]), |
| 67 | + onPressed: (){ |
| 68 | + setState(() { |
| 69 | + fitIndex = index + 3; |
| 70 | + }); |
| 71 | + }, |
| 72 | + ); |
| 73 | + }), |
| 74 | + ), |
| 75 | + Row( |
| 76 | + children: List.generate(2, (index) { |
| 77 | + return FlatButton( |
| 78 | + child: Text(fitValue3[index]), |
| 79 | + onPressed: (){ |
| 80 | + setState(() { |
| 81 | + fitIndex = index + 5; |
| 82 | + }); |
| 83 | + }, |
| 84 | + ); |
| 85 | + }), |
| 86 | + ), |
| 87 | + SizedBox( |
| 88 | + child: Text('修改alignment的值'), |
| 89 | + ), |
| 90 | + Row( |
| 91 | + children: List.generate(3, (index) { |
| 92 | + return FlatButton( |
| 93 | + child: Text('${alignmentValue1[index]}', style: TextStyle(fontSize: 12.0),), |
| 94 | + onPressed: (){ |
| 95 | + setState(() { |
| 96 | + alignmentIndex = index; |
| 97 | + }); |
| 98 | + }, |
| 99 | + ); |
| 100 | + }), |
| 101 | + ), |
| 102 | + Row( |
| 103 | + children: List.generate(3, (index) { |
| 104 | + return FlatButton( |
| 105 | + child: Text('${alignmentValue2[index]}', style: TextStyle(fontSize: 12.0),), |
| 106 | + onPressed: (){ |
| 107 | + setState(() { |
| 108 | + alignmentIndex = index + 3; |
| 109 | + }); |
| 110 | + }, |
| 111 | + ); |
| 112 | + }), |
| 113 | + ), |
| 114 | + Row( |
| 115 | + children: List.generate(3, (index) { |
| 116 | + return FlatButton( |
| 117 | + child: Text('${alignmentValue3[index]}', style: TextStyle(fontSize: 11.0),), |
| 118 | + onPressed: (){ |
| 119 | + setState(() { |
| 120 | + alignmentIndex = index + 6; |
| 121 | + }); |
| 122 | + }, |
| 123 | + ); |
| 124 | + }), |
| 125 | + ), |
| 126 | + Center( |
| 127 | + child: Container( |
| 128 | + width: 200.0, |
| 129 | + height: 200.0, |
| 130 | + color: Color(0xfff8bbd0), |
| 131 | + // color: Theme.of(context).primaryColor, |
| 132 | + constraints: BoxConstraints( |
| 133 | + maxWidth: 200.0 |
| 134 | + ), |
| 135 | + margin: const EdgeInsets.all(10.0), |
| 136 | + child: FittedBox( |
| 137 | + fit: fit[fitIndex], |
| 138 | + alignment: alignment[alignmentIndex], |
| 139 | + child: Container( |
| 140 | + color: Color(0xfff48fb1), |
| 141 | + child: Text('FittedBox', style: TextStyle(color: Colors.white),), |
| 142 | + ), |
| 143 | + ), |
| 144 | + ), |
| 145 | + ) |
| 146 | + ], |
| 147 | + ) |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
0 commit comments