栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Flutter中各种对话框的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter中各种对话框的使用

Flutter提供了多种对话框组件供开发者使用,以下代码中演示了常见对话框的实现,供大家参考,欢迎大家复制粘贴和吐槽。

import 'package:flutter/material.dart';
// pubspec.yaml 中配置 fluttertoast: ^8.0.7
import 'package:fluttertoast/fluttertoast.dart'; 

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

// 抽离成一个单独的组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        // 导航条
        appBar: AppBar(title: Text('Flutter各种对话框')),
        // 主体
        body: DialogPage(),
      ),
      theme: ThemeData(primarySwatch: Colors.yellow)
    );
  }
}

class DialogPage extends StatefulWidget {

  DialogPage({Key key}) : super(key: key);
  _DialogPageState createState() => _DialogPageState();

}

class _DialogPageState extends State {

  // 提示对话框
  _showToastDialog() {
    Fluttertoast.showToast(
      msg: "提示信息",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.black,
      textColor: Colors.white,
      fontSize: 16.0
    );
  }

  // 确认对话框
  _showalertDialog() async {
    var result = await showDialog(
      // 表示点击灰色背景的时候是否消失弹出框
      barrierDismissible: false, 
      context: context,
      builder: (context) {
        return alertDialog(
          title: Text("提示信息"),
          content: Text("您确定要删除吗?"),
          actions: [
            TextButton(
              child: Text("取消"),
              onPressed: () {
                print("取消");
                Navigator.pop(context, 'Cancle');
              },
            ),
            TextButton(
              child: Text("确定"),
              onPressed: () {
                print("确定");
                Navigator.pop(context, "Ok");
              }
            )
          ]
        );
      }
    );
    print(result);
  }

  // 选择对话框
  _showSelectDialog() async {
    var result = await showDialog(
      // 表示点击灰色背景的时候是否消失弹出框
      barrierDismissible: false, 
      context: context,
      builder: (context) {
        return SimpleDialog(

          title: Text("选择内容"),
          children: [

            SimpleDialogOption(
              child: Text("Option A"),
              onPressed: () {
                print("Option A");
                Navigator.pop(context, "A");
              },
            ),

            Divider(),

            SimpleDialogOption(
              child: Text("Option B"),
              onPressed: () {
                print("Option B");
                Navigator.pop(context, "B");
              },
            ),

            Divider(),
            
            SimpleDialogOption(
              child: Text("Option C"),
              onPressed: () {
                print("Option C");
                Navigator.pop(context, "C");
              },
            ),

            Divider()

          ]
        );
      }
    );
    print(result);
  }

  // 底部弹出选择框
  _showActionSheetDialog() async {
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return Container(
          height: 220,
          child: Column(
            children: [
              ListTile(
                title: Text("分享 A"),
                onTap: () {
                  Navigator.pop(context, "分享 A");
                }
              ),

              Divider(),

              ListTile(
                title: Text("分享 B"),
                onTap: () {
                  Navigator.pop(context, "分享 B");
                }
              ),

              Divider(),

              ListTile(
                title: Text("分享 C"),
                onTap: () {
                  Navigator.pop(context, "分享 C");
                }
              )
            ]
          )
        );
      }
    );
    print(result);
  }


  @override
  Widget build(BuildContext context) {
    return  Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [

          ElevatedButton(
            child: Text('提示对话框'),
            onPressed: _showToastDialog,
          ),

          SizedBox(height: 20),

          ElevatedButton(
            child: Text('确认对话框'),
            onPressed: _showalertDialog,
          ),
            
          SizedBox(height: 20),

          ElevatedButton(
            child: Text('选择对话框'),
            onPressed: _showSelectDialog,
          ),

          SizedBox(height: 20),

          ElevatedButton(
            child: Text('底部弹出选择框'),
            onPressed: _showActionSheetDialog,
          )

        ]
      )
    );
  }
}

页面渲染效果如下,对话框的效果大家可自行点击查看。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/658280.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号