'Accessing Classes in different pages
can somebody explain me when we have one file like Home.dart i have access to extends state classes and it's static variables
Home.dart
import 'package:flutter/material.dart';
class FirstClass extends StatefulWidget {
const FirstClass({Key? key}) : super(key: key);
@override
State<FirstClass> createState() => _FirstClassState();
}
class _FirstClassState extends State<FirstClass> {
static void someFunc() {
print('nothing');
}
@override
Widget build(BuildContext context) {
return Container();
}
}
class SecondClass extends StatefulWidget {
const SecondClass({Key? key}) : super(key: key);
@override
State<SecondClass> createState() => _SecondClassState();
}
class _SecondClassState extends State<SecondClass> {
@override
Widget build(BuildContext context) {
return ElevatedButton(onPressed: _FirstClassState.someFunc, child: Text(' '));
}
}
but when we have two different file like home1.dart and home2.dart we cant access to extends state classes?
Home1.dart
import 'package:flutter/material.dart';
class FirstClass extends StatefulWidget {
const FirstClass({Key? key}) : super(key: key);
@override
State<FirstClass> createState() => _FirstClassState();
}
class _FirstClassState extends State<FirstClass> {
static void someFunc() {
print('nothing');
}
@override
Widget build(BuildContext context) {
return Container();
}
}
Home2.dart
import 'package:flutter/material.dart';
import 'Home1.dart';
class SecondClass extends StatefulWidget {
const SecondClass({Key? key}) : super(key: key);
@override
State<SecondClass> createState() => _SecondClassState();
}
class _SecondClassState extends State<SecondClass> {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: **_FirstClassState.someFunc?????**, child: Text(' '));
}
}
i know i can have access to FirstClass but why can not have access to _FirstClassState ?
Solution 1:[1]
I think it is better to separate your widget classes and logic classes that contains some function.
Since you are extending Stateless/Stateful widget, the class is becoming a widget.
Create you method or function in another class and don't extend anything if you don't have to then use static keyword in front of the functions so that you can access those functions inside another class or let's say in your stateful widget class.
However, if you need to pass values from one Stateful widget to another then add that object to your widget's constructor. Example as follows:
class FirstClass extends StatefulWidget {
String username = ""; //Creating & initializing username variable
//Adding username variable inside constructor
const FirstClass({Key? key, required this.username}) : super(key: key);
@override
State<FirstClass> createState() => _FirstClassState();
}
class _FirstClassState extends State<FirstClass> {
static void someFunc() {
print('nothing');
}
@override
Widget build(BuildContext context) {
return Text(widget.username); //Accessing username
}
}
Then, pass your value in the widget like this:
class SecondClass extends StatefulWidget {
const SecondClass({Key? key}) : super(key: key);
@override
State<SecondClass> createState() => _SecondClassState();
}
class _SecondClassState extends State<SecondClass> {
@override
Widget build(BuildContext context) {
return FirstClass(username: "Tricolore"); //Passing username value
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
