Flutter 无状态widget到状态widget
我正在修改WikipediaExplorer(开源)中的一个类来浏览预先选择的页面。我试图添加一个页面计数器,它不会更新,因为它是一个无状态小部件。有人能帮我把它变成有状态的小部件吗Flutter 无状态widget到状态widget,flutter,statefulwidget,statelesswidget,Flutter,Statefulwidget,Statelesswidget,我正在修改WikipediaExplorer(开源)中的一个类来浏览预先选择的页面。我试图添加一个页面计数器,它不会更新,因为它是一个无状态小部件。有人能帮我把它变成有状态的小部件吗 class NavigationControls extends StatelessWidget { const NavigationControls(this._webViewControllerFuture) : assert(_webViewControllerFuture != null);
class NavigationControls extends StatelessWidget {
const NavigationControls(this._webViewControllerFuture)
: assert(_webViewControllerFuture != null);
final Future<WebViewController> _webViewControllerFuture;
@override
Widget build(BuildContext context) {
return FutureBuilder<WebViewController>(
future: _webViewControllerFuture,
builder:
(BuildContext context, AsyncSnapshot<WebViewController> snapshot) {
final bool webViewReady =
snapshot.connectionState == ConnectionState.done;
final WebViewController controller = snapshot.data;
return _buttonsPagination(webViewReady, controller, context);
},
);
}
类导航控件扩展了无状态小部件{
const NavigationControls(this.\u webViewControllerFuture)
:assert(_webViewControllerFuture!=null);
最终的未来webViewControllerFuture;
@凌驾
小部件构建(构建上下文){
回归未来建设者(
未来:_webViewControllerFuture,
建设者:
(BuildContext上下文,异步快照){
最终boolwebviewready=
snapshot.connectionState==connectionState.done;
最终WebViewController=snapshot.data;
返回按钮映射(webViewReady、控制器、上下文);
},
);
}
您可以通过按键盘上的快捷键将其自动转换为无状态小部件
,它应该为您提供转换为状态小部件
的选项
在Mac上尝试:CMD+
在窗口上尝试:CTRL+
不管怎样,这是你的:
class NavigationControls extends StatefulWidget {
const NavigationControls(this._webViewControllerFuture)
: assert(_webViewControllerFuture != null);
final Future<WebViewController> _webViewControllerFuture;
@override
_NavigationControlsState createState() => _NavigationControlsState();
class _NavigationControlsState extends State<NavigationControls> {
@override
Widget build(BuildContext context) {
return FutureBuilder<WebViewController>(
future: widget._webViewControllerFuture,
builder:
(BuildContext context, AsyncSnapshot<WebViewController> snapshot) {
final bool webViewReady =
snapshot.connectionState == ConnectionState.done;
final WebViewController controller = snapshot.data;
return _buttonsPagination(webViewReady, controller, context);
},
);
}}
类导航控件扩展StatefulWidget{
const NavigationControls(this.\u webViewControllerFuture)
:assert(_webViewControllerFuture!=null);
最终的未来webViewControllerFuture;
@凌驾
_NavigationControlsState createState()=>\u NavigationControlsState();
类_NavigationControlsState扩展状态{
@凌驾
小部件构建(构建上下文){
回归未来建设者(
未来:widget.\u webViewControllerFuture,
建设者:
(BuildContext上下文,异步快照){
最终boolwebviewready=
snapshot.connectionState==connectionState.done;
最终WebViewController=snapshot.data;
返回按钮映射(webViewReady、控制器、上下文);
},
);
}}
您只需将光标放在无状态小部件上,按Alt+Enter
,然后单击Convert toStatefulWidget
。所有样板代码将自动为您创建
耶