flutter_package_hyo 1.0.0
flutter_package_hyo: ^1.0.0 copied to clipboard
A Test Package.
example/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_package_hyo/flutter_package.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
// useMaterial3: true,
primarySwatch: Colors.grey,
highlightColor: const Color.fromRGBO(1, 0, 0, 0.0),
splashColor: const Color.fromRGBO(1, 0, 0, 0.0),
),
home: FriendsPage(),
);
}
}
class FriendsPage extends StatefulWidget {
@override
State<FriendsPage> createState() => _FriendsPageState();
}
class _FriendsPageState extends State<FriendsPage> with AutomaticKeepAliveClientMixin<FriendsPage> {
late ScrollController _scrollController;
var offsetDic = {};
static const double cellHeight = 44.5;
static const double headerHeight = 30;
@override
bool get wantKeepAlive => true;
@override
void initState() {
super.initState();
print("FriendsPage~initState");
_scrollController = ScrollController();
datas.sort((Friends a, Friends b) {
return (a.indexLetter ?? "").compareTo(b.indexLetter ?? "");
});
double offsetY = cellHeight * headDatas.length;
for (int i = 0; i < datas.length; i++) {
if (i == 0) {
offsetDic.addAll({datas[i].indexLetter: offsetY});
offsetY += (cellHeight + headerHeight);
} else if (datas[i].indexLetter == datas[i - 1].indexLetter) {
offsetY += cellHeight;
} else {
offsetDic.addAll({datas[i].indexLetter: offsetY});
offsetY += (cellHeight + headerHeight);
}
}
}
Widget cellForRow(context, index) {
if (index < headDatas.length) {
return FriendsCell(friends: headDatas[index], showHeader: false,);
}
var friends = datas[index - headDatas.length];
bool hiddenHeader = index - headDatas.length > 0 && (friends.indexLetter! == datas[index - headDatas.length - 1].indexLetter!);
return FriendsCell(friends: friends, showHeader: !hiddenHeader,);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: weChatThemeColor,
centerTitle: true,
elevation: 0.0,
title: const Text('通讯录页面'),
),
body: Stack(
children: [
ListView.builder(
controller: _scrollController,
itemBuilder:cellForRow,
itemCount: headDatas.length + datas.length,
),
Positioned(
top: screenHeight(context)/8,
// height: screenHeight(context)/2,
right: 0.0,
// width: 120,
child: IndexBar((indexName) {
if (offsetDic.keys.contains(indexName)) {
_scrollController.animateTo(offsetDic[indexName], duration: const Duration(milliseconds: 300), curve:Curves.ease);
}
})
),
],
),
);
}
}
class FriendsCell extends StatelessWidget {
FriendsCell({required this.friends, required this.showHeader});
final Friends friends;
final bool showHeader;
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
alignment: Alignment.centerLeft,
width: screenWidth(context),
color: weChatThemeColor,
height: showHeader ? 30 : 0,
padding: const EdgeInsets.only(left: 15),
child: Text(showHeader ? (friends.indexLetter ?? "") : ""),
),
Container(
color: Colors.white,
padding: const EdgeInsets.only(left: 15),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 44,
width: 44,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: friends.imageUrl != null ? Image(image: NetworkImage(friends.imageUrl!)) : Image(image: AssetImage(friends.assetImageName!)),
),
),
const SizedBox(width: 15,),
Text(friends.name),
],
),
Row(
children: [
const SizedBox(
width: 44 + 15,
),
Expanded(child: Container(
// width: screenWidth(context) - 44 - 15 - 15,
height: 0.5,
color: weChatThemeColor,
))
],
)
],
),
)
],
);
}
}
class Friends {
Friends(
{this.assetImageName, this.imageUrl, required this.name, this.indexLetter});
final String? assetImageName;
final String? imageUrl;
final String name;
String? indexLetter;
}
List<Friends> datas = [
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lina', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: '菲儿', indexLetter: 'F'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lida', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://upload-images.jianshu.io/upload_images/11480095-8dabc7ef2d6ecca8.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lina', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: '菲儿', indexLetter: 'F'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lida', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://upload-images.jianshu.io/upload_images/11480095-8dabc7ef2d6ecca8.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lina', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: '菲儿', indexLetter: 'F'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lida', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://upload-images.jianshu.io/upload_images/11480095-8dabc7ef2d6ecca8.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lina', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: '菲儿', indexLetter: 'F'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lida', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://upload-images.jianshu.io/upload_images/11480095-8dabc7ef2d6ecca8.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lina', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: '菲儿', indexLetter: 'F'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Lida', indexLetter: 'L'),
Friends(imageUrl: 'https://download.shmaas.cn/images/_ali_420a7b45bafd425eb0708fb871a18aab.png', name: 'Tana', indexLetter: 'T'),
Friends(imageUrl: 'https://upload-images.jianshu.io/upload_images/11480095-8dabc7ef2d6ecca8.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240', name: 'Tana', indexLetter: 'T'),
];
List<Friends> headDatas = [
Friends(assetImageName: 'images/person_payment.png', name: '新的朋友'),
Friends(assetImageName: 'images/person_payment.png', name: '群聊'),
Friends(assetImageName: 'images/person_payment.png', name: '标签'),
Friends(assetImageName: 'images/person_payment.png', name: '公众号'),
];
List<String> indexWords = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
];
const Color weChatThemeColor = Color.fromRGBO(220, 220, 220, 1);
double screenWidth(BuildContext context) => MediaQuery.of(context).size.width;
double screenHeight(BuildContext context) => MediaQuery.of(context).size.height;