有两个有状态的小部件。 MyHomePage的状态包含计数器。 MyHomePage将内容包装在第二个有状态小部件SubPage中。 子页面具有一个子窗口小部件,其中包含来自MyHomePage的数据。
为解决此问题,计数器更改时,SubPage子级内部的第一个textwidget不会更新。
子页面外部的textwidget会按预期增加。
如果我们要更新内部有状态小部件的内容,我们该怎么办?
我们必须在那里使用一个有状态的小部件。在实际的应用程序中,此小部件具有真实的用例。
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: Column(
children: <Widget>[
SubPage(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class SubPage extends StatefulWidget {
SubPage({Key key, this.child}) : super(key: key);
final Widget child;
@override
SubPageState createState() => new SubPageState(child);
}
class SubPageState extends State<SubPage> {
final Widget child;
SubPageState(this.child);
@override
Widget build(BuildContext context) {
print("subpage build");
return this.child;
}
}
答案 0 :(得分:2)
您不必将<!doctype html>
<html>
<head>
<title> Home </title>
<link href="mainCSS.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="wrapper">
<header>
<div class="topnav" id="myTopnav">
<!-- Whole navigation menu -->
<ul>
<h1 class="first"><a href="MyWebsiteCSS.html">UrbanAero</a></h1>
<li class="room"><a href="#">Rooms</a>
<div class="subnav" id="mySubnav">
<ul class="boi">
<li><a href="#">Living room</a></li><br>
<li><a href="#">Dining room</a></li><br>
<li><a href="#">Home office and Study</a></li><br>
<li><a href="#">Bedroom</a></li>
<a href="javascript:void(0);" class="subIcon" onclick="subnavFunction();"><i class="fa fa-caret-down" aria-hidden="true"></i></a>
</ul>
</div>
</li>
<li><a href="#">lighting</a></li>
<li><a href="#">Accessories</a></li>
<li><a class="GreyText" href="ContactCSS.html">Contact</a></li>
<li><a class="GreyText" href="AboutCSS.html">About Us</a></li>
<li>
<a href="javascript:void(0);" class="icon" onclick="myFunction()"></li>
<li><i class="fa fa-bars"></i></a>
</li>
</ul>
</div>
</header>
<div class="push">
</div>
</div>
<footer class="footer">©UrbanAero</footer>
</body>
</html>
设置为状态字段。实际上,这是此错误的原因。这里的工作代码
child