1

I have a main dart file and a settings dart file. The settings dart file is responsible for the appearance of the main dart file. Settings dart has a AppTheme class. Upon users typing on this settings page I want the main page to update.

My attempt at this was calling the class and redefining the variables based on user input. Doesnt work whether or not I use setState(). I also tried jus staying on the main page and tried changing the theme onPressed for the settings button. That didnt work either. Iconbutton update doesnt update the state either.

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:resume/settings.dart';


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


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Title',
      theme: ThemeData.light(),
      home: HomePage(),
    );
  }
}


class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}


class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {

    double buttonMargin = MediaQuery.of(context).size.width / 10;
    double screenWidth = MediaQuery.of(context).size.width;
    double screenHeight = MediaQuery.of(context).size.height;

    return MaterialApp(
      home: Scaffold(
        backgroundColor: AppTheme().backgroundMain,
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
            child: Column(
              children: <Widget>[
                Container(
                  height: (MediaQuery.of(context).size.height / 2.25),
                  child: Column(children: <Widget>[
                    Container(
                      margin: EdgeInsets.all(10),
                      child: Text(AppTheme().name,
                          style: TextStyle(
                              fontFamily: 'Pacifico',
                              fontSize: screenWidth / 8.57142857143,
                              color: AppTheme().nameTextColor,
                              fontWeight: FontWeight.bold)),
                    ),
                  ]),
                ),
                Container(
                  height: screenHeight / 2.3,
                  alignment: Alignment.bottomCenter,
                  child: ListView(
                    scrollDirection: Axis.horizontal,
                    children: <Widget>[
                      Container(
                          margin: EdgeInsets.fromLTRB(
                              buttonMargin * 1.5,
                              buttonMargin * 3,
                              buttonMargin * 1.5,
                              buttonMargin * 3),
                          height: MediaQuery.of(context).size.height / 2,
                          width: MediaQuery.of(context).size.width / 2,
                          child: RaisedButton(
                            color: AppTheme().backgroundSecondary,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Icon(
                                  Icons.settings,
                                  color: Colors.white,
                                ),
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Text(
                                    'Settings',
                                    style: TextStyle(color: Colors.white),
                                  ),
                                )
                              ],
                            ),
                            onPressed: () {
                              setState(() {
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(builder: (context) {
                                    return MySettings();
                                  }),
                                );
                              },
                              );
                              },
                          ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}




settings.dart

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void settings() {
  runApp(MySettings());
}

class AppTheme {
  var backgroundMain = Colors.red;
  var backgroundSecondary = Colors.teal;
  var backgroundAvatar = Colors.white;
  var nameTextColor = Colors.white;
  var professionTextColor = Colors.red[100];
  var contactTextColor = Colors.teal;
  var testPrint = print("hi");

  String name = 'John Doe';

  String nameFont = 'Pacifico';
  String professionFont = 'roboto';
}

class MySettings extends StatefulWidget {
  @override
  MySettingsState createState() => MySettingsState();
}

class MySettingsState extends State<MySettings> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: AppTheme().backgroundMain,
          title: Text('Settings'),
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              setState(() {
                AppTheme().backgroundMain = Colors.yellow;
                print(AppTheme().name);
                Navigator.pop(context);
              });
            },
          ),
        ),
        backgroundColor: Colors.teal,
        body: SafeArea(
            child: ListView(
          children: <Widget>[
            Container(
              child: TextField(
                onChanged: (text) {
                  setState(() {
                    AppTheme().name = text;
                  });
                },
              ),
            ),
            Container(
              child: TextField(),
            ),
          ],
        )),
      ),
    );
  }
}

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
idroid8
  • 177
  • 1
  • 4
  • 14

1 Answers1

0

1 Make sure to properly import

Flutter/Dart Static variables lost / keep getting reinitialized

2 You cannot alter the variables in the class. You must create an object and alter the object.

i.e I was doing

Class MyClass {
var myVariable = someValue
}

MyClass.myVariale = aDifferentValue

This did not update MyClass What I needed to do worked once I created an object

var myClassObject = new Myclass();
myClassObject.myVariable = aDifferentValue

Now I just alter and call on myClassObject.

Community
  • 1
  • 1
idroid8
  • 177
  • 1
  • 4
  • 14
  • Having a similar issue on my question if you don't mind taking a look at it https://stackoverflow.com/questions/71605034/update-variables-data-using-set-state – LearnFlutter Mar 24 '22 at 18:50