【发布时间】:2019-11-01 03:25:11
【问题描述】:
我有一个基于 StatelessWidget 的 Flutter 应用程序。我看到example 和StatefulWidget 允许程序员在dispose() 方法中执行一些操作。我想看看StatelessWidget是否也提供这样的功能?
import 'package:flutter/material.dart';
import 'routes.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
primarySwatch: colorCustom,
fontFamily: 'Roboto',
),
initialRoute: '/',
routes: routes,
);
}
}
更新
import 'package:flutter/material.dart';
import 'routes.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
Future<bool> _exitApp(BuildContext context) {
return showDialog(
context: context,
child: new AlertDialog(
title: new Text('Do you want to exit this application?'),
content: new Text('We hate to see you leave...'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('No'),
),
new FlatButton(
onPressed: () => Navigator.of(context).pop(true),
child: new Text('Yes'),
),
],
),
) ??
false;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
home: WillPopScope(
child: MaterialApp(
theme: ThemeData(
primarySwatch: colorCustom,
fontFamily: 'Roboto',
),
initialRoute: '/',
routes: routes
),
onWillPop: () => _exitApp(context)
)
);
}
嗯,更新代码后,AlertDialog 没有弹出?
【问题讨论】:
标签: flutter