为单独的卡片添加不同的 onTap 页面



>我正在查看一个垂直放置不同卡片的 UI,我尝试添加 onTap 功能以为每个不同的卡片打开一个单独的页面。在此UI中,放置了带有图像,标题和稍后阅读按钮数据的垂直卡片,在clipReact上方添加手势检测器以添加onTap材料页面路由功能,并在稍后阅读按钮下方添加onTap,但是我应该将onTap放在哪里

onTap: () => Navigator.of(context)
                      .push(MaterialPageRoute(builder: (_) => /*what to define here*/)),

有没有办法为不同卡的单独页面添加单独的 onTap 功能?

class CardScrollWidget extends StatelessWidget {
  var currentPage;
  var padding = 20.0;
  var verticalInset = 20.0;
  CardScrollWidget(this.currentPage);
  @override
  Widget build(BuildContext context) {
    return new AspectRatio(
      aspectRatio: widgetAspectRatio,
      child: LayoutBuilder(builder: (context, contraints,) {
        var width = contraints.maxWidth;
        var height = contraints.maxHeight;
        var safeWidth = width - 2 * padding;
        var safeHeight = height - 2 * padding;
        var heightOfPrimaryCard = safeHeight;
        var widthOfPrimaryCard = heightOfPrimaryCard * cardAspectRatio;
        var primaryCardLeft = safeWidth - widthOfPrimaryCard;
        var horizontalInset = primaryCardLeft / 2;
        List<Widget> cardList = new List();
        for (var i = 0; i < images.length; i++) {
          var delta = i - currentPage;
          bool isOnRight = delta > 0;
          var start = padding + max(primaryCardLeft -horizontalInset * -delta * (isOnRight ? 15 : 1),
                  0.0);
          var cardItem = Positioned.directional(
            top: padding + verticalInset * max(-delta, 0.0),
            bottom: padding + verticalInset * max(-delta, 0.0),
            start: start,
            textDirection: TextDirection.rtl,
            child: GestureDetector(
            child: ClipRRect(
              borderRadius: BorderRadius.circular(16.0),
              child:Container(
                decoration: BoxDecoration(color: Colors.white, boxShadow: [
                  BoxShadow(
                      color: Colors.black12,
                      offset: Offset(3.0, 6.0),
                      blurRadius: 10.0)
                ]),
                child: AspectRatio(
                  aspectRatio: cardAspectRatio,
                  child: Stack(
                    fit: StackFit.expand,
                    children: <Widget>[
                      Image.asset(images[i], fit: BoxFit.cover),
                      Align(
                        alignment: Alignment.bottomLeft,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Padding(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 16.0, vertical: 8.0),
                              child: Text(title[i],
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 25.0,
                                      fontFamily: "SF-Pro-Text-Regular")),
                            ),
                            SizedBox(
                              height: 10.0,
                            ),
                            Padding(
                              padding: const EdgeInsets.only(
                                  left: 12.0, bottom: 12.0),
                              child: Container(
                                padding: EdgeInsets.symmetric(
                                    horizontal: 22.0, vertical: 6.0),
                                decoration: BoxDecoration(
                                    color: Colors.blueAccent,
                                    borderRadius: BorderRadius.circular(20.0)),
                                child: Text("Read Later",
                                    style: TextStyle(color: Colors.white)),),),],),)],)),),),
              onTap: () => Navigator.of(context).push(MaterialPageRoute(builder: (_) => )),
            ),);
          cardList.add(cardItem);
        }
        return Stack(
          children: cardList,
        );}),);}}

数据-

List<String> images = [
  "assets/image_04.jpg",
  "assets/image_03.jpg",
  "assets/image_02.jpg",
  "assets/image_01.png",
];
List<String> title = [
  "Hounted Ground",
  "Fallen In Love",
  "The Dreaming Moon",
  "Jack the Persian and the Black Castel",
];

您可以更改数据结构以放置目标页面并将其放入MaterialPageRoute生成器方法。

List pages = [
  {
    "image" : "assets/image_01.png",
    "title" : "Hounted Ground 1",
    "page" : YOURWIDGET(),
  },
 {
    "image" : "assets/image_02.png",
    "title" : "Hounted Ground2",
    "page" : YOURWIDGET(),
  },
 {
    "image" : "assets/image_03.png",
    "title" : "Hounted Ground 3",
    "page" : YOURWIDGET(),
  }
];
// in your cards page build method
// note that you should place below code in a multi widget support widget like Column(), ListView(), ...
Column(
  children: pages.map((page) {
    return Card(
       // card content and attributes
       onTap: () {
         Navigator.of(context).push(MaterialPageRoute(
            builder: (BuildContext ctx) {
                return page['page'];
            }
         ));
       }
    );
  }).toList()
);
  onTap: () => Navigator.of(context).push(MaterialPageRoute(
                  builder: (_) => (i == 0)
                      ? pageOne()
                      : (i == 1)
                          ? pageTwo()
                          : (i == 2)
                              ? pageThree()
                              : pageFour())),

相关内容

  • 没有找到相关文章

最新更新