1

I'm struggling to find way to access variable from different class. I want to change background image of decorationImage on button click. But because I do not want to rebuild entire state/page just the background I put the code to different class, but I'm not sure how to access _imagepath1 or _imagepath2 from other class.

class CustomMainPageState extends State<SecondRoute> {
  Color myColor = Color(0xff5D6592);

 
  String _imagepath1 = "images/image3.jpg";

  String _imagepath2 = "images/image4.jpg";
  

  Widget build(BuildContext context) => Container(
        //color: Colors.transparent,
        decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage(_imagepath1), fit: BoxFit.cover)),
        child: Scaffold(
    class background extends StatefulWidget {
      backgroundG createState() => backgroundG();
    }
    
    class backgroundG extends State<background> {
      @override
      Widget buildbottom() {
        return Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              CircleAvatar(
                radius: 25,
                backgroundColor: Colors.white,
                child: IconButton(
                  icon: Image.asset('images/icon2.png'),
                  onPressed: () => setState(() {
                    //_imagefile1 = _imagefile2;
                  }),
                ),
              ),
            ]);
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Row(
            children: [
              SizedBox(height: 100),
              buildbottom(),
              //SizedBox(height: 10),
              //SizedBox(height: 10),//puts empty box to space things out
            ],
          ),
        );
      }
    }

Thank you for helping!

Bn Minki
  • 155
  • 7
  • this is duplicate of https://stackoverflow.com/q/66831198/13984728 – Mithson Oct 05 '21 at 18:56
  • I think using a state management solution will help, for example, have the bgPath in a provider so you can change it independent of the current state. – Hooshyar Oct 05 '21 at 19:12

1 Answers1

1

For passing one image to different class you can use flutter hero widget,

below is sample code of working with Hero widget check documentation also for more info about hero widget

import 'package:flutter/material.dart';

void main() => runApp(HeroApp());

class HeroApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Transition Demo',
      home: MainScreen(),
    );
  }
}

class MainScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Main Screen'),
      ),
      body: GestureDetector(
        child: Hero(
          tag: 'imageHero',
          child: Image.network(
            'https://picsum.photos/250?image=9',
          ),
        ),
        onTap: () {
          Navigator.push(context, MaterialPageRoute(builder: (_) {
            return DetailScreen();
          }));
        },
      ),
    );
  }
}

class DetailScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        child: Center(
          child: Hero(
            tag: 'imageHero',
            child: Image.network(
              'https://picsum.photos/250?image=9',
            ),
          ),
        ),
        onTap: () {
          Navigator.pop(context);
        },
      ),
    );
  }
} 

for passing data between widgets or class I would suggest you to use InheritedWidget class or any state management like provider, bloc, getx to know more about this check this video and documentation and if you just want to pass single image then refer this blog

Mithson
  • 1,554
  • 13
  • 33
  • Hello, thank you for replying. Im struggling to apply this code to my one, where do i put the `CustomMainPageState instance = new CustomMainPageState();` Would that be in the my backgroundg class? – Bn Minki Oct 05 '21 at 20:27
  • I have added some reference to last of my answer check them out and If my answer was useful, click the upvote button (▲) to the left of it. If it answered your question, click the checkmark (✓) to accept it. That way others know that you've been (sufficiently) helped. Also see https://stackoverflow.com/help/someone-answers – Mithson Oct 05 '21 at 21:03