firebase - 如何使用出现在另一个小部件中?
问题描述
我试图在另一个小部件中使用 appbar 小部件,以便我的代码更清晰。但问题是我该怎么做?
这是我的应用栏小部件:
class thisappbar extends StatelessWidget {
thisappbar(this.isMe, this.username, {this.key});
final Key key;
final bool isMe;
String username;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
automaticallyImplyLeading: false,
backgroundColor: Colors.white,
flexibleSpace: SafeArea(
child: Container(
padding: EdgeInsets.only(right: 16),
child: Row(
children: <Widget>[
IconButton(
onPressed: (){
Navigator.pop(context);
},
----------
这是我想使用它的方式:
class ChatScreen extends StatelessWidget {
static const route = '/messages';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(thisappbar(),
),
body: Container(
child: Column(
children: <Widget>[
Expanded(child: Messages()),
NewMessage(),
],
)),
);
}
}
希望有人能帮忙谢谢!
这是我的方法在编辑用户 @Steve Nosse 的一些更改后首先显示在应用栏小部件上的方式:
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
CustomAppBar({this.key, this.isMe, this.username}) : super(key: key);
final Key key;
final bool isMe;
String username;
@override
Widget build(BuildContext context) {
/// Build you AppBar widget here
return Scaffold(
appBar: AppBar(
elevation: 0,
automaticallyImplyLeading: false,
backgroundColor: Colors.white,
flexibleSpace: SafeArea(
child: Container(
padding: EdgeInsets.only(right: 16),
child: Row(
children: <Widget>[
IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(Icons.arrow_back, color: Colors.black,),
),
SizedBox(width: 2,),
CircleAvatar(
backgroundImage: NetworkImage(
'https://randomuser.me/api/portraits/men/5.jpg'),
maxRadius: 20,
),
SizedBox(width: 12,),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
username, style: TextStyle(
fontSize: 16, fontWeight: FontWeight.w600),),
SizedBox(height: 6,),
Text("Online", style: TextStyle(
color: Colors.grey.shade600, fontSize: 13),),
],
),
),
Icon(Icons.settings, color: Colors.black54,),
],
),
),
),
),
);
}
@override
Size get preferredSize => Size.fromHeight(56);
}
这里是我使用 appbar 小部件的地方:
class ChatScreen extends StatelessWidget {
static const route = '/messages';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(),
body: Container(
child: Column(
children: <Widget>[
Expanded(child: Messages()),
NewMessage(),
],
)),
);
}
}
这是错误:
======== Exception caught by widgets library =======================================================
The following assertion was thrown building CustomAppBar(dirty):
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 378 pos 10: 'data != null'
The relevant error-causing widget was:
CustomAppBar file:///Users/name/StudioProjects/project/lib/seitenleiste/nachrichten.dart:390:15
When the exception was thrown, this was the stack:
#2 new Text (package:flutter/src/widgets/text.dart:378:10)
#3 CustomAppBar.build (package:project/seitenleiste/nachrichten.dart:357:23)
#4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4646:28)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4572:15)
#6 Element.rebuild (package:flutter/src/widgets/framework.dart:4265:5)
...
解决方案
你可以试试这个:
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
CustomAppBar({this.key, this.isMe, this.username}) : super(key: key);
final Key key;
final bool isMe;
String username;
@override
Widget build(BuildContext context) {
/// Build you AppBar widget here
return Container();
}
@override
Size get preferredSize => Size.fromHeight(56);
}
与任何其他类一样,您的 CustomAppBar 可以设计为从外部获取参数来构建自己。
它实现的事实PreferredSizeWidget
使我们可以轻松地将其用作 Scaffold 小部件的 appBar 属性的参数。
推荐阅读
- sirishortcuts - “发生未知错误”尝试启动我的 Siri 快捷方式时
- square-connect - 平方舍入误差(银行家舍入)
- php - PHP Date 将日期转换为日期
- api - Twitter Premium Archive 上的“请求”如何计算?
- geometry - 将子节点放置在圆圈中而不重叠
- r - 如何下载 R Shiny 中的动态图形?
- apache-kafka - Flink 不使用来自 Kafka 发布者的数据
- python - 为什么我不断得到
作为输出 - c# - 占据列表的前 26 位
- reactjs - TypeError: Object(...) is not a function when using react-toastify