main.dart
import 'package:flutter/material.dart';
import './components/MyDialog.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
//标题栏
appBar: AppBar(
title: Text("Dialog Demo"),
),
//内容区域
body: DatePickerPubPage(),
),
//主题
theme: ThemeData(primarySwatch: Colors.red),
);
}
}
class DatePickerPubPage extends StatefulWidget {
@override
_DatePickerPubPageState createState() => _DatePickerPubPageState();
}
class _DatePickerPubPageState extends State {
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
child: Text("自定义Dialog"),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return MyDialog("关于我们","我是内容");
});
},
),
],
),
);
}
}
MyDialog.dart
import 'package:flutter/material.dart';
import 'dart:async';
class MyDialog extends Dialog {
String title;
String content;
MyDialog(this.title, this.content);
//定时器,3秒后自动关闭dialog
_showTimer(context) {
Timer.periodic(Duration(milliseconds: 3000), (t) {
Navigator.pop(context);
t.cancel();
});
}
@override
Widget build(BuildContext context) {
_showTimer(context);
// TODO: implement build
return Material(
type: MaterialType.transparency,
child: Center(
child: Container(
height: 200,
width: 300,
color: Colors.white,
child: Column(
children: [
Padding(
padding: EdgeInsets.all(20),
child: Stack(
children: [
Align(
alignment: Alignment.center,
child: Text("${this.title}"),
),
Align(
alignment: Alignment.centerRight,
child: InkWell(
child: Icon(Icons.close),
onTap: () {
Navigator.pop(context);
},
),
),
],
),
),
Divider(),
Container(
padding: EdgeInsets.all(10),
width: double.infinity,
child: Text(
"${this.content}",
textAlign: TextAlign.left,
),
),
],
),
),
),
);
}
}
显示效果:
作者:蓝枫amy