如何在构建器中返回值?[颤振]



我的应用程序有一个PageView.builder,它根据日期显示页面(例如:初始页面将设置为DateTime.now()(,当用户向左或向右滑动时,它将相应地更改页面和显示的日期。

我还有一个日历,我希望它让用户跳转到他们在日历中选择的页面。所选日期位于此处的builder中:

selectedDayBuilder: (context, date, _) { //need to return date
//          var value = MonthPageViewExample(date);
return FadeTransition(
opacity: Tween(begin: 0.0, end: 1.0).animate(_animationController),
child: Container(
margin: const EdgeInsets.all(4.0),
padding: const EdgeInsets.only(top: 5.0, left: 6.0),
color: Colors.deepOrange[300],
width: 100,
height: 100,
child: Text(
'${date.day}',
style: TextStyle().copyWith(fontSize: 16.0),
),
),
);
},

我想使用日期作为参数,该参数将在PageView.builder传递,然后具有所选日期的页面将显示在屏幕上。

我可以得到一些关于如何将日期值存储在参数中(也许(以便我可以将其传递给PageView.builder的建议吗?

此外,我还试图找到有关如何在构建器方法中返回值的教程,但我没有运气。如果此方法可用于从任何类型的生成器方法返回值,也会有所帮助

我通过使用全局变量对此问题使用了临时解决方法。我已将变量定义为DateTime universal = DateTime.now().我以这种方式在selectedDayBuilder内传递了这个变量:

selectedDayBuilder: (context, date, _) {
Calendar_screen(dateToString(date));
universal = date;   // Selected date is assigned the user selected value here
return FadeTransition(
opacity: Tween(begin: 0.0, end: 1.0).animate(_animationController),
child: Container(
margin: const EdgeInsets.all(4.0),
padding: const EdgeInsets.only(top: 5.0, left: 6.0),
color: Colors.deepOrange[300],
width: 100,
height: 100,
child: GestureDetector(
onDoubleTap: () async{
final PageRouteBuilder _route = PageRouteBuilder(
pageBuilder: (BuildContext context, _, __) {
return DayPageViewExample(date);
});
await Navigator.pushAndRemoveUntil(context, _route, (Route<dynamic> r) => false);
},
child: Text(
'${date.day}',
style: TextStyle().copyWith(fontSize: 16.0),
),
),
),
);
},

我能够将DateTime universal传递给此屏幕中存在的任何其他小部件,并且它成功传递了selectedDayBuilder中选择的日期。

String text = "Edit this" ;
Navigator.of(context).push(pageRoute(EditTextPage(text:text))).
then((result) {
if(result != null){
text = result ;
}
});
},
Route pageRoute(Widget page) {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => page,
transitionsBuilder: (context, animation, secondaryAnimation, child) {
var begin = Offset(0.0, 1.0);
var end = Offset.zero;
var curve = Curves.ease;
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
return SlideTransition(
position: animation.drive(tween),
child: child,
);
},

(; }

最新更新