Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter How to get images in Positioned from Firestore?

I already geting background image, title data from Firestore but i couldn't get image in Positioned. This is what i want: enter image description here

I already have this final Query query = FirebaseFirestore.instance.collection("1doga"); Getting cards background images like this:

decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(9.6),
                            image: DecorationImage(
                              fit: BoxFit.cover,
                              image: CachedNetworkImageProvider(
                                  querySnapshot.docs[index].data()['image'],
                                  maxHeight: 200,
                                  maxWidth: 200),
                            ),
                          ),

Here is the point, i can't add image from Firestore:

Positioned(
                                bottom: 19.2,
                                left: 19.2,
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(4.8),
                                  child: BackdropFilter(
                                    filter: ImageFilter.blur(
                                        sigmaY: 19.2, sigmaX: 19.2),
                                    child: Container(
                                      height: 26,
                                      padding: EdgeInsets.only(
                                          left: 0.0, right: 0.0),
                                      alignment: Alignment.centerLeft,
                                      child: Row(
                                        children: <Widget>[
                                          SizedBox(
                                            width: 9.52,
                                          ),
                                          SvgPicture.asset(
                                              'assets/svg/icon_location.svg'),
                                          SizedBox(
                                            width: 9.52,
                                          ),
                                          SvgPicture.asset(
                                            'assets/svg/icon_location.svg',
                                            height: 50,
                                          ),
                                          SizedBox(
                                            width: 9.52,
                                          ),
                                          SvgPicture.asset(
                                              'assets/svg/icon_location.svg'),
                                          SizedBox(
                                            width: 9.52,
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ),

I need to change "SvgPicture.asset( 'assets/svg/icon_location.svg')" this and add images from Firestore but i don't know how. Full of my codes:

Widget getImage(String urlFirebase, int? size) {
          return Column(
            children: [
              CachedNetworkImage(                        
                height: 40,
                width: 40,
                fit: BoxFit.cover,
                imageUrl: urlFirebase,
                placeholder: (context, url) => size != null?
                  SvgPicture.asset(
                    'assets/svg/icon_location.svg',
                      height: 60,
                      width: 60,
                  )
                  : SvgPicture.asset(
                    'assets/svg/icon_location.svg',
                      height: 60,
                      width: 60,                                               
                  ),
                    errorWidget: (context, url, error) => Center(
                    child: Image.asset(
                    'assets/images/img.png',
                      height: 60,
                      width: 60,
                    ),
                  ),
                ),                
                    const SizedBox(width: 9.52),
                   ]
              );
}

  /// Page Controller

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {
            Navigator.pop(context);
          },
          icon: const Icon(Icons.arrow_back_ios_new_sharp),
        ),
        title: SizedBox(
          width: 170,
          child: Image.network(
            'https://i.hizliresim.com/sf4kbvu.jpg',
          ),
        ),
        centerTitle: true,
        actions: [
          IconButton(
            onPressed: () {},
            icon: const Icon(Icons.cloud_queue_outlined),
          ),
        ],
      ),
      drawer: NavDrawer(),
      body: SafeArea(        
          child: StreamBuilder(
            stream: query.snapshots(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return const Center(
                  child: CircularProgressIndicator(),
                );
              } else if (snapshot.hasError) {
                return const Center(
                  child: Icon(Icons.error),
                );
              }
              final QuerySnapshot<Map<String, dynamic>> querySnapshot =
snapshot.data as QuerySnapshot<Map<String, dynamic>> ;
              return ListView(
                physics: const BouncingScrollPhysics(),
                children: <Widget>[
                  Container(
                    margin: const EdgeInsets.only(top: 28.8, bottom: 16.8),
                    height: 724.8,
                    child: ListView.builder(
                      itemCount: querySnapshot.size,
                      padding: const EdgeInsets.only(left: 28.8, right: 12),
                      scrollDirection: Axis.vertical,
                      physics: const BouncingScrollPhysics(),
                      itemBuilder: (context, index) {
                        return Container(
                          height: 214.8,
                          width: 188.4,
                          margin: const EdgeInsets.only(right: 16.8, bottom: 50),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(9.6),
                            image: DecorationImage(
                              fit: BoxFit.cover,
                              image: CachedNetworkImageProvider(
                                  querySnapshot.docs[index].data()['image'],
                                  maxHeight: 200,
                                  maxWidth: 200),
                            ),
                          ),
                          child: Stack(
                            children: <Widget>[
                              GestureDetector(
                                  onTap: () => gotoPage(querySnapshot
                                      .docs[index]
                                      .data()['title'])),
                              /*  Navigator.push(
                                      context,
                                      CupertinoPageRoute(
                                          builder: (redContext) => MyApp()));*/

                                           Positioned(
                                bottom: 19.2,
                                left: 19.2,
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(4.8),
                                  child: BackdropFilter(
                                    filter: ImageFilter.blur(
                                        sigmaY: 19.2, sigmaX: 19.2),
                                    child: Container(
                                      height: 26,
                                      padding: const EdgeInsets.only(
                                          left: 0.0, right: 0.0),
                                      alignment: Alignment.centerLeft,
                                      child: Row(
                                        children: <Widget>[
                                          getImage(firebaseUrl),
                                          getImage(firebaseUrl, 50),
                                          getImage(firebaseUrl, 50),                                          
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ),                           
                              Align(
                                alignment: Alignment.center,
                                child: Container(
                                  margin: const EdgeInsets.all(20),
                                  child: Text(
                                    querySnapshot.docs[index].data()['tr'],
                                    style: const TextStyle(
                                        fontSize: 35,
                                        color: Colors.white,
                                        fontWeight: FontWeight.bold),
                                    textAlign: TextAlign.center,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                      },
                    ),
                  )
                ],
              );
            },
          ),        
      ),
    );
  }
}
like image 545
Berkan Avatar asked Dec 09 '21 10:12

Berkan


People also ask

How do I fetch images from Firebase in flutter?

Go to your project dashboard on Firebase Console, select “Storage” from the left-hand menu then you can see the bucket path as shown in the screenshot below: You don't have to manually add the path to your Flutter code because it will be handled automatically by Firebase packages.

How do I display data from firestore in flutter?

There should be array called contacts and inside that there should be 3 maps according to your data. Then create a list in your screen class. List contacts; Then create a function to retrieve data from firestore.

How do I get the image from Firebase Storage in flutter Web?

Make use of getDownloadURL() to get the URL of the image stored in Firebase storage and pass in the image file name. (it's “Flutter. png” in our example.) This is how we read image from Firebase storage and display them in our Flutter Web application.

How to add images to Firebase Storage in flutter?

So, since we are going to use images inside the assets folder and add them to Firebase Storage, therefore first we can create a folder called images under the assets folder and inside the images folder we can add the images: Then to access them inside the Flutter application, we need to declare them inside the pubspec.yaml:

What is stack and positioned widget in flutter?

We will also implement a demo of the stack and positioned widget, describes its properties, and how to use them in your flutter applications. So let’s get started. The stack is a widget in Flutter. It contains a list of widgets and places them on top of each other. And it places their children on top of each other like a stack of books.

How to access all images inside a flutter application?

Then to access them inside the Flutter application, we need to declare them inside the pubspec.yaml: Now, you can access all images inside the assets/images/ in your application. To display those images inside the Flutter application, we can use a gridView widget.

What is the difference between futurebuilder and statefulwidget in Firebase?

I’ll be using StatefulWidget to load image since I'll be toggling between two images available in my FirebaseStorage bucket. FutureBuilder widget is used to fetch the image from the Storage, and display the image depending on the connectionState and successful image retrieval.


2 Answers

Using the response of Elvis Salabarria, you can do something like that in your code

         Positioned(
                                bottom: 19.2,
                                left: 19.2,
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(4.8),
                                  child: BackdropFilter(
                                    filter: ImageFilter.blur(
                                        sigmaY: 19.2, sigmaX: 19.2),
                                    child: Container(
                                      height: 26,
                                      padding: EdgeInsets.only(
                                          left: 0.0, right: 0.0),
                                      alignment: Alignment.centerLeft,
                                      child: Row(
                                        children: <Widget>[
                                          getImage(firebaseUrl),
                                          getImage(firebaseUrl, 50),
                                          getImage(firebaseUrl, 50),                                          
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ),

// Method to manage the change

Widget getImage(String urlFirebase, int? size) {
            return Column(
               children: [
                 CachedNetworkImage(                        
                        height: 40,
                        width: 40,
                        fit: BoxFit.cover,
                        imageUrl: urlFirebase,
                        placeholder: (context, url) => size != null?
                            SvgPicture.asset(
                                              'assets/svg/icon_location.svg',
                                               height: size,
                       )
                       : SvgPicture.asset(
                                              'assets/svg/icon_location.svg',                                               
                       )
,
                        errorWidget: (context, url, error) => Center(
                          child: Image.asset(
                            'assets/images/img.png',
                            height: 60,
                            width: 60,
                          ),
                        ),
                      )),
                     SizedBox(width: 9.52),

                   ]
              )

}
like image 99
Alejandro Campos Palacios Avatar answered Oct 18 '22 03:10

Alejandro Campos Palacios


use this plugin to work with images provided by the network cached_network_image: ^3.0.0

https://pub.dev/packages/cached_network_image

CachedNetworkImage(
                        
                        height: 40,
                        width: 40,
                        fit: BoxFit.cover,
                        imageUrl: imageNetwork,
                        placeholder: (context, url) =>
                            CircularProgressIndicator(
                          valueColor:
                              AlwaysStoppedAnimation<Color>(Colors.white),
                          backgroundColor: const Color(0xFF02204c),
                        ),
                        errorWidget: (context, url, error) => Center(
                          child: Image.asset(
                            'assets/images/img.png',
                            height: 60,
                            width: 60,
                          ),
                        ),
                      )),
like image 44
Elvis Salabarria Aquino Avatar answered Oct 18 '22 04:10

Elvis Salabarria Aquino