Flutter 增加按键抖动时的文本大小
我是个新手,我正在尝试一个简单的函数。。 我有一个带有文本和凸起按钮的页面,我想在每次按下按钮时将文本大小增加1.0。。 我试过这个,但不起作用Flutter 增加按键抖动时的文本大小,flutter,textstyle,Flutter,Textstyle,我是个新手,我正在尝试一个简单的函数。。 我有一个带有文本和凸起按钮的页面,我想在每次按下按钮时将文本大小增加1.0。。 我试过这个,但不起作用 class _StoryDetailsState extends State<StoryDetails> { @override Widget build(BuildContext context) { var textSize = 10.0; return Scaffold( backgroundColor: Color(0xffE
class _StoryDetailsState extends State<StoryDetails> {
@override
Widget build(BuildContext context) {
var textSize = 10.0;
return Scaffold(
backgroundColor: Color(0xffEA3A82),
appBar: AppBar(
elevation: 0.0,
backgroundColor: Color(0xffEA3A82),
title: Text(widget.story_title),
)
,body: SingleChildScrollView(
child: Column(
children: <Widget>[
Align(
alignment: Alignment.topLeft
,child: Image.network(widget.story_detail_pic , width: double.infinity,)
),
RaisedButton(
child: Text('enlarge text'),
onPressed: (){
setState(() {
textSize = textSize + 1.0;
print(textSize);
});
},
),
Padding(
padding: const EdgeInsets.all(10.0),
child: ClipRRect(borderRadius: BorderRadius.circular(10)
,child: Container(color: Colors.white.withOpacity(0.6) ,child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(widget.story , style: TextStyle(fontSize: textSize),),
))),
)
],
),
),
);
class\u故事细节状态扩展状态{
@凌驾
小部件构建(构建上下文){
var textSize=10.0;
返回脚手架(
背景颜色:颜色(0xffEA3A82),
appBar:appBar(
标高:0.0,
背景颜色:颜色(0xffEA3A82),
标题:文本(widget.story_标题),
)
,正文:SingleChildScrollView(
子:列(
儿童:[
对齐(
对齐:alignment.topLeft
,子:Image.network(widget.story_detail_pic,宽度:double.infinity,)
),
升起的按钮(
子项:文本(“放大文本”),
已按下:(){
设置状态(){
textSize=textSize+1.0;
打印(文本大小);
});
},
),
填充物(
填充:常数边集全部(10.0),
子项:ClipRRect(borderRadius:borderRadius.circular(10)
,子:容器(颜色:Colors.white.withOpacity(0.6),子:填充(
填充:常数边集全部(8.0),
子项:文本(widget.story,style:TextStyle(fontSize:textSize),),
))),
)
],
),
),
);
}
}问题在于您在build方法内部声明了
textsize
。每次调用set state时,都会调用build方法,并且您的textsize
再次设置为10。只需移动var textSize=10.0代码>在构建方法之外,它可以正常工作
class _StoryDetailsState extends State<StoryDetails> {
var textSize = 10.0;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xffEA3A82),
appBar: AppBar(
elevation: 0.0,
backgroundColor: Color(0xffEA3A82),
title: Text("Title"),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Align(
alignment: Alignment.topLeft,
child: Image.network(
"http://via.placeholder.com/640x360",
width: double.infinity,
)),
RaisedButton(
child: Text('enlarge text'),
onPressed: () {
textSize = textSize + 1.0;
print(textSize);
setState(() {});
},
),
Padding(
padding: const EdgeInsets.all(10.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
color: Colors.white.withOpacity(0.6),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"story",
style: TextStyle(fontSize: textSize),
),
))),
)
],
),
),
);
}
}
class\u故事细节状态扩展状态{
var textSize=10.0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色(0xffEA3A82),
appBar:appBar(
标高:0.0,
背景颜色:颜色(0xffEA3A82),
标题:文本(“标题”),
),
正文:SingleChildScrollView(
子:列(
儿童:[
对齐(
对齐:alignment.topLeft,
孩子:Image.network(
"http://via.placeholder.com/640x360",
宽度:double.infinity,
)),
升起的按钮(
子项:文本(“放大文本”),
已按下:(){
textSize=textSize+1.0;
打印(文本大小);
setState((){});
},
),
填充物(
填充:常数边集全部(10.0),
孩子:ClipRRect(
边界半径:边界半径。圆形(10),
子:容器(
颜色:颜色。白色。不透明度(0.6),
孩子:填充(
填充:常数边集全部(8.0),
子:文本(
“故事”,
样式:TextStyle(fontSize:textSize),
),
))),
)
],
),
),
);
}
}