[英]Access State variable from outside the widget
因此,我制作了PageViewer
,但无法在其中使用Navigator
,而我想做的是使用返回函数的StatefulWidget
在内部传递不同的小部件,我想访问所述StatefulWidget
并更改其中的变量它,找不到办法做到这一点
这是PageView
:
Expanded(
child: PageView(
children: [
SetPage(), // Look at the bottom need to be changeable
],
),
),
这是我用来在页面中传递的StatefulWidget
:
class SetPage extends StatefulWidget {
@override
_SetPageState createState() => _SetPageState();
}
class _SetPageState extends State<SetPage> {
@override
Widget build(BuildContext context) {
var _page = ChooseAccount();
return _page;
}
}
您不需要StatefulWidget
只是为了包含另一个Widget
(在本例中为ChooseAccount
)。 如果我正确理解您的问题,您想动态更改PageView
吗? 在这种情况下,您可以执行以下操作:
// List of pages in your app
List<Widget> pages = [
ChooseAccount(), // The choose account page
Container(
color: Colors.blue,
), // Some other pages
Container(
color: Colors.green,
),
];
然后,您可以简单地使用PageView
中的此页面列表与PageController
结合在页面之间导航:
// Define the controller here
PageController _controller = PageController(initialPage: 0);
// Use this method to navigate between pages (for example when changing tabs in the TabBar`
navigateToPage(int index) {
_controller.jumpToPage(index);
}
// Use the controller and the pages with the PageView
PageView(
controller: _controller,
physics: NeverScrollableScrollPhysics(),
children: pages,
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.