Home > OS >  Flutter: Difficulties in Refactor - extract
Flutter: Difficulties in Refactor - extract

Time:03-31

What I'm creating is an avatar. I'm using Android Studio and I did this Widget extraction, how can I put "Widget image" in "backgroundImage", so I can call the image here "_buildUserAvatar(image: Image.asset('image'), name: 'name '),"?

Widget _buildUserAvatar({required Widget image, required String name}) {
return GestureDetector(
  onTap: () {},
  child: Padding(
    padding: const EdgeInsets.only(left: 16, top: 10.0, right: 0.0, bottom: 5.0),
    child: Column(
      children: [
        SizedBox(
          child: CircleAvatar(
            radius: 35.0,
            backgroundColor: Colors.white,
            child: CircleAvatar(
              child: Align(
                alignment: Alignment.bottomRight,
                child: CircleAvatar(
                  backgroundColor: Colors.white,
                  radius: 15.0,
                  child: Icon(
                    Icons.camera_alt,
                    size: 15.0,
                    color: Color(0xFF404040),
                  ),
                ),
              ),
              radius: 35.0,
              backgroundImage: AssetImage(image,), <<==
            ),
          ),
        ),
        Padding(
          padding: EdgeInsets.all(8.0),
          child: Text(name, style: TextStyle(fontSize: 14),),
        ),
      ],
    ),
  ),
);

}

CodePudding user response:

You can just take in the AssetImage and pass it to backgroundImage.

Widget _buildUserAvatar({required AssetImage image, required String name})

and then pass it directly:

backgroundImage: image,

Also, just a quick note, avoid using functions that build widgets, as they carry out unnecessary rebuilds. This could be made into a stateless widget.

  • Related