我试图根据用户单击的屏幕来更改我的应用栏的高度。我以前使用过setState,但由于屏幕上有很多组件,所以更喜欢用streambuilder来做。 我遇到的问题是使用streambuilder时出现错误
这是我的代码
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: StreamBuilder<Object>(
stream: bloc.titleStream,
initialData: home,
builder: (context, snapshot) {
return Text(snapshot.data);
}
),
backgroundColor: colorPrimary,
elevation: StreamBuilder<double>(
stream: bloc.appBarElevetionStream,
// initialData: 0.0,
builder: (context, snapshot) {
return snapshot.data.toDouble();
}
),
actions: <Widget>[
Builder(
builder: (context) => bloc.navigationProvider.currentNavigation == home ?
homeCalendar(context):otherCalendar(context) //if it's not 0 we return an empty widget
)
],
),
drawer: DrawerNavigation(),
body: homeScreen()
);
这是集团的代码
import 'dart:async';
import 'dart:ui';
import 'package:finsec/features/common/presentation/bloc/navigation_provider.dart';
class NavigationDrawerBloc {
final navigationController = StreamController();
StreamController<String> _title = StreamController<String>();
StreamController<String> _drawerItemColor = StreamController<String>();
StreamController<double> _appBarElevetion = StreamController<double>();
NavigationProvider navigationProvider = new NavigationProvider();
Stream get getNavigation => navigationController.stream;
Stream<String> get titleStream => _title.stream;
Stream<String> get itemColorStream => _drawerItemColor.stream;
Stream<double> get appBarElevetionStream => _appBarElevetion.stream;
// Color color = Color(0xffECECEC);
void updateNavigation(String navigation, String title, double elevation) {
navigationProvider.updateNavigation(navigation, title, elevation);
navigationController.sink.add(navigationProvider.currentNavigation);
_title.sink.add(title);
_drawerItemColor.sink.add('gray');
_appBarElevetion.sink.add(elevation);
}
void dispose() {
navigationController.close();
_title.close();
}
}
final bloc = NavigationDrawerBloc();
我的提供者类
import 'package:finsec/core/res/export_resources.dart';
class NavigationProvider {
String currentNavigation = home;
String currentTitle = home;
String drawerItemColor = 'gray';
double appBarElevation = 0.0;
void updateNavigation(String navigation, String title, double elevation) {
currentNavigation = navigation;
currentTitle = title;
drawerItemColor = 'gray';
appBarElevation = elevation;
}
}
在构建功能中,appbar的海拔高度属性出现错误
elevation: StreamBuilder<double>(
stream: bloc.appBarElevetionStream,
// initialData: 0.0,
builder: (context, snapshot) {
return snapshot.data.toDouble();
}
),
错误是:无法将参数类型StreamBuilder分配给double类型的参数。 返回类型'double'并不是闭包上下文所要求的'widget'
有什么方法可以将streambuilder输出转换为double以便可以在appbar的height属性中使用它?如何在海拔属性中使用streambuilder?预先感谢
答案 0 :(得分:0)
StreamBuilder
返回一个widget
。 double
中的StreamBuilder
是stream
而非StreamBuilder
返回的数据类型。
如注释中所指定,将Scaffold
包裹在Stream Bulder
中,或使用您以前使用setState
的方法。
答案 1 :(得分:0)
StreamBuilder
不仅返回一个值,它还是带有构建器的窗口小部件本身,该构建器在流可用时提供流的值。将StreamBuilder
包裹在Scaffold
周围,并以相同的方式访问值。
Widget build(BuildContext context) {
return StreamBuilder<double>(
stream: bloc.appBarElevetionStream,
initialData: 0.0,
builder: (context, snapshot) {
return Scaffold(
appBar: AppBar(
title: StreamBuilder<Object>(
stream: bloc.titleStream,
initialData: home,
builder: (context, snapshot) {
return Text(snapshot.data);
}
),
backgroundColor: colorPrimary,
elevation: snapshot.data,
actions: <Widget>[
Builder(
builder: (context) => bloc.navigationProvider.currentNavigation == home ?
homeCalendar(context):otherCalendar(context) //if it's not 0 we return an empty widget
)
],
),
drawer: DrawerNavigation(),
body: homeScreen()
);
}
);
}