init project
This commit is contained in:
40
example/lib/widgets/amap_gridview.dart
Normal file
40
example/lib/widgets/amap_gridview.dart
Normal file
@ -0,0 +1,40 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AMapGradView extends StatefulWidget {
|
||||
///子控件列表
|
||||
final List<Widget> childrenWidgets;
|
||||
|
||||
///一行的数量
|
||||
final int? crossAxisCount;
|
||||
|
||||
///宽高比
|
||||
final double? childAspectRatio;
|
||||
|
||||
AMapGradView(
|
||||
{Key? key,
|
||||
this.crossAxisCount,
|
||||
this.childAspectRatio,
|
||||
required this.childrenWidgets})
|
||||
: super(key: key);
|
||||
@override
|
||||
_GradViewState createState() => _GradViewState();
|
||||
}
|
||||
|
||||
class _GradViewState extends State<AMapGradView> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView.count(
|
||||
primary: false,
|
||||
physics: new NeverScrollableScrollPhysics(),
|
||||
//水平子Widget之间间距
|
||||
crossAxisSpacing: 1.0,
|
||||
//垂直子Widget之间间距
|
||||
mainAxisSpacing: 0.5,
|
||||
//一行的Widget数量
|
||||
crossAxisCount: widget.crossAxisCount ?? 2,
|
||||
//宽高比
|
||||
childAspectRatio: widget.childAspectRatio ?? 4,
|
||||
children: widget.childrenWidgets,
|
||||
shrinkWrap: true);
|
||||
}
|
||||
}
|
77
example/lib/widgets/amap_radio_group.dart
Normal file
77
example/lib/widgets/amap_radio_group.dart
Normal file
@ -0,0 +1,77 @@
|
||||
import 'package:amap_map_example/widgets/amap_gridview.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AMapRadioGroup<T> extends StatefulWidget {
|
||||
final String? groupLabel;
|
||||
final T? groupValue;
|
||||
final Map<String, T>? radioValueMap;
|
||||
final ValueChanged<T>? onChanged;
|
||||
AMapRadioGroup(
|
||||
{Key? key,
|
||||
this.groupLabel,
|
||||
this.groupValue,
|
||||
this.radioValueMap,
|
||||
this.onChanged})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
_AMapRadioGroupState createState() => _AMapRadioGroupState();
|
||||
}
|
||||
|
||||
class _AMapRadioGroupState extends State<AMapRadioGroup> {
|
||||
dynamic _groupValue;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_groupValue = widget.groupValue ?? null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
List<Widget> radioList = <Widget>[];
|
||||
_groupValue = widget.groupValue ?? null;
|
||||
Widget _myRadio(String label, dynamic radioValue) {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(label),
|
||||
Radio<dynamic>(
|
||||
value: radioValue,
|
||||
groupValue: _groupValue,
|
||||
onChanged: (_value) {
|
||||
setState(() {
|
||||
_groupValue = _value;
|
||||
});
|
||||
if (null != widget.onChanged) {
|
||||
widget.onChanged!(_value);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
if (widget.radioValueMap != null) {
|
||||
widget.radioValueMap!.forEach((key, value) {
|
||||
radioList.add(_myRadio(key, value));
|
||||
});
|
||||
}
|
||||
return Container(
|
||||
padding: EdgeInsets.all(5),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(widget.groupLabel!),
|
||||
Container(
|
||||
padding: EdgeInsets.only(left: 10),
|
||||
child: AMapGradView(
|
||||
childrenWidgets: radioList,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
57
example/lib/widgets/amap_switch_button.dart
Normal file
57
example/lib/widgets/amap_switch_button.dart
Normal file
@ -0,0 +1,57 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
typedef void OnChanged(bool value);
|
||||
|
||||
class AMapSwitchButton extends StatefulWidget {
|
||||
const AMapSwitchButton({
|
||||
Key? key,
|
||||
this.label,
|
||||
this.onSwitchChanged,
|
||||
this.defaultValue = true,
|
||||
}) : super(key: key);
|
||||
|
||||
final Text? label;
|
||||
final Function? onSwitchChanged;
|
||||
final bool defaultValue;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _SwitchButtonState();
|
||||
}
|
||||
|
||||
class _SwitchButtonState extends State<AMapSwitchButton> {
|
||||
late bool _localValue;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_localValue = widget.defaultValue;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
widget.label!,
|
||||
Switch(
|
||||
value: _localValue,
|
||||
onChanged: (null != widget.onSwitchChanged)
|
||||
? (value) {
|
||||
setState(() {
|
||||
_localValue = value;
|
||||
});
|
||||
widget.onSwitchChanged!(value);
|
||||
}
|
||||
: null,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
70
example/lib/widgets/demo_group.dart
Normal file
70
example/lib/widgets/demo_group.dart
Normal file
@ -0,0 +1,70 @@
|
||||
import 'package:amap_map_example/base_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class DemoGroupWidget extends StatefulWidget {
|
||||
final String groupLabel;
|
||||
final List<BasePage>? itemPages;
|
||||
|
||||
DemoGroupWidget({Key? key, required this.groupLabel, this.itemPages})
|
||||
: super(key: key);
|
||||
@override
|
||||
State<StatefulWidget> createState() => _GroupState();
|
||||
}
|
||||
|
||||
class _GroupState extends State<DemoGroupWidget> {
|
||||
void _pushPage(BuildContext context, BasePage page) {
|
||||
Navigator.of(context).push(MaterialPageRoute<void>(
|
||||
builder: (_) => Scaffold(
|
||||
appBar: AppBar(title: Text(page.title)),
|
||||
body: page,
|
||||
)));
|
||||
}
|
||||
|
||||
bool _hasItemPages = false;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.itemPages != null && widget.itemPages!.length > 0) {
|
||||
_hasItemPages = true;
|
||||
} else {
|
||||
_hasItemPages = false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(
|
||||
widget.groupLabel,
|
||||
style: TextStyle(fontWeight: FontWeight.w600, fontSize: 18),
|
||||
)),
|
||||
Divider(height: 1),
|
||||
Container(
|
||||
padding: EdgeInsets.only(left: 10),
|
||||
child: _hasItemPages
|
||||
? ListView.separated(
|
||||
shrinkWrap: true,
|
||||
physics: NeverScrollableScrollPhysics(),
|
||||
itemCount: widget.itemPages!.length,
|
||||
itemBuilder: (_, int index) => ListTile(
|
||||
title: Text(widget.itemPages![index].title),
|
||||
subtitle: Text(widget.itemPages![index].subTitle),
|
||||
onTap: () => _pushPage(context, widget.itemPages![index]),
|
||||
),
|
||||
separatorBuilder: (BuildContext context, int index) =>
|
||||
Divider(
|
||||
height: 1,
|
||||
indent: 16,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
Divider(height: 1),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
136
example/lib/widgets/toast.dart
Normal file
136
example/lib/widgets/toast.dart
Normal file
@ -0,0 +1,136 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
//Toast 显示位置控制
|
||||
enum ToastPostion {
|
||||
top,
|
||||
center,
|
||||
bottom,
|
||||
}
|
||||
|
||||
class Toast {
|
||||
// toast靠它加到屏幕上
|
||||
static OverlayEntry? _overlayEntry;
|
||||
// toast是否正在showing
|
||||
static bool _showing = false;
|
||||
// 开启一个新toast的当前时间,用于对比是否已经展示了足够时间
|
||||
static DateTime? _startedTime;
|
||||
// 提示内容
|
||||
static String? _msg;
|
||||
// toast显示时间
|
||||
static int _showTime = 10;
|
||||
// 背景颜色
|
||||
static Color? _bgColor;
|
||||
// 文本颜色
|
||||
static Color? _textColor;
|
||||
// 文字大小
|
||||
static double? _textSize;
|
||||
// 显示位置
|
||||
static ToastPostion? _toastPosition;
|
||||
// 左右边距
|
||||
static double? _pdHorizontal;
|
||||
// 上下边距
|
||||
static double? _pdVertical;
|
||||
static void toast(
|
||||
BuildContext context, {
|
||||
//显示的文本
|
||||
String? msg,
|
||||
//显示的时间 单位毫秒
|
||||
int showTime = 1000,
|
||||
//显示的背景
|
||||
Color bgColor = Colors.black,
|
||||
//显示的文本颜色
|
||||
Color textColor = Colors.white,
|
||||
//显示的文字大小
|
||||
double textSize = 14.0,
|
||||
//显示的位置
|
||||
ToastPostion position = ToastPostion.center,
|
||||
//文字水平方向的内边距
|
||||
double pdHorizontal = 20.0,
|
||||
//文字垂直方向的内边距
|
||||
double pdVertical = 10.0,
|
||||
}) async {
|
||||
assert(msg != null);
|
||||
_msg = msg;
|
||||
_startedTime = DateTime.now();
|
||||
_showTime = showTime;
|
||||
_bgColor = bgColor;
|
||||
_textColor = textColor;
|
||||
_textSize = textSize;
|
||||
_toastPosition = position;
|
||||
_pdHorizontal = pdHorizontal;
|
||||
_pdVertical = pdVertical;
|
||||
//获取OverlayState
|
||||
OverlayState overlayState = Overlay.of(context)!;
|
||||
_showing = true;
|
||||
if (_overlayEntry == null) {
|
||||
//OverlayEntry负责构建布局
|
||||
//通过OverlayEntry将构建的布局插入到整个布局的最上层
|
||||
_overlayEntry = OverlayEntry(
|
||||
builder: (BuildContext context) => Positioned(
|
||||
//top值,可以改变这个值来改变toast在屏幕中的位置
|
||||
top: buildToastPosition(context),
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
width: MediaQuery.of(context).size.width,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 40.0),
|
||||
child: AnimatedOpacity(
|
||||
opacity: _showing ? 1.0 : 0.0, //目标透明度
|
||||
duration: _showing
|
||||
? Duration(milliseconds: 100)
|
||||
: Duration(milliseconds: 400),
|
||||
child: _buildToastWidget(),
|
||||
),
|
||||
)),
|
||||
));
|
||||
//插入到整个布局的最上层
|
||||
overlayState.insert(_overlayEntry!);
|
||||
} else {
|
||||
//重新绘制UI,类似setState
|
||||
_overlayEntry!.markNeedsBuild();
|
||||
}
|
||||
// 等待时间
|
||||
await Future.delayed(Duration(milliseconds: _showTime));
|
||||
//2秒后 到底消失不消失
|
||||
if (DateTime.now().difference(_startedTime!).inMilliseconds >= _showTime) {
|
||||
_showing = false;
|
||||
_overlayEntry!.markNeedsBuild();
|
||||
await Future.delayed(Duration(milliseconds: 400));
|
||||
_overlayEntry!.remove();
|
||||
_overlayEntry = null;
|
||||
}
|
||||
}
|
||||
|
||||
//toast绘制
|
||||
static _buildToastWidget() {
|
||||
return Center(
|
||||
child: Card(
|
||||
color: _bgColor,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: _pdHorizontal!, vertical: _pdVertical!),
|
||||
child: Text(
|
||||
_msg!,
|
||||
style: TextStyle(
|
||||
fontSize: _textSize,
|
||||
color: _textColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 设置toast位置
|
||||
static buildToastPosition(context) {
|
||||
var backResult;
|
||||
if (_toastPosition == ToastPostion.top) {
|
||||
backResult = MediaQuery.of(context).size.height * 1 / 4;
|
||||
} else if (_toastPosition == ToastPostion.center) {
|
||||
backResult = MediaQuery.of(context).size.height * 2 / 5;
|
||||
} else {
|
||||
backResult = MediaQuery.of(context).size.height * 3 / 4;
|
||||
}
|
||||
return backResult;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user