Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

dart - Flutter: How can I add onPressed commands when I use card widgets?

I want to direct people who touch the card to a different page. I can't use onPressed commands. For eg. when I tap the "to do card", i want to navigate different page.

               SizedBox(
                  width:160.0,
                  height: 150.0,
                  child: Card(

                    color: Color.fromARGB(255,21, 21,21),
                    elevation: 2.0,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0)
                    ),
                    child:Center(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                        children: <Widget>[
                          Image.asset("assets/images/todo.png",width: 64.0,),
                          
                          SizedBox(
                            height: 10.0,
                          ),
                          Text(
                            "Todo List",
                            style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 20.0
                            ),
                          ),
                        
                        ],
                        ),
                      )
                    ),
                  ),
                ),
question from:https://stackoverflow.com/questions/66054219/flutter-how-can-i-add-onpressed-commands-when-i-use-card-widgets

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An alternative to the GestureDetector is to use an InkWell if you want some splash effect where you pressed.

Something like this:

              SizedBox(
                width: 160.0,
                height: 150.0,
                child: InkWell(
                  onTap: () => Navigator.pushNamed(context, '/secondPage'),
                  child: Card(
                      //
                      // All your card information
                      //
                  ),
                ),
              ),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...