How To Make Resize An Image Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 02-11-2020

Instructs Flutter to decode the image at the specified dimensions instead of at its native size.This allows finer control of the size of the image in ImageCache and is generally used to reduce the memory footprint of ImageCache.The decoded image may still be displayed at sizes other than the cached size provided here.

How To Make Resize An Image Using Flutter Android App

Resize An Image
Step 1: 
The first step is to create a new folder and name it "assets" at the root of the Flutter project directory as    shown in the image. Now add the image inside the newly created assets directory.

Step 2:
The image added inside the assets/images folder won't be accessible until we list it in the assets section of our pubspec.yaml file. In step 2, list the image file under the assets section of pubspec.yaml as shown below

flutter: 
  assets: 
   - assets/images/

Complete Code For Resize An Image In Flutter
main.dart
import 'package:flutter/material.dart';
void main() {
  runApp(new MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Transform',
      home: new MyHomePage(),
    );
  }
}
class MyHomePage extends StatefulWidget {
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
        vsync: this,
        duration: Duration(
            seconds:
            2));
    _animation = Tween<double>(begin: 1.0, end: 3.5).animate(
        _animationController);

    _animation.addListener(() {
      setState(() {});
    });

    _animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _animationController
            .reverse();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.red[800],
        centerTitle: true,
        title: Text('Resize Image'),
      ),
      body: Center(
        child: GestureDetector(
            onTap: () {
              _animationController
                  .forward();
            },
            child: Image.asset(
              'assets/images/img2.jpg',
              height: 250.0 * _animation.value,
              width: 150.0 * _animation.value,
            )),
      ),
    );
  }
}

Related Post