- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有表格的页面。一旦用户点击保存,它应该会显示一个 SnackBar。保存按钮在一个单独的自定义 AppBar 小部件中(在一个单独的文件中),它具有从页面发送的 2 个函数和表单。 AppBar 为可重用目的而分开。
我曾尝试使用 Builder 方法,但它不起作用。然后我使用了 Global Key 方法,它不会给我错误,但仍然没有 SnackBar。
import 'package:flutter/material.dart';
import '../models/author.dart';
import '../widgets/edit_app_bar.dart';
class AuthorEditPage extends StatefulWidget {
static const PAGE_TITLE = 'Edit Author';
static const ROUTE_NAME = '/author-edit';
@override
_AuthorEditPageState createState() => _AuthorEditPageState();
}
class _AuthorEditPageState extends State<AuthorEditPage> {
@override
Widget build(BuildContext context) {
final _operation = ModalRoute.of(context).settings.arguments as String;
final GlobalKey<ScaffoldState> _scaffoldKey =
new GlobalKey<ScaffoldState>();
Author _initialize() {
return Author(
id: null,
name: 'Test',
nameOther: null,
facebook: null,
website: null,
lastUpdated: DateTime.now().toIso8601String(),
);
}
void _displaySnackBar(Author author) {
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text(
'Author: ${author.name} added',
),
),
);
}
return Scaffold(
key: _scaffoldKey,
appBar: EditAppBar(
title: AuthorEditPage.PAGE_TITLE,
saveAndAddNew: () async {
Author author = _initialize();
bool result = await author.createUpdateDelete(_operation);
if (result) {
_displaySnackBar(author);
setState(() {});
}
},
save: () async {
Author author = _initialize();
bool result = await author.createUpdateDelete(_operation);
if (result) {
_displaySnackBar(author);
Navigator.of(context).pop();
}
},
),
body: null,
);
}
}
import 'package:flutter/material.dart';
class EditAppBar extends StatelessWidget with PreferredSizeWidget {
EditAppBar({
Key key,
@required this.title,
@required this.saveAndAddNew,
@required this.save,
}) : super(key: key);
final String title;
final Function saveAndAddNew;
final Function save;
@override
Widget build(BuildContext context) {
return AppBar(
title: Text(
title,
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.add,
),
onPressed: saveAndAddNew,
),
IconButton(
icon: Icon(
Icons.save,
),
onPressed: save,
),
],
);
}
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
最佳答案
Once the user clicks on Save, it should display a SnackBar
save: () async {
Author author = _initialize();
bool result = await author.createUpdateDelete(_operation);
if (result) {
_displaySnackBar(author);
Navigator.of(context).pop(); //disposing the Scaffold widget
}
},
如果你真的想显示一个 Scaffold,你应该使用你希望它显示的 ScaffoldWidget 的 GlobalKey(在上一页的这种情况下)。还要避免在 build 方法中创建 GlobalKey,每次调用 setState 时都会创建一个新的。这是一个有 2 个页面和 2 个 GloabalKeys 的示例,第一页为第二个页面提供了 globalKey,因此它可以在需要时使用它。
class Page1 extends StatelessWidget{
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: Center(
child: FlatButton(
child: Text('SecondPage'),
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) => AuthorEditPage(_scaffoldKey)))
)
)
);
}
}
class AuthorEditPage extends StatefulWidget {
static const PAGE_TITLE = 'Edit Author';
static const ROUTE_NAME = '/author-edit';
final GlobalKey<ScaffoldState> previousScaffold;
AuthorEditPage(this.previousScaffold);
@override
_AuthorEditPageState createState() => _AuthorEditPageState();
}
class _AuthorEditPageState extends State<AuthorEditPage> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>(); //initialize variables here
Author _initialize() {
return Author(
id: null,
name: 'Test',
nameOther: null,
facebook: null,
website: null,
lastUpdated: DateTime.now().toIso8601String(),
);
}
//give the GlobalKey of the scaffold you want to display the snackbar
void _displaySnackBar(Author author, GlobalKey<ScaffoldState> scaffold) {
scaffold?.currentState?.showSnackBar(
SnackBar(
content: Text(
'Author: ${author.name} added',
),
),
);
}
@override
Widget build(BuildContext context) {
final _operation = ModalRoute.of(context).settings.arguments as String; //This one requires the context so it's fine to be here
//Avoid creating objects or methods not related to the build method here, you can make them in the class
return Scaffold(
key: _scaffoldKey,
appBar: EditAppBar(
title: AuthorEditPage.PAGE_TITLE,
saveAndAddNew: () async {
Author author = _initialize();
bool result = await author.createUpdateDelete(_operation);
print(result);
if (result) {
_displaySnackBar(author, _scaffoldKey);
setState(() {});
}
},
save: () async {
Author author = _initialize();
bool result = await author.createUpdateDelete(_operation);
print(result);
if (result) {
_displaySnackBar(author, widget.previousScaffold); //I sue the globalKey of the scaffold of the first page
Navigator.of(context).pop();
}
},
),
body: null,
);
}
}
Flutter 2.0 稳定版
ScaffoldMessenger.of(context)
发布后不再需要之前的逻辑在稳定版 2.0
class AuthorEditPage extends StatefulWidget {
static const PAGE_TITLE = 'Edit Author';
static const ROUTE_NAME = '/author-edit';
//final GlobalKey<ScaffoldState> previousScaffold; not needed anymore
AuthorEditPage();
@override
_AuthorEditPageState createState() => _AuthorEditPageState();
}
class _AuthorEditPageState extends State<AuthorEditPage> {
//final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>(); //initialize variables here //not needed anymore
Author _initialize() {
return Author(
id: null,
name: 'Test',
nameOther: null,
facebook: null,
website: null,
lastUpdated: DateTime.now().toIso8601String(),
);
}
@override
Widget build(BuildContext context) {
final _operation = ModalRoute.of(context).settings.arguments as String; //This one requires the context so it's fine to be here
//Avoid creating objects or methods not related to the build method here, you can make them in the class
return Scaffold(
appBar: Builder( //Wrap it in a builder to get the context of the scaffold you're currently in
builder (context) {
return EditAppBar(
title: AuthorEditPage.PAGE_TITLE,
saveAndAddNew: () async {
Author author = _initialize();
bool result = await author.createUpdateDelete(_operation);
print(result);
if (result) {
ScaffoldMessenger.maybeOf(context)?.showSnackBar(
SnackBar(
content: Text(
'Author: ${author.name} added',
),
),
);
setState(() {});
}
},
save: () async {
Author author = _initialize();
bool result = await author.createUpdateDelete(_operation);
print(result);
if (result) {
// It should keep the snackbar across pages
ScaffoldMessenger.maybeOf(context)?.showSnackBar(
SnackBar(
content: Text(
'Author: ${author.name} added',
),
),
);
Navigator.of(context).pop();
}
},
);
}
),
body: null,
);
}
}
关于flutter - 打开 SnackBar 内部函数发送到 AppBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62308780/
在 app_bar.dart 文件中提到,海拔控制应用栏下方阴影的大小,并且默认情况下,如果没有内容在 AppBar 下滚动,则不会绘制阴影。 这是我的代码: Scaffold( appBar:
我正在创建一个 GlobalPage/FrameHoster,其中将包含我的 AppBar,然后创建一个将托管我的所有应用程序页面的 Frame 控件。除了一些页面导航控件被应用栏隐藏之外,这一切都很
我正在尝试实现 flutter_tagging里面AppBar .我设法添加了标签 TextField里面AppBar并调整 AppBar使用 PreferredSize使用以下代码的小部件: ret
我的 AppBar 在屏幕上的图像后面或者我看不到它,有什么建议可以在顶部显示我的 AppBar 和 buttomNavigator 并且是半透明的吗? Widget build(BuildConte
我的应用栏上有两个图标,点击保存保存图标消失,批准图标会弹出它工作正常,但根据下图在大屏幕上通过应用栏图标剪切。这是我的代码 appBar: PreferredSize( preferre
我想在容器上的 DoubleTap 上动态隐藏和显示 App Bar,并在隐藏时使用一些动画,但此链接的解决方案不适用于我的项目:Flutter - How can I dynamically sho
自定义应用栏的屏幕截图: 我想制作一个像照片一样的自定义应用栏任何人都可以帮助我创建这个我已经搜索了很多但没有找到任何地方 最佳答案 我不认为它是一个圆形的appBar,而是它下面的一个圆形的容器。
嘿,我正在创建底部应用栏并向其添加 3,应用栏按钮,如下所示。一个使用内置图标,另外两个使用位图图标 带有位图图标的 AppBarButton 在设计 View 中正确显示,即包含在
自定义应用栏的屏幕截图: 我想制作一个像照片一样的自定义应用栏任何人都可以帮助我创建这个我已经搜索了很多但没有找到任何地方 最佳答案 我不认为它是一个圆形的appBar,而是它下面的一个圆形的容器。
嘿,我正在创建底部应用栏并向其添加 3,应用栏按钮,如下所示。一个使用内置图标,另外两个使用位图图标 带有位图图标的 AppBarButton 在设计 View 中正确显示,即包含在
代码:
我创建了一个 AppBar。 AppBar 始终保持在顶部,当它失去焦点时,返回到 AppBar 需要点击两次而不是一次。我相信第一次点击会激活表单,然后第二次点击我会收到鼠标点击事件(如预期的那样)
我制作了一个简单的 appBar,在屏幕顶部只有一个标签可以缩小桌面,但我无法让它显示为我的第二台显示器。我一直在四处寻找,但我发现的一切都是针对 WPF 的。这些很可能是我犯错的地方,但如果您需要查
当你想让你的应用扩展到全屏时(包括状态栏和应用栏),你必须这样做: var applicationView = Windows.UI.ViewManagement.ApplicationView.Ge
我写了一个应用程序桌面工具栏(又名 AppBar),它工作得很好,除了如果我终止进程,AppBar 代码永远没有机会通过发送 ABM_REMOVE 来清理。问题是这基本上搞砸了用户的桌面。 AppBa
我开始使用 React/Material-UI,也是 CSS 等的新手...我有一个带有 APPBar 的简单页面布局。不幸的是,此 AppBar 与本应位于其下方的元素重叠。 我找到了这个答案: A
我正在开发的 Windows 应用商店应用的主页上有两个 AppBar。一个在顶部用于导航,另一个在下方用于命令。 显然,我希望 AppBars 在单击新命令按钮后关闭。下面的代码可以工作,但看起来很
我的布局中有一个 AppBar 和一个水平线性布局(其中包括一个编辑文本和两个图像按钮)和其他内容。当用户向下滚动时,我想要 AppBar(实际上,Toolbar 隐藏。这是我尝试过的,appbar
我有一个应用栏,我想在其中显示标题,然后在它旁边有一个凸起的按钮,该按钮会展开以填充剩余的水平空间。我试过这个的变体: AppBar( title: Row( children: [
我正在尝试为我的所有屏幕使用相同的 Appbar,但我不想多次添加相同的代码,因此我考虑创建具有默认 Appbar 的类 MyAppbar,以便我可以使用它在我的屏幕上没有重复相同的代码,但我不知道我
我是一名优秀的程序员,十分优秀!