我在第一个Dart文件(main.dart)中使用了一个休闲的showTimePicker
import 'package:flutter/material.dart';
import 'dart:async';
//import 'package:android_alarm_manager/android_alarm_manager.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CreateWeckerPage('Wecker erstellen'),
);
}
}
class CreateWeckerPage extends StatefulWidget{
CreateWeckerPage(this.name):super();
final String name;
@override
_CreateWeckerPage createState() => _CreateWeckerPage();
}
class _CreateWeckerPage extends State<CreateWeckerPage>{
TimeOfDay _time = new TimeOfDay.now();
//Zeit Picker Funktion
Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay picked = await showTimePicker(
context: context,
initialTime: _time,
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
child: child,
);
}
);
if(picked != null && picked != _time){
print('Timeselected: ${_time.toString()}');
setState((){
_time = picked;
});
}
}
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('${widget.name}')
),
body: Container(
child: Column(
children: <Widget>[
Text('Time Selected: ${_time.toString()}'),
RaisedButton(
onPressed: (){
_selectTime(context);
},
child: Text(
"Zeit eingeben",
),
),
],
),
),
);
}
}
我想保留代码,并开始将另一个dart文件中的页面和_selectTime函数外包到另一个dart文件中。所以我创建了一个名为createWeckerPage.dart的文件
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:wecker/Funktions/timePicker.dart';
class CreateWeckerPage extends StatefulWidget{
CreateWeckerPage(this.name):super();
final String name;
@override
_CreateWeckerPage createState() => _CreateWeckerPage();
}
class _CreateWeckerPage extends State<CreateWeckerPage>{
TimeOfDay _time;
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('${widget.name}')
),
body: Container(
child: Column(
children: <Widget>[
Text('Time Selected: ${_time.toString()}'),
RaisedButton(
onPressed: (){
testClass.testFunktion();
_time = _selectTime(context):
);
},
child: Text(
"Zeit eingeben",
),
),
],
),
),
);
}
}
和一个timePicker.dart
import 'package:flutter/material.dart';
import 'dart:async';
Future<Null> _selectTime(BuildContext context) async {
TimeOfDay _time = TimeOfDay.now();
final TimeOfDay picked = await showTimePicker(
context: context,
initialTime: _time,
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
child: child,
);
}
);
if(picked != null && picked != _time){
print('Timeselected: ${_time.toString()}');
return picked;
}
return _time;
}
问题是,如标题中所述,在createWeckerPage类中未定义_selectTime方法。如何实现Future _selectTime函数以在createWeckerPage.dart内部工作以显示在应用程序中?
谢谢。
答案 0 :(得分:0)
下划线表示不公开,您可以删除_下划线
请从
_selectTime(BuildContext context)
到
selectTime(BuildContext context)