我不明白如何使用LayoutBuilder来获取Widget的高度.
我需要显示小部件列表并获得它们的高度,以便我可以计算一些特殊的滚动效果.我正在开发一个包,其他开发人员提供小部件(我不控制它们).我读到LayoutBuilder可以用来获得高度.
在非常简单的情况下,我试图在LayoutBuilder.builder中包装Widget并将其放入Stack中,但我总是得到minHeight 0.0,并且maxHeight INFINITY.我是否滥用LayoutBuilder?
编辑:看起来LayoutBuilder似乎不行.我发现CustomSingleChildLayout几乎是一个解决方案.
我扩展了该委托,我能够在getPositionForChild(Size size, Size childSize)方法中获得小部件的高度.但是,第一个调用的方法是Size getSize(BoxConstraints constraints)作为约束,我得到0到INFINITY,因为我在ListView中放置这些CustomSingleChildLayouts.
我的问题是SingleChildLayoutDelegate getSize像需要返回视图的高度一样运行.那时我不知道孩子的身高.我只能返回constraints.smallest(为0,高度为0)或constraints.biggest,这是无穷大并崩溃应用程序.
在文档中它甚至说:
...但是父母的大小不能取决于孩子的大小.
这是一个奇怪的限制.
有没有一种方法可以在Flutter中的TextField Widget中添加一个清除按钮?
我发现在InputDecoration的suffixIcon中设置一个清晰的IconButton.这是正确的方法吗?
我的屏幕上有一个listView.我已经附加了一个控制器.我可以调用我的端点,接收响应,解析它并插入行.ListView应该自动滚动.确实如此,但并非完美无缺.我总是背后的一个项目.这是我的代码:
@override
Widget build(BuildContext context) {
// Scroll to the most recent item
if (equationList.length > 0) {
_toEnd();
}
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: EquList(equationList, _scrollController),
floatingActionButton: new FloatingActionButton(
onPressed: onFabClick,
tooltip: 'Fetch Post',
child: new Icon(isLoading ? Icons.pause : Icons.play_arrow),
),
);
}
void _toEnd() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 250),
curve: Curves.ease,
);
}
Run Code Online (Sandbox Code Playgroud)
问题是,我_toEnd()在最后一项插入列表之前调用函数.所以,我正在寻找一个回调(如果有的话)告诉我build()完成了.然后我打电话给我的_toEnd()功能.
在这种情况下,最佳做法是什么?
假设我有两个页面,Page1 和 Page2 包含一个英雄。在 Page2 中,我想在 Hero 动画完成后开始一个 Widget 动画。
是否可以通过回调在 Page2 中获得有关 Hero 动画状态的通知?
到目前为止,我发现的唯一解决方法是向 Widget 动画添加延迟,以避免它在 Hero 动画完成之前开始:
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) => Container(
child: Hero(
tag: "hero-tag",
child: IconButton(
icon: Icon(Icons.person),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => Page2(),
));
}),
),
);
}
class Page2 extends StatefulWidget {
@override
_Page2State createState() => _Page2State();
}
class _Page2State extends State<Page2> with TickerProviderStateMixin {
AnimationController _controller;
Animation _fabAnimation;
@override
void initState() …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序并遇到了 SingleChildScrollView 从顶部开始的问题:
我如何让它像这样从底部开始:
滚动视图是在topContent屏幕的上半部分实现的:
final topContent = Stack(
children: <Widget>[
Container(
padding: EdgeInsets.only(left: 10.0),
height: MediaQuery.of(context).size.height * 0.5,
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/images/drones1.jpg"),
fit: BoxFit.cover,
),
)),
Container(
height: MediaQuery.of(context).size.height * 0.5,
padding: EdgeInsets.all(40.0),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(color: Color.fromRGBO(58, 66, 86, .9)),
child: SingleChildScrollView( //scroll view implemented here
child: Center(
child: topContentText,
),
),
),
Positioned(
left: 8.0,
top: 60.0,
child: InkWell(
onTap: () {
Navigator.pop(context);
},
child: Icon(Icons.arrow_back, color: Colors.white),
),
) …Run Code Online (Sandbox Code Playgroud) 尝试根据我的应用程序中的用户状态(已登录或未登录)进行重定向,但它不会像我希望的那样工作,因为我不确定如何在方法中获取 BuildContext。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:t2/helpers/currentuser.dart';
import 'screens/dashboard.dart';
import 'screens/login.dart';
void main() => runApp(new MyApp());
CurrentUser user = new CurrentUser();
Future checkActiveUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
user.usr = prefs.get('usr');
user.pwd = prefs.get('pwd');
if (user.usr.length == 0 && user.pwd.length == 0) {
user.isLoggedIn = false;
Navigator.of(x).pushNamedAndRemoveUntil('/dashboard', (Route<dynamic> route) => false);
} else {
// Send to login screen
user.isLoggedIn = false;
Navigator.of(x).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
}
return user.isLoggedIn;
/*
// How to read/write …Run Code Online (Sandbox Code Playgroud) 什么是Flutter等同于Android View::onAttachedToWindow和View::onDetachedFromWindow?
基本上,在Flutter中,如何确定用户是否可以在屏幕上看到特定的小部件,或者是否覆盖了它,例如,顶部有另一个屏幕?
我想在打开应用程序之前显示一个确认警报对话框,有人可以告诉我如何在颤振中实现这一点吗?
showDialog() 方法需要一个上下文,因此我应该将它与 buildContext 放在某个地方,我假设在应用程序的构建方法中,但是如何在屏幕上构建实际布局之前触发对话框?
我尝试实现一个重定向系统。
如果我在应用程序处于后台时收到通知(来自 Firebase 消息传递),我会将发送的路线信息(例如:redirect = /medias/details/430)存储在共享首选项中。
当应用程序打开时,我检查sharedPrefs(在主要部分),如果找到重定向字符串,我将其添加到流中:
MainSingleton().notificationStream.sink.add(redirectTo);
Run Code Online (Sandbox Code Playgroud)
在我的主页中initState,我检查流中是否存在重定向,如果是这种情况,我将用户重定向到正确的页面:
void initState() {
super.initState();
MainSingleton().notificationStream.stream.listen((redirect) {
if (redirect != null) {
log("Home page found a redirect request: going to $redirect");
WidgetsBinding.instance!.addPostFrameCallback((_) {
log("Callback called");
Application.router.navigateTo(context, redirect);
});
}
});
}
Run Code Online (Sandbox Code Playgroud)
"Home page found a redirect request..."当我单击通知时,我收到了消息,但没有收到消息"Callback called",显然也没有收到重定向消息。
我需要再次将应用程序设置在后台,然后再次恢复它以获得重定向。
WidgetsBinding.instance不为空,所以我不知道为什么回调没有被调用。
我有什么地方说错了吗?
在我的 flutter 应用程序中,我正在播放动画。我想仅在页面上的所有小部件加载后才开始播放。本质上,我正在寻找window.onloadflutter 中的 JS 等效项。现在,动画会尽快开始,结果是,当应用程序午餐时,我会在应用程序加载几秒钟时看到白屏,当应用程序最终加载时,动画已经播放了一半。
flutter 中是否有信号或事件让我知道应用程序已加载?