[英]how to achieve gridview horizontally scrolling (carousell) for List<Widget> function in flutter?
I have a list of menu and it's overpopulate inside one page because the list of menu is more than expected, I want to change this listview into gridview with horizontally scrolling or carousell swipe, so other menu (card) will be shown when I swipe it to left, previously I use List<Widget> function
and ListView()
to create a list of card.我有一个菜单列表,它在一页内填充过多,因为菜单列表超出预期,我想通过水平滚动或轮播滑动将此列表视图更改为 gridview,因此当我滑动它时将显示其他菜单(卡片)在左侧,我之前使用
List<Widget> function
和ListView()
创建卡片列表。 I want three stack of horizontal card and when I swipe it to the left, it will showing other three stack horizontal card, just like this:我想要三叠横牌,当我向左滑动时,它会显示其他三叠横牌,就像这样:
this is my previous code:这是我以前的代码:
Padding(
padding: const EdgeInsetsDirectional.all(Dimensions.paddingSizeDefault),
child: Obx(() => ListView(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
addSemanticIndexes: false,
children: buildMainMenu(),
)));
buildMainMenu()
is the name of List<Widget> function
buildMainMenu()
是List<Widget> function
To create a swipeable stack widget use flutter_card_swiper
package ( https://pub.dev/packages/flutter_card_swiper ).要创建可滑动的堆栈小部件,请使用
flutter_card_swiper
package ( https://pub.dev/packages/flutter_card_swiper )。
There are plenty of swipeable packages: -有很多可滑动的包裹:-
https://pub.dev/packages?q=swipe+card https://pub.dev/packages?q=swipe+card
Sample Code: -示例代码:-
class Example extends StatelessWidget {
List<Container> cards = [
Container(
alignment: Alignment.center,
child: const Text('1'),
color: Colors.blue,
),
2nd Widget,
3nd Widget,
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Flexible(
child: CardSwiper(
cards: cards,
),
),
);
}
}
Complete Code: -完整代码:-
import 'package:flutter/material.dart';
import 'package:appinio_swiper/appinio_swiper.dart';
import 'package:flutter/cupertino.dart';
void main() => runApp(const Example());
class Example extends StatefulWidget {
const Example({super.key});
@override
State<Example> createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
List<Container> cards = [
Container(
alignment: Alignment.center,
color: CupertinoColors.activeBlue,
child: const Text('1'),
),
Container(
alignment: Alignment.center,
color: CupertinoColors.activeGreen,
child: const Text('2'),
),
Container(
alignment: Alignment.center,
color: CupertinoColors.activeOrange,
child: const Text('3'),
),
Container(
alignment: Alignment.center,
color: CupertinoColors.systemPink,
child: const Text('4'),
)
];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: CupertinoPageScaffold(
child: Center(
child: SizedBox(
height: 500,
width: 400,
child: AppinioSwiper(
allowUnswipe: false,
maxAngle: 2,
direction: AppinioSwiperDirection.left,
cards: cards,
),
),
),
),
),
);
}
}
Output: - Output:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.