flutter使用更改日期在其他小部件中
我有两个状态的小部件。在一个小部件中,我在DateTime中获得了一个实际日期,然后单击一个按钮。 我想在第二个小部件中使用更改的日期。我想通过提供商将实际日期保存在变量中的提供商中。我想在第二个小部件中调用这个变量,因此我可以使用该小部件中使用的更改日期。但是我不确定,这是最佳选择或如何调用该函数,因此只能返回日期。
提供商类DateModel:
import 'package:flutter/material.dart';
class DateModel with ChangeNotifier {
var dateGeneral = DateTime(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
DateTime.now().weekday,
);
DateTime actualSavedDate(DateTime actualTime) {
DateTime actualSavedTime = actualTime;
actualSavedTime.day;
return actualSavedTime;
}
DateTime monatHigher(DateTime actualHigherDate) {
DateTime dateHigherMonth = DateTime(
DateTime.now().year,
actualHigherDate.month + 1,
DateTime.now().day,
DateTime.now().weekday,
);
notifyListeners();
return dateHigherMonth;
}
DateTime monatLower(DateTime actualLowerMonth) {
DateTime dateLowerMonth = DateTime(
DateTime.now().year,
actualLowerMonth.month - 1,
DateTime.now().day,
DateTime.now().weekday,
);
notifyListeners();
return dateLowerMonth;
}
}
更改日期的小部件;
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:date_format/date_format.dart';
import 'package:dart_date/dart_date.dart';
import 'package:provider/provider.dart';
import '../provider/date_model.dart';
class MonatUmschalt extends StatefulWidget {
@override
_MonatUmschaltState createState() => _MonatUmschaltState();
}
class _MonatUmschaltState extends State<MonatUmschalt> {
DateTime actualDate = DateTime.now();
@override
Widget build(BuildContext context) {
final dateData = Provider.of<DateModel>(context);
dateData.actualSavedDate(actualDate);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () {
actualDate = dateData.monatLower(actualDate);
dateData.actualSavedDate(actualDate);
},
icon: Icon(Icons.arrow_circle_left_sharp),
),
Container(
color: Colors.amber[600],
width: 100,
height: 40.0,
child: Center(
child: Text(
//(actualDate == dateChangedMonth)
// ? dateChangedMonth.format('MMMM yyyy', 'de_DE')
/*:*/ actualDate.format('MMMM yyyy', 'de_DE'),
textAlign: TextAlign.center,
),
),
),
IconButton(
onPressed: () {
actualDate = dateData.monatHigher(actualDate);
dateData.actualSavedDate(actualDate);
},
icon: Icon(Icons.arrow_circle_right_sharp),
),
],
);
}
}
我认为不是第二个小部件,因为在此中,我只想使用更改的日期。但是,每次在第一个小部件中更改它时,必须在第二个小部件中更新日期。
感谢您的
帮助 帕特里克
I've got two stateful widgets. In one widget I got an actual date in DateTime, which I change with a click on a Button.
I want to use the changed date in the second Widget. I want to achieve this with the Provider in which I save the actual date in a variable. This variable I want to call in the second widget, so I can use the use the changed date in that widget. But I'm not sure, wether this is the best option or how to call the function so that only the date is returned.
the provider class DateModel:
import 'package:flutter/material.dart';
class DateModel with ChangeNotifier {
var dateGeneral = DateTime(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
DateTime.now().weekday,
);
DateTime actualSavedDate(DateTime actualTime) {
DateTime actualSavedTime = actualTime;
actualSavedTime.day;
return actualSavedTime;
}
DateTime monatHigher(DateTime actualHigherDate) {
DateTime dateHigherMonth = DateTime(
DateTime.now().year,
actualHigherDate.month + 1,
DateTime.now().day,
DateTime.now().weekday,
);
notifyListeners();
return dateHigherMonth;
}
DateTime monatLower(DateTime actualLowerMonth) {
DateTime dateLowerMonth = DateTime(
DateTime.now().year,
actualLowerMonth.month - 1,
DateTime.now().day,
DateTime.now().weekday,
);
notifyListeners();
return dateLowerMonth;
}
}
the widget in which the date is changed;
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:date_format/date_format.dart';
import 'package:dart_date/dart_date.dart';
import 'package:provider/provider.dart';
import '../provider/date_model.dart';
class MonatUmschalt extends StatefulWidget {
@override
_MonatUmschaltState createState() => _MonatUmschaltState();
}
class _MonatUmschaltState extends State<MonatUmschalt> {
DateTime actualDate = DateTime.now();
@override
Widget build(BuildContext context) {
final dateData = Provider.of<DateModel>(context);
dateData.actualSavedDate(actualDate);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () {
actualDate = dateData.monatLower(actualDate);
dateData.actualSavedDate(actualDate);
},
icon: Icon(Icons.arrow_circle_left_sharp),
),
Container(
color: Colors.amber[600],
width: 100,
height: 40.0,
child: Center(
child: Text(
//(actualDate == dateChangedMonth)
// ? dateChangedMonth.format('MMMM yyyy', 'de_DE')
/*:*/ actualDate.format('MMMM yyyy', 'de_DE'),
textAlign: TextAlign.center,
),
),
),
IconButton(
onPressed: () {
actualDate = dateData.monatHigher(actualDate);
dateData.actualSavedDate(actualDate);
},
icon: Icon(Icons.arrow_circle_right_sharp),
),
],
);
}
}
I think the second widget is not necessary, because in this I just want to use the changed date. But every time it is changed in the first widget, the date has to be updated in the second.
Thank your for your help
Best regards
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论