14.3 去除ConstrainedBox的多重限制
1.源代码
import 'package:flutter/material.dart';
void main () => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Widget blueBox = DecoratedBox(decoration: BoxDecoration(color: Colors.blue));
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Column(
children: <Widget>[
ConstrainedBox(
constraints: BoxConstraints(minHeight: 100.0,minWidth: 60.0),
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 80.0,minWidth: 80.0),
child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
),
),
ConstrainedBox(
constraints: BoxConstraints(minHeight: 80.0,minWidth: 80.0),
child: UnconstrainedBox(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 100.0,minWidth: 60.0),
child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
),
)
),
],
)
)
)
);
}
}
2.解释源代码
import 'package:flutter/material.dart';
void main () => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Widget blueBox = DecoratedBox(decoration: BoxDecoration(color: Colors.blue));
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Column(
children: <Widget>[
ConstrainedBox(
constraints: BoxConstraints(minHeight: 100.0,minWidth: 60.0),
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 80.0,minWidth: 80.0),
child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
),
),
ConstrainedBox(
constraints: BoxConstraints(minHeight: 80.0,minWidth: 80.0),
child: UnconstrainedBox(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 100.0,minWidth: 60.0),
child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
),
)
),
],
)
)
)
);
}
}
3.效果图