当前位置:网站首页>【Flutter -- GetX】弹框 - Dialog、Snackbar、BottomSheet
【Flutter -- GetX】弹框 - Dialog、Snackbar、BottomSheet
2022-07-26 20:32:00 【Kevin-Dev】

前言
本文是基于官方最新稳定版本 get: ^4.5.1 来进行开发。
通过阅读本文,你将学会如下:
- 会使用 Dialog
- 会使用 Snackbar
- 会使用 BottomSheet
GetX 集成
1. 在 pubspec.yaml 文件中添加 GetX 的依赖,如下:
dependencies:
flutter:
sdk: flutter
get: ^4.5.1
2. 需要对 GetX 进行初始化,将默认的 MaterialApp 替换为 GetMaterialApp 即可,如下:
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
Dialog
1. 简介
Dialog 底层其实是对 AlertDialog 进行了封装, 一般用于二次确认的弹出框,比如当点击某个按钮提交资料时,需要用户二次确认,以防止误操作。
2. 属性


3. 使用
- 效果图

- 代码
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: Text('GetX 对话框基本使用'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: (){
Get.defaultDialog(onConfirm: () => print("Ok"), middleText: "Dialog made in 3 lines of code");
},
child: Text("显示 Dialog"),
),
],
),
)
)
);
}
}
Snackbar
1. 简介
如果想在应用程序中触发某些特定的事件后,需要弹出一则快捷消息,那么使用 Snackbar 则是最佳的选择,接下来我们看一下 GetX 如何来联调 Snackbar 来使用。
2. 属性



3. 使用
效果图

代码
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: Text('GetX 对话框基本使用'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: (){
Get.snackbar("Snackbar 标题", "欢迎使用Snackbar");
},
child: Text("显示 Snackbar"),
),
],
),
)
)
);
}
}
BottomSheet
1. 简介
BottomSheet 是底部弹出的一个组件,常用于单选、验证码二次校验弹窗等,GetX 的 BottomSheet 底部弹出是自定义通过路由push 的方法实现底部弹窗的一个效果。
2. 属性

3. 使用
- 效果图

- 代码
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: Text('GetX 对话框基本使用'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: (){
Get.bottomSheet(
Container(
child: Wrap(
children: <Widget>[
ListTile(leading: Icon(Icons.music_note), title: Text('Music'), onTap: () => {
}),
ListTile(
leading: Icon(Icons.videocam),
title: Text('Video'),
onTap: () => {
},
),
],
),
),
backgroundColor: Colors.white);
},
child: Text("BottomSheets"),
),
],
),
)
)
);
}
}
边栏推荐
- Niuke brush questions - MySQL series
- Why does it system need observability?
- 平滑滚动到元素
- Alkbh1
- Sign up now: July 29 recommendation system summit 2022
- 记一次invalid bound statement xxxxxx 问题解决思路
- encodeURI VS encodeURIComponent
- 测试用例千万不能随便,记录由一个测试用例异常引起的思考
- [Oracle training] - deploy Ogg known as zero downtime migration
- Deepfake pinches his face. It's hard to tell whether it's true or false. Tom Cruise is more like himself than himself!
猜你喜欢
![[must read new] Keya valuation analysis of University of technology, heating energy-saving products](/img/e8/c3ac4e5f11db58031cb9249a4ba0f4.jpg)
[must read new] Keya valuation analysis of University of technology, heating energy-saving products

一些意想不到的bug记录

How to configure the legendary SF lander to automatically read the list without a network

4年软件测试工作经验,跳槽之后面试20余家公司的总结

【HCIA安全】用户认证

Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management
Monitor MySQL based on MySQL exporter

idea中debug时如何进入指定的用户方法体中?

GOM login configuration free version generate graphic tutorial

How to enter the specified user method body when debugging in idea?
随机推荐
LeetCode 练习——剑指 Offer II 005. 单词长度的最大乘积
Devops has been practiced for many years. What is the most painful thing?
[HCIA security] bidirectional nat
Broadcast voice H5 speechsynthesisutterance
Basic use of livedatade
(C语言)浅识#define
洛谷-线段覆盖-(区间排序问题总结)
Daily practice ----- there is a group of students' grades. Arrange them in descending order. To add a student's grade, insert it into the grade sequence and keep the descending order
TCP的粘包拆包问题解决方案
TypeScript中的类型断言
Deployment of kubernetes
Niuke multi school -journey- (map building distra+ card often optimization)
:active vs :focus
Flutter Performance Optimization Practice - UI chapter
2022开放原子全球开源峰会议程速递 | 7 月 27 日分论坛议程一览
在上传之前预览图像
[Oracle training] - deploy Ogg known as zero downtime migration
功能尝鲜 | 解密 Doris 复杂数据类型 ARRAY
和月薪3W的字节程序员聊过后,才知道自己一直在打杂...
我们被一个 kong 的性能 bug 折腾了一个通宵