**change the appbar title dynamically**
从数据库获取appbar标题,然后设置到appbar中。我是Flutter的新手,尝试了setState但仍然不起作用。如何根据服务器响应以及数据库值更改appbar文本?
使用Async/await和FutureBuilder从数据库中获取数据并在AppBar中使用setState()更新标题。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:parking_app/data/database_helper.dart';
import'package:parking_app/models/user.dart';
class HomeScreen extends StatefulWidget { @override State<StatefulWidget> createState() {
return new HomeScreenState(); } }
class HomeScreenState extends State<HomeScreen> { @override Widget build(BuildContext context) {
var db = new DatabaseHelper();
Future<User> user = db.getUser();
String appBarTitle = "eClerx Parking";
var appBarTitleText = new Text(appBarTitle);
if (user != null) {
user.then((val) {
if (val == null) {
return;
}
print(TAG+ " user data : " + val.emailId);
setState(() {
build(context);
appBarTitle = val.emailId;
});
});
}
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: appBarTitleText,
actions: <Widget>[
// action button
new IconButton(
icon: new Icon(Icons.settings_power),
onPressed: () {
_logout();
},
),
// action button
],
),
body: new Padding(
padding: const EdgeInsets.all(16.0),
child: new ChoiceCard(choice: choices[0]),
),
),
); } }