FLUTTER: CHANGE IMAGE WIDGET PICTURE USING SETSTATE()

 Code used in the video:

import 'package:flutter/material.dart';
import 'package:lessons/second_page.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _currentPicture = 'assets/images/bike.jpg';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text(
            'Hello World',
            style: TextStyle(color: Colors.white),
          ),
          centerTitle: true,
          backgroundColor: Colors.blue,
          elevation: 10.0,
        ),
        body: Center(
          child: Column(
            children: [
              Image.asset(
                _currentPicture,
                width: 300,
                height: 300,
                fit: BoxFit.fill,
              ),
              const SizedBox(
                height: 10.0,
              ),
              ElevatedButton(
                onPressed: () {
                  if (_currentPicture == 'assets/images/bike.jpg') {
                    _currentPicture = 'assets/images/twitter.jpg';
                  } else {
                    _currentPicture = 'assets/images/bike.jpg';
                  }
                },
                style: ElevatedButton.styleFrom(
                  foregroundColor: Colors.white,
                  backgroundColor: Colors.blue,
                ),
                child: const Text("Change Picture"),
              ),
            ],
          ),
        ));
  }
}


No comments:

Post a Comment

UNITY: USING FIREBALL TO ELIMINATE ENEMIES

 Code user in the video: Fireball Controller using System . Collections ; using System . Collections . Generic ; using UnityEngine ; publ...