Flutter 在颤振中动态更改AppBar标题
我在不同的类中有一个可重用的AppBar小部件。必须根据API响应设置应用程序栏标题。设置appBar标题后,所有屏幕的标题都应相同 飞镖Flutter 在颤振中动态更改AppBar标题,flutter,dart,Flutter,Dart,我在不同的类中有一个可重用的AppBar小部件。必须根据API响应设置应用程序栏标题。设置appBar标题后,所有屏幕的标题都应相同 飞镖 import 'package:flutter/material.dart'; class CustomAppBar extends StatefulWidget implements PreferredSizeWidget { CustomAppBar({Key key}) : preferredSize = Size.fromHeigh
import 'package:flutter/material.dart';
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
CustomAppBar({Key key})
: preferredSize = Size.fromHeight(kToolbarHeight),
super(key: key);
@override
final Size preferredSize; // default is 56.0
@override
_CustomAppBarState createState() => _CustomAppBarState();
}
class _CustomAppBarState extends State<CustomAppBar> {
@override
Widget build(BuildContext context) {
return AppBar(
title: Text('Emotely'),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [themeColor, themeColorLight],
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
类CustomAppBar扩展StatefulWidget实现PreferredSizeWidget{
CustomAppBar({Key})
:preferredSize=大小。fromHeight(kToolbarHeight),
超级(键:键);
@凌驾
最终大小preferredSize;//默认值为56.0
@凌驾
_CustomAppBarState createState()=>\u CustomAppBarState();
}
类_CustomAppBarState扩展状态{
@凌驾
小部件构建(构建上下文){
返回AppBar(
标题:文本('emotly'),
flexibleSpace:容器(
装饰:盒子装饰(
梯度:线性梯度(
颜色:[颜色,颜色],
),
),
),
);
}
}
主屏幕.省道
//api response
upload(String base64Image) {
var body = jsonEncode({"image": base64Image});
http
.post(api, headers: {"Content-Type": "application/json"}, body: body)
.then((result) {
print(result.body);
setStatus(result.statusCode == 200 ? result.body : errMessage);
})
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(), //Need to change the AppBar Title here
body: Container(
padding: EdgeInsets.all(30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[]
//api响应
上载(字符串base64Image){
var body=jsonEncode({“image”:base64Image});
http
.post(api,标题:{“内容类型”:“应用程序/json”},正文:正文)
.然后((结果){
打印(结果.正文);
设置状态(result.statusCode==200?result.body:errMessage);
})
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:CustomAppBar(),//需要在此处更改appBar标题
主体:容器(
填充:所有边缘设置(30.0),
子:列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[]
您可以将最后一个变量添加到自定义应用程序栏中
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
CustomAppBar(String title) {
this.preferredSize = Size.fromHeight(kToolbarHeight);
this.title = title; // <-- add This
}
@override
final Size preferredSize; // default is 56.0
final String title // <-- Add this
@override
_CustomAppBarState createState() => _CustomAppBarState();
}
现在在主状态完整小部件类中声明一个字符串变量:
String myAppBarTitle = ""; // <-- Add this
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(myAppBarTitle), // <-- Add this
.....
.....
)
};
此外,我建议将自定义应用程序栏更改为一个状态Less小部件,因为据我所知,您没有更改应用程序栏的状态。这只适用于一个屏幕。我有5个屏幕。一旦我设置了应用程序栏标题的状态,它在其他屏幕中也应该保持不变。您可以用它修改答案吗?然后我可以将此标记为更正。您可以帮助我将应用程序栏标题状态设置为所有屏幕吗??
String myAppBarTitle = ""; // <-- Add this
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(myAppBarTitle), // <-- Add this
.....
.....
)
};
setState(() {
myAppBarTitle = "some new title";
});