gpt4 book ai didi

dart - Flutter - 从其他小部件调用 setState()

转载 作者:IT老高 更新时间:2023-10-28 12:37:25 26 4
gpt4 key购买 nike

是否可以从其他小部件 onPressed() 方法调用特定小部件(嵌入在其他小部件中)的 setState() 方法,所以只有 那个 小部件被重绘了?

我想点击按钮并查看“MyTextWidget”的状态发生变化。其余布局相同,没有任何变化,因此不应重写。

这是我的代码:

import 'package:flutter/material.dart';
void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Timer',
theme: new ThemeData(
primaryColor: Colors.grey.shade800,
),
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatelessWidget {

int _seconds = 1;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("title"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
MyTextWidget(), //just update this widget
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
icon: Icon(Icons.add_circle),
onPressed: _addPressed,
iconSize: 150.0,
),
IconButton(
icon: Icon(Icons.remove_circle),
onPressed: ()=> print("to be implemented"),
iconSize: 150.0,
),
],
)
],
),
);
}

void _addPressed() {
//somehow call _updateSeconds()
}
}

这是我要更新的有状态的 MyTextWidget。

class MyTextWidget extends StatefulWidget{
@override
_MyTextWidgetState createState() => _MyTextWidgetState();
}

class _MyTextWidgetState extends State<MyTextWidget> {

int secondsToDisplay = 0;

void _updateSeconds(int newSeconds) {
setState(() {
secondsToDisplay = newSeconds;
});
}

@override
Widget build(BuildContext context) {
return Text(
secondsToDisplay.toString(),
textScaleFactor: 5.0,
);
}
}

我想要实现的目标似乎很简单,但我无法弄清楚。想象一下,如果“MyTextWidget”被埋在巨大的布局树中,每次我想更新它时,我都需要重新绘制整个树。

最佳答案

这是使用流的可能解决方案:

import 'dart:async';

import 'package:flutter/material.dart';
void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Timer',
theme: new ThemeData(
primaryColor: Colors.grey.shade800,
),
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatelessWidget {

StreamController<int> _controller = StreamController<int>();

int _seconds = 1;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("title"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
MyTextWidget(stream: _controller.stream), //just update this widget
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
icon: Icon(Icons.add_circle),
onPressed: _addPressed,
iconSize: 150.0,
),
IconButton(
icon: Icon(Icons.remove_circle),
onPressed: ()=> _controller.add(_seconds++),
iconSize: 150.0,
),
],
)
],
),
);
}

void _addPressed() {
//somehow call _updateSeconds()
}
}

class MyTextWidget extends StatefulWidget{

final Stream<int> stream;

MyTextWidget({this.stream});

@override
_MyTextWidgetState createState() => _MyTextWidgetState();
}

class _MyTextWidgetState extends State<MyTextWidget> {

int secondsToDisplay = 0;

void _updateSeconds(int newSeconds) {
setState(() {
secondsToDisplay = newSeconds;
});
}

@override
void initState() {
super.initState();
widget.stream.listen((seconds) {
_updateSeconds(seconds);
});
}

@override
Widget build(BuildContext context) {
return Text(
secondsToDisplay.toString(),
textScaleFactor: 5.0,
);
}
}

关于dart - Flutter - 从其他小部件调用 setState(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52761888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com