我有一个StatefulWidget类'FirstClass',它扩展了一个状态'_FirstClassState'。从一个单独的State类,称为“ _SecondClassState”,我正在尝试访问变量的值(在“ _FirstClassState”中称为“计数器”)。
我已经找到了解决方法,但是我不确定这是否是解决问题的最佳方法。我尝试过看看其他类似的问题:
second_class.dart:
import 'package:brew_crew/question/first_class.dart';
import 'package:flutter/material.dart';
class SecondClass extends StatefulWidget {
@override
_SecondClassState createState() => _SecondClassState();
}
class _SecondClassState extends State<SecondClass> {
final FirstClass firstClass = FirstClass();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("MyApp"),
centerTitle: true,
),
body: Column(
children: <Widget>[
firstClass, //Displays the button to increase 'counter'
FlatButton(
child: Text("Submit"),
onPressed: () {
print(firstClass
.getCounter()); //I need to get the value of 'counter' here to use elsewhere
}),
],
),
);
}
}
first_class.dart
import 'package:flutter/material.dart';
class FirstClass extends StatefulWidget {
final _FirstClassState firstClassState = _FirstClassState();
@override
_FirstClassState createState() => firstClassState;
int getCounter() {
return firstClassState.counter;
}
}
class _FirstClassState extends State<FirstClass> {
int counter = 0;
//Increases counter by 1
void _increaseCounter() {
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
//A button which increases counter when clicked
return FlatButton.icon(
icon: Icon(Icons.add),
label: Text("Counter: $counter"),
onPressed: () {
_increaseCounter();
},
);
}
}
如您所见,在“ FirstClass”中,我初始化了“ _FirstClassState”的新实例。在“ getCounter()”函数中使用它,我可以获得“计数器”值。
是否有比此方法更好的方法(例如,最佳实践,更少的代码行,更易于理解的方法等)来实现我想做的事情?
答案 0 :(得分:0)
如果绝对必须从外部访问小部件的状态,则绝对推荐使用GlobalKey
。但是,在这种情况下,您不应使用任何一种方法。
_SecondClassState
应该包含counter
,并且您应该将其与increaseCounter
函数一起作为参数传递给FirstClass
。如果要增加数量,只需调用该函数即可。
遵循以下原则:
class SecondClass extends StatefulWidget {
@override
_SecondClassState createState() => _SecondClassState();
}
class _SecondClassState extends State<SecondClass> {
int counter = 0;
//Increases counter by 1
void _increaseCounter() {
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("MyApp"),
centerTitle: true,
),
body: Column(
children: <Widget>[
FirstClass(counter: counter, increaseCounter: _increaseCounter), //Displays the button to increase 'counter'
FlatButton(
child: Text("Submit"),
onPressed: () {
print(counter.toString()); //I need to get the value of 'counter' here to use elsewhere
}),
],
),
);
}
}
class FirstClass extends StatefulWidget {
final int counter;
final Function increaseCounter;
FirstClass({this.counter, this.increaseCounter});
final _FirstClassState firstClassState = _FirstClassState();
@override
_FirstClassState createState() => firstClassState;
}
class _FirstClassState extends State<FirstClass> {
@override
Widget build(BuildContext context) {
//A button which increases counter when clicked
return FlatButton.icon(
icon: Icon(Icons.add),
label: Text("Counter: ${widget.counter}"),
onPressed: () {
widget.increaseCounter();
},
);
}
}