@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Hello Flutter"),),
body: Column(children: [
GestureDetector(
onTap: () {
showTimePicker(
context: context,
initialTime: const TimeOfDay(hour: 7, minute: 0),
);
},
child: Text("22:30")
)
Trying to add timePicker to my Flutter application like this. Tapping on the text results in error.
E/flutter (23737): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception: E/flutter (23737): NoSuchMethodError: The getter 'modalBarrierDismissLabel' was called on null. E/flutter (23737): Receiver: null E/flutter (23737): Tried calling: modalBarrierDismissLabel E/flutter (23737): #0
Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:48:5) E/flutter (23737): #1 showDialog (package:flutter/src/material/dialog.dart:615:53) E/flutter (23737):
This is a problem because the context passed to the showTimePicker()
is above the MaterialApp
in the widget tree.
To fix it you need another context and you can use a Builder
below MaterialApp
.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Hello Flutter"),
),
body: Builder(
builder: (context) => Column(
children: [
GestureDetector(
onTap: () {
showTimePicker(
context: context,
initialTime: const TimeOfDay(hour: 7, minute: 0),
);
},
child: Text("22:30"),
)
],
),
),
),
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.