Flutter How to create Card with background Image?

后端 未结 4 1685
青春惊慌失措
青春惊慌失措 2020-12-14 01:51

I\'m trying to create a Card with an Image as a background. The problem is, the Image overflows the Card, so the Corners of the don\'t show up.

I need to either set

相关标签:
4条回答
  • 2020-12-14 02:35

    For those of you who want to show the Card corner to draw above the image now use

    borderOnForeground: true
    
    0 讨论(0)
  • 2020-12-14 02:41
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text("Ejemplo"),
            ),
            body: Center(child: SwipeList()));
      }
    }
    
    class SwipeList extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return ListItemWidget();
      }
    }
    
    class ListItemWidget extends State<SwipeList> {
      List items = getDummyList();
    
      @override
      Widget build(BuildContext context) {
        return Container(
            child: ListView.builder(
              itemCount: items.length,
              itemBuilder: (context, index) {
                return Dismissible(
                  key: Key(items[index]),
                  background: Container(
                    alignment: AlignmentDirectional.centerEnd,
                    color: Colors.red,
                    child: Icon(
                      Icons.delete,
                      color: Colors.white,
                    ),
                  ),
                  onDismissed: (direction) {
                    setState(() {
                      items.removeAt(index);
                    });
                  },
                  direction: DismissDirection.endToStart,
                  child: Card(
                      margin: EdgeInsets.only(left: 10, right: 10, top: 12),
                      elevation: 8,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12.0),
                      ),
                      semanticContainer: true,
                      clipBehavior: Clip.antiAliasWithSaveLayer,
                      child: Container(
                        height: 135,
                        child: Stack(children: <Widget>[
                          Positioned(
                              top: 0,
                              left: 0,
                              right: 0,
                              child: Container(
                                  color: Colors.white,
                                  child: Column(
                                    children: <Widget>[
                                      Image.network(
                                        'https://placeimg.com/640/480/any',
                                        fit: BoxFit.fitHeight,
                                      ),
                                    ],
                                  ))),
                          Positioned(
                              top: 20,
                              left: 0,
                              right: 0,
                              child: Container(
                                child: Column(
                                  children: <Widget>[
                                    Text(items[index],
                                        style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            fontSize: 25,
                                            color: Colors.white),
                                        textAlign: TextAlign.center),
                                  ],
                                ),
                              )),
                        ]),
                      )),
                );
              },
            ));
      }
    
      static List getDummyList() {
        List list = List.generate(5, (i) {
          return "Item ${i + 1}";
        });
        print(list);
        return list;
      }
    }
    
    0 讨论(0)
  • 2020-12-14 02:45

    You can wrap your image in ClipRRect

    ClipRRect(
      borderRadius: BorderRadius.vertical(top: Radius.circular(10.0)),
      child: Image.network(...),
    )
    
    0 讨论(0)
  • 2020-12-14 02:46

    Other Way Without using - ClipRRect Widget - is To set semanticContainer: true, of Card Widget.

    Example Code as Below:

    Card(
              semanticContainer: true,
              clipBehavior: Clip.antiAliasWithSaveLayer,
              child: Image.network(
                'https://placeimg.com/640/480/any',
                fit: BoxFit.fill,
              ),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
              elevation: 5,
              margin: EdgeInsets.all(10),
            ),
    

    Output:

    0 讨论(0)
提交回复
热议问题