Flutter How to create Card with background Image?

后端 未结 4 1694
青春惊慌失措
青春惊慌失措 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: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 createState() {
        return ListItemWidget();
      }
    }
    
    class ListItemWidget extends State {
      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: [
                          Positioned(
                              top: 0,
                              left: 0,
                              right: 0,
                              child: Container(
                                  color: Colors.white,
                                  child: Column(
                                    children: [
                                      Image.network(
                                        'https://placeimg.com/640/480/any',
                                        fit: BoxFit.fitHeight,
                                      ),
                                    ],
                                  ))),
                          Positioned(
                              top: 20,
                              left: 0,
                              right: 0,
                              child: Container(
                                child: Column(
                                  children: [
                                    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;
      }
    }
    

提交回复
热议问题