[Flutter/dart] Image display method according to the display area
Overview
If you want to display an image in the app, I will explain how to display the image according to the area to be displayed.
Method
1 The aspect ratio remains the same and the image is displayed on the full screen
Put the image in a SizedBox with infinite width.
Widget _myImg(){
return SizedBox(
width: double.infinity,
child: Image.asset('image/person.png',),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child:_myImg()
),
);
}
2 Display in a fixed size area
Put the Image in a FittedBox and set the fit property to BoxFit.contain.
Widget _myImg(){
return FittedBox(
fit: BoxFit.contain,
child:
Image.asset('image/person.png',),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(32),
child: _myImg(),
width: 200,
height: 200,
)
),
);
}
Recent Posts
See AllWhat want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to...
What want to do There is a variable that remain unchanged once the initial value is determined. However, it cannot be determined yet when...
What want to do As the title suggests. Place two widgets in one line on the screen One in the center of the screen and the other on the...
Comments