|
| 1 | +### Align |
| 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 alignment = [ |
| 13 | + Alignment.topLeft, |
| 14 | + Alignment.topCenter, |
| 15 | + Alignment.topRight, |
| 16 | + Alignment.centerLeft, |
| 17 | + Alignment.center, |
| 18 | + Alignment.centerRight, |
| 19 | + Alignment.bottomLeft, |
| 20 | + Alignment.bottomCenter, |
| 21 | + Alignment.bottomRight |
| 22 | + ]; |
| 23 | + List alignmentValue1 = ['topLeft', 'topCenter', 'topRight']; |
| 24 | + List alignmentValue2 = ['centerLeft', 'center', 'centerRight']; |
| 25 | + List alignmentValue3 = ['bottomLeft', 'bottomCenter', 'bottomRight']; |
| 26 | + int alignmentIndex = 0; |
| 27 | + String showText = 'topLeft'; |
| 28 | +
|
| 29 | + @override |
| 30 | + Widget build(BuildContext context) { |
| 31 | + return Scaffold( |
| 32 | + appBar: AppBar(title: Text('Align Demo'),), |
| 33 | + body: ListView( |
| 34 | + children: <Widget>[ |
| 35 | + SizedBox( |
| 36 | + child: Text('修改alignment的值'), |
| 37 | + ), |
| 38 | + Row( |
| 39 | + children: List.generate(3, (index) { |
| 40 | + return FlatButton( |
| 41 | + child: Text('${alignmentValue1[index]}', style: TextStyle(fontSize: 12.0),), |
| 42 | + onPressed: (){ |
| 43 | + setState(() { |
| 44 | + alignmentIndex = index; |
| 45 | + showText = alignmentValue1[index]; |
| 46 | + }); |
| 47 | + }, |
| 48 | + ); |
| 49 | + }), |
| 50 | + ), |
| 51 | + Row( |
| 52 | + children: List.generate(3, (index) { |
| 53 | + return FlatButton( |
| 54 | + child: Text('${alignmentValue2[index]}', style: TextStyle(fontSize: 12.0),), |
| 55 | + onPressed: (){ |
| 56 | + setState(() { |
| 57 | + alignmentIndex = index + 3; |
| 58 | + showText = alignmentValue2[index]; |
| 59 | + }); |
| 60 | + }, |
| 61 | + ); |
| 62 | + }), |
| 63 | + ), |
| 64 | + Row( |
| 65 | + children: List.generate(3, (index) { |
| 66 | + return FlatButton( |
| 67 | + child: Text('${alignmentValue3[index]}', style: TextStyle(fontSize: 11.0),), |
| 68 | + onPressed: (){ |
| 69 | + setState(() { |
| 70 | + alignmentIndex = index + 6; |
| 71 | + showText = alignmentValue3[index]; |
| 72 | + }); |
| 73 | + }, |
| 74 | + ); |
| 75 | + }), |
| 76 | + ), |
| 77 | + Center( |
| 78 | + child: Container( |
| 79 | + width: 200.0, |
| 80 | + height: 200.0, |
| 81 | + decoration: BoxDecoration( |
| 82 | + image: DecorationImage( |
| 83 | + image: NetworkImage('https://wx3.sinaimg.cn/large/006bllTKly1fmvmfjfn6pj30v90v9785.jpg'), |
| 84 | + fit: BoxFit.cover |
| 85 | + ) |
| 86 | + ), |
| 87 | + child: Align( |
| 88 | + widthFactor: 1, |
| 89 | + heightFactor: 1, |
| 90 | + alignment: alignment[alignmentIndex], |
| 91 | + child: Text( |
| 92 | + '$showText', |
| 93 | + style: TextStyle( |
| 94 | + fontSize: 30.0, |
| 95 | + color: Theme.of(context).primaryColor, |
| 96 | + fontWeight: FontWeight.bold), |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ) |
| 101 | + ], |
| 102 | + ), |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
0 commit comments