app-starlock/lib/tools/jh_pop_menus.dart

247 lines
7.2 KiB
Dart
Raw Normal View History

2023-07-10 17:50:31 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
2024-08-19 15:24:14 +08:00
import 'package:get/get.dart';
2023-07-10 17:50:31 +08:00
import 'screen_utils.dart';
// List _listData = [
2024-08-19 15:24:14 +08:00
// {'text': '读取记录'.tr},
// {'text': '清空记录'.tr},
// {'text': '导出记录'.tr},
// ];
2023-07-10 17:50:31 +08:00
const Color _bgColor = Color(0xFF2D2D2D);
const double _fontSize = 20.0;
2023-07-10 17:50:31 +08:00
const double _cellHeight = 50.0;
const double _imgWH = 22.0;
class JhPopMenus {
2024-08-19 15:24:14 +08:00
// 显示pop
2023-07-10 17:50:31 +08:00
static void show(
BuildContext context, {
required List<dynamic> listData,
2023-07-15 15:11:28 +08:00
Function(int selectIndex, String selectText)? clickCallback,
2023-07-10 17:50:31 +08:00
}) {
// Cell
Widget buildMenuCell(dataArr) {
return ListView.builder(
itemCount: dataArr.length,
itemExtent: _cellHeight.h,
2023-07-10 17:50:31 +08:00
padding: const EdgeInsets.all(0.0),
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
return Material(
color: _bgColor,
child: InkWell(
onTap: () {
clickCallback?.call(index, listData[index]['text']);
2023-07-10 17:50:31 +08:00
Navigator.pop(context);
},
child: Row(
children: <Widget>[
2024-08-19 15:24:14 +08:00
const SizedBox(width: 25),
2023-07-10 17:50:31 +08:00
// Image.asset(dataArr[index]['icon'], width: _imgWH, height: _imgWH, color: Colors.white),
// const SizedBox(width: 15),
2024-08-19 15:24:14 +08:00
Text('你好'.tr,
style: TextStyle(
color: Colors.white, fontSize: _fontSize.sp)),
2023-07-10 17:50:31 +08:00
],
),
),
);
},
);
}
Widget menusView(dataArr) {
var cellH = dataArr.length * _cellHeight.h;
2024-08-19 15:24:14 +08:00
double navH = JhScreenUtils.navigationBarHeight;
2023-07-10 17:50:31 +08:00
return Positioned(
right: 10,
top: navH - 10,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Image.asset('images/ic_menu_up_arrow.png', width: 28, height: 5),
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: Container(
color: _bgColor,
width: 160,
height: cellH,
child: buildMenuCell(dataArr)),
2023-07-10 17:50:31 +08:00
)
],
),
);
}
Navigator.of(context)
.push(DialogRouter(_BasePopMenus(child: menusView(listData))));
2023-07-10 17:50:31 +08:00
}
2024-08-19 15:24:14 +08:00
// 显示带线带背景 pop
static void showLinePop(
BuildContext context, {
2024-08-19 15:24:14 +08:00
required List<dynamic> listData, bool isShowBg = false,
Function(int selectIndex, String selectText)? clickCallback,
}) {
2023-07-10 17:50:31 +08:00
// 带线
Widget buildMenuLineCell(dataArr) {
return ListView.separated(
itemCount: dataArr.length,
padding: const EdgeInsets.all(0.0),
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
return Material(
color: _bgColor,
child: InkWell(
onTap: () {
clickCallback?.call(index, listData[index]['text']);
2023-07-10 17:50:31 +08:00
Navigator.pop(context);
},
child: SizedBox(
height: _cellHeight.h,
// padding: EdgeInsets.only(left: 20.h),
2023-07-10 17:50:31 +08:00
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
2023-07-10 17:50:31 +08:00
children: <Widget>[
// const SizedBox(width: 25),
// Image.asset(dataArr[index]['icon'], width: _imgWH, height: _imgWH, color: Colors.white),
// const SizedBox(width: 12),
Center(
child: Text(dataArr[index]['text'],
style: TextStyle(
color: Colors.white, fontSize: _fontSize.sp)))
2023-07-10 17:50:31 +08:00
],
),
),
),
);
},
2024-08-19 15:24:14 +08:00
separatorBuilder: (BuildContext context, int index) => Divider(
height: .1,
indent: 20.w,
endIndent: 20.w,
2024-08-19 15:24:14 +08:00
color: const Color(0xFFE6E6E6)),
2023-07-10 17:50:31 +08:00
);
}
Widget menusView(dataArr) {
2024-08-19 15:24:14 +08:00
final cellH = dataArr.length * _cellHeight.h;
double navH = JhScreenUtils.navigationBarHeight;
2023-07-10 17:50:31 +08:00
if (isShowBg == true) {
navH = navH - JhScreenUtils.topSafeHeight;
} else {
navH = navH - 10;
}
return Positioned(
right: 10,
top: navH,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Image.asset('images/ic_menu_up_arrow.png', width: 28, height: 5),
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: Container(
color: _bgColor,
width: 180.w,
height: cellH,
child: buildMenuLineCell(dataArr)))
2023-07-10 17:50:31 +08:00
],
),
);
}
if (isShowBg == true) {
// 带背景
showDialog(
context: context,
barrierDismissible: false,
2024-08-19 15:24:14 +08:00
builder: (BuildContext context) {
return _BasePopMenus(child: menusView(listData));
2023-07-10 17:50:31 +08:00
},
);
} else {
Navigator.of(context)
.push(DialogRouter(_BasePopMenus(child: menusView(listData))));
2023-07-10 17:50:31 +08:00
}
}
}
class _BasePopMenus extends Dialog {
const _BasePopMenus({
Key? key,
this.child,
}) : super(key: key);
2024-08-19 15:24:14 +08:00
final child;
2023-07-10 17:50:31 +08:00
@override
Widget build(BuildContext context) {
return Material(
type: MaterialType.transparency,
child: Stack(
fit: StackFit.expand,
children: <Widget>[
GestureDetector(onTap: () => Navigator.pop(context)),
// 内容
child ?? Container()
],
),
);
}
}
class DialogRouter extends PageRouteBuilder {
DialogRouter(this.page)
: super(
opaque: false,
// 自定义遮罩颜色
barrierColor: Colors.white10.withAlpha(1),
transitionDuration: const Duration(milliseconds: 150),
2024-08-19 15:24:14 +08:00
pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) => page,
transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) =>
child,
2023-07-10 17:50:31 +08:00
);
2024-08-19 15:24:14 +08:00
final Widget page;
2023-07-10 17:50:31 +08:00
}
class CustomDialog extends Dialog {
const CustomDialog({
2023-07-15 15:11:28 +08:00
Key? key,
2023-07-10 17:50:31 +08:00
this.child,
this.clickBgHidden = false, // 点击背景隐藏,默认不隐藏
}) : super(key: key);
2024-08-19 15:24:14 +08:00
final bool clickBgHidden;
final child;
2023-07-10 17:50:31 +08:00
@override
Widget build(BuildContext context) {
return Material(
// 透明层
type: MaterialType.transparency,
child: Stack(
children: <Widget>[
InkWell(
onTap: () {
if (clickBgHidden == true) {
Navigator.pop(context);
}
},
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
),
),
// 内容
Center(child: child)
],
),
);
}
}