提问者:小点点

嗨,如何添加将我发送到主页的按钮


类_BottomPart扩展StatelessWidget{const_BottomPart({Key? key}):super(key:key);

@overwrite Widget build(BuildContext context){返回对齐(对齐方式:对齐. backomCenter,子:填充(填充:const EdgeInsets.symrics(水平:40.0),子:列(

      mainAxisSize: MainAxisSize.min,
      children: [
        const Text(
          'Find The Best Coffee for You',
          style: TextStyle(
              fontSize: 27.0,
              fontWeight: FontWeight.bold,
              color: Colors.white),
        ),
        const SizedBox(height: 30.0),
        Text(
          'Lorem ipsum dolor sit amet, adipiscing elit. '
              'Nullam pulvinar dolor sed enim eleifend efficitur.',
          style: TextStyle(
            fontSize: 15.0,
            color: Colors.white.withOpacity(0.8),
            height: 1.5,
          ),
        ),
        const SizedBox(height: 50.0),
        Align(
          alignment: Alignment.centerRight,
          child: Container(

            height: 85.0,
            width: 85.0,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              border: Border.all(color: Colors.white, width: 2.0),
            ),
            child: const Icon(
              Icons.chevron_right,
              size: 50.0,
              color: Colors.white,
            ),
          ),
        ),
        const SizedBox(height: 50.0),

      ],
    ),
  ),
);

} }


共2个答案

匿名用户

最简单的方法是(假设你的家是一个以/为名称的命名路由):

Navigator.popUntil(context, ModalRoute.withName('/'));

匿名用户

下面是我如何回到主页并清空堆栈的示例。它在断开连接时也很有用。

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return TextButton(
      child: const Text('Log out'), // Go to the HomePage 
      onPressed:(){
        // if you use named routes
        Navigator.of(context).pushAndRemoveUntil<void>(
            "/route/to/homepage", (route) => false,
        );

        // if you don't use named routes
        Navigator.of(context).popUntil((route) => route.isFirst);
      }
    );
  }
}