Flutter showAboutDialog 组件的代码演示

27 min read

以下是一个使用showAboutDialog组件的简单示例代码:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'About Dialog Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _showAboutDialog() {
    showAboutDialog(
      context: context,
      applicationName: 'Flutter Demo',
      applicationVersion: '1.0.0',
      applicationIcon: FlutterLogo(),
      children: [
        Text('This is a demo app.'),
        SizedBox(height: 10),
        Text('Made with Flutter.'),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('About Dialog Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _showAboutDialog,
          child: Text('Show About Dialog'),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用程序,包含一个按钮。当按钮被点击时,将显示一个包含应用程序信息的About Dialog。其中showAboutDialog函数接受多个参数,包括context(上下文对象)、applicationName(应用程序名称)、applicationVersion(应用程序版本)、applicationIcon(应用程序图标)和children(自定义的子组件列表)。可以根据需要自定义About Dialog的内容。