有什么办法可以在状态class中调用状态class吗?
Is there any way to call state class in state class?
main.dart
void main() {
runApp(MyApp());
}
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'A',
theme: ThemeData(
fontFamily: 'Bookman',
primarySwatch: Colors.indigo,
),
home: MyHomePage(title: 'A'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
//navigationBar createNavState() => navigationBar();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
SizeConfig().init(context);
return Scaffold(
key: _scaffoldKey,
endDrawer: Container(
child: Column(
children: [],
),
),
appBar: PreferredSize(
preferredSize: Size.fromHeight(SizeConfig.screenHeight / 16),
child: AppBar(
leading: Icon(Icons.menu),
title: new Text(
'A',
style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.normal),
),
actions: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Icon(Icons.search),
),
Icon(Icons.person),
],
),
),
body: Column(
children: [
Row(
children: [
Container(
width: SizeConfig.screenWidth,
height: SizeConfig.screenHeight * (1 / 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[200])),
)
],
),
Row(
children: [
Column(
children: [
Container(
width: SizeConfig.screenWidth / 7,
height: SizeConfig.screenHeight * (13 / 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[200])),
)
],
),
Column(
children: [
Row(
children: [Container(child: Text("CALL IN HERE!") )],
),
Row(children: [
Container(
width: SizeConfig.screenWidth * (6 / 7),
height: SizeConfig.screenHeight * (1 / 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[200])),
)
]),
Row(
children: [myDataTable()],
)
],
)
],
)
],
));
}
}
navigationBar.dart
var _logo = new Image.asset('image/**.ico', width: 40, height: 50);
Future dwln() async {
const url = "https://**/download.php";
if (await canLaunch(url))
await launch(url);
else
throw "Could not launch $url";
}
//class navigationBar extends StatefulWidget {}
class navigationBarState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Container(
width: SizeConfig.screenWidth * (6 / 7),
height: SizeConfig.screenHeight * (1 / 16),
decoration: BoxDecoration(border: Border.all(color: Colors.grey[200])),
child: Center(
child: Column(
children: [
Row(
children: [
Text(
"New ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.add),
color: Colors.pinkAccent,
onPressed: () {
which = "new";
endDrawer();
},
iconSize: 15,
),
Text(
"Exit grid view ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.edit),
color: Colors.pinkAccent,
onPressed: () {
setState(() {
selectedUsers.length = 0;
if (getir == true) {
getir = false;
} else {
getir = true;
}
});
},
iconSize: 15,
),
Text(
"Export to EXCEL ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.save),
color: Colors.pinkAccent,
onPressed: () {
dwln();
},
iconSize: 15,
),
Text(
"Import EXCEL file ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.import_contacts),
color: Colors.pinkAccent,
onPressed: () {},
iconSize: 15,
),
if (editlabel == true)
Row(
children: [
Text(
"Edit ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.edit),
color: Colors.pinkAccent,
onPressed: () {
setState(() {
which = "update";
});
endDrawer();
},
iconSize: 15,
),
],
)
],
),
],
),
),
);
}
}
大家好,
我想在 main.dart 文件中调用 navigationBarState class。但是由于“状态”,它不允许这样做。它说“参数类型 'navigationBarState' 无法分配给参数类型 'Widget'。”。但是我需要 State extend 才能使用 setState。有解决办法吗?
看来你只有状态。您还需要 StatefulWidget
。我不确定你为什么把它注释掉,但你需要把它放回去。
至少你需要:
class navigationBar extends StatefulWidget {
navigationBar ({Key? key}) : super(key: key);
@override
navigationBarState createState() => navigationBarState();
}
然后您可以在小部件树中放置一个 navigationBar
,而不是状态本身。
状态 class 本身必须使用小部件,因为它是通用参数:
class navigationBarState extends State<navigationBar>
main.dart
void main() {
runApp(MyApp());
}
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'A',
theme: ThemeData(
fontFamily: 'Bookman',
primarySwatch: Colors.indigo,
),
home: MyHomePage(title: 'A'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
//navigationBar createNavState() => navigationBar();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
SizeConfig().init(context);
return Scaffold(
key: _scaffoldKey,
endDrawer: Container(
child: Column(
children: [],
),
),
appBar: PreferredSize(
preferredSize: Size.fromHeight(SizeConfig.screenHeight / 16),
child: AppBar(
leading: Icon(Icons.menu),
title: new Text(
'A',
style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.normal),
),
actions: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Icon(Icons.search),
),
Icon(Icons.person),
],
),
),
body: Column(
children: [
Row(
children: [
Container(
width: SizeConfig.screenWidth,
height: SizeConfig.screenHeight * (1 / 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[200])),
)
],
),
Row(
children: [
Column(
children: [
Container(
width: SizeConfig.screenWidth / 7,
height: SizeConfig.screenHeight * (13 / 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[200])),
)
],
),
Column(
children: [
Row(
children: [Container(child: Text("CALL IN HERE!") )],
),
Row(children: [
Container(
width: SizeConfig.screenWidth * (6 / 7),
height: SizeConfig.screenHeight * (1 / 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[200])),
)
]),
Row(
children: [myDataTable()],
)
],
)
],
)
],
));
}
}
navigationBar.dart
var _logo = new Image.asset('image/**.ico', width: 40, height: 50);
Future dwln() async {
const url = "https://**/download.php";
if (await canLaunch(url))
await launch(url);
else
throw "Could not launch $url";
}
//class navigationBar extends StatefulWidget {}
class navigationBarState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Container(
width: SizeConfig.screenWidth * (6 / 7),
height: SizeConfig.screenHeight * (1 / 16),
decoration: BoxDecoration(border: Border.all(color: Colors.grey[200])),
child: Center(
child: Column(
children: [
Row(
children: [
Text(
"New ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.add),
color: Colors.pinkAccent,
onPressed: () {
which = "new";
endDrawer();
},
iconSize: 15,
),
Text(
"Exit grid view ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.edit),
color: Colors.pinkAccent,
onPressed: () {
setState(() {
selectedUsers.length = 0;
if (getir == true) {
getir = false;
} else {
getir = true;
}
});
},
iconSize: 15,
),
Text(
"Export to EXCEL ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.save),
color: Colors.pinkAccent,
onPressed: () {
dwln();
},
iconSize: 15,
),
Text(
"Import EXCEL file ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.import_contacts),
color: Colors.pinkAccent,
onPressed: () {},
iconSize: 15,
),
if (editlabel == true)
Row(
children: [
Text(
"Edit ",
style: TextStyle(fontSize: 13),
),
IconButton(
icon: Icon(Icons.edit),
color: Colors.pinkAccent,
onPressed: () {
setState(() {
which = "update";
});
endDrawer();
},
iconSize: 15,
),
],
)
],
),
],
),
),
);
}
}
大家好, 我想在 main.dart 文件中调用 navigationBarState class。但是由于“状态”,它不允许这样做。它说“参数类型 'navigationBarState' 无法分配给参数类型 'Widget'。”。但是我需要 State extend 才能使用 setState。有解决办法吗?
看来你只有状态。您还需要 StatefulWidget
。我不确定你为什么把它注释掉,但你需要把它放回去。
至少你需要:
class navigationBar extends StatefulWidget {
navigationBar ({Key? key}) : super(key: key);
@override
navigationBarState createState() => navigationBarState();
}
然后您可以在小部件树中放置一个 navigationBar
,而不是状态本身。
状态 class 本身必须使用小部件,因为它是通用参数:
class navigationBarState extends State<navigationBar>