Home > Back-end >  How to setState between different files
How to setState between different files

Time:04-16

please somebody learn me how to do this the below is very simple version of my code but i want it exactly like this we have three different files. every one them has one statefull widget like this

Home.dart

import 'package:flutter/material.dart';
import 'package:gtradar_test_1/GG/appbarz.dart';
import 'package:gtradar_test_1/GG/bodyz.dart';


class Home extends StatefulWidget {
  const Home({ Key? key }) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Bodyz(),
      appBar: PreferredSize(preferredSize: Size(20,20),child: AppBarz(),),
    );
  }
}

Bodyz.dart

import 'package:flutter/material.dart';



class Bodyz extends StatefulWidget {
  const Bodyz({ Key? key }) : super(key: key);

  @override
  State<Bodyz> createState() => _BodyzState();
}

class _BodyzState extends State<Bodyz> {
  bool xxx = false;
  void xxx_Changer(){
    setState(() {
      xxx = !xxx;
    });

  }
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: xxx? Container(color: Colors.black):SizedBox(),
      
    );
  }
}

Appbarz

import 'package:flutter/material.dart';

class AppBarz extends StatefulWidget {
  const AppBarz({ Key? key }) : super(key: key);

  @override
  State<AppBarz> createState() => _AppBarzState();
}

class _AppBarzState extends State<AppBarz> {
  @override
  Widget build(BuildContext context) {
    return AppBar(
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: ?,
        ),

    )
      
    ;
  }
}

ok,how can i call xxx_Changer() in appbar icon button on pressed or change xxx in appbar?

CodePudding user response:

final ValueChanged onValueChanged;

above Field do the trick... you have to use this and can get the value from appbar class to homeclass then use setState to update your UI

CodePudding user response:

You need to pass xxx_Changer as a function to the AppBarz and get xxx variable as a parameter in Bodyz widget. Like this;

Home.dart

import 'package:flutter/material.dart';
import 'package:gtradar_test_1/GG/appbarz.dart';
import 'package:gtradar_test_1/GG/bodyz.dart';


class Home extends StatefulWidget {
  const Home({ Key? key }) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  bool xxx = false;
  void xxx_Changer(){
    setState(() {
      xxx = !xxx;
    });

  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Bodyz(xxx: xxx),
      appBar: PreferredSize(preferredSize: Size(20,20),child: AppBarz(xxx_Changer: xxx_Changer),),
    );
  }
}

Bodyz.dart

import 'package:flutter/material.dart';



class Bodyz extends StatefulWidget {
 final String xxx;
  const Bodyz({ Key? key, this.xxx }) : super(key: key);

  @override
  State<Bodyz> createState() => _BodyzState();
}

class _BodyzState extends State<Bodyz> {
  
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: widget.xxx? Container(color: Colors.black):SizedBox(),
      
    );
  }
}

Appbarz

import 'package:flutter/material.dart';

class AppBarz extends StatefulWidget {
  final Function xxx_Changer;
  const AppBarz({ Key? key, Function xxx_Changer }) : super(key: key);

  @override
  State<AppBarz> createState() => _AppBarzState();
}

class _AppBarzState extends State<AppBarz> {
  @override
  Widget build(BuildContext context) {
    return AppBar(
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: widget.xxx_Changer(),
        ),

    )
      
    ;
  }
}
  • Related