【问题标题】:flutter : How to make Card Radio Button?flutter : 如何制作卡片单选按钮?
【发布时间】:2022-01-04 08:27:21
【问题描述】:

我想制作卡片单选按钮。 我要制作卡片单选按钮。我要制作卡片单选按钮。 我要制作卡片单选按钮。我要制作卡片单选按钮。 我想制作卡片单选按钮。 我要制作卡片单选按钮。我要制作卡片单选按钮。我要制作卡片单选按钮。我要制作卡片单选按钮。我要制作卡片单选按钮。我要制作卡片单选按钮。我要制作卡片单选按钮。我想要制作卡片单选按钮。

这是我的代码

import 'package:cwc/ui/Home/homePage.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class LandingPage extends StatefulWidget {
  const LandingPage({Key? key}) : super(key: key);

  @override
  _LandingPageState createState() => _LandingPageState();
}

class _LandingPageState extends State<LandingPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: MediaQuery.of(context).size.height,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/bg.png"),
            fit: BoxFit.cover,
          ),
        ),
        width: double.infinity,
        child: SafeArea(
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: 80,
                ),
                Text(
                  'Membership Packages',
                  textAlign: TextAlign.center,
                  style: GoogleFonts.poppins(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                      color: Colors.white),

                ),
                Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Text(
                    'Choose a membership plan to unlock all the features of the CWC Wellness Resource App',
                    textAlign: TextAlign.center,
                    style: GoogleFonts.poppins(
                      fontSize: 14,
                      color: Colors.white,
                    ),
                  ),
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      height: 13,
                    ),

                    InkWell(
                      onTap: () {
                        Navigator.pushReplacement(
                          context,
                          MaterialPageRoute(
                            builder: (context) => HomePage(),
                          ),
                        );
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 12.0),
                        child: Container(
                            decoration:  const BoxDecoration(
                              // borderRadius: BorderRadius.circular(10),
                              image: DecorationImage(
                                image: AssetImage("assets/ona.png"),
                                fit: BoxFit.fill,
                              ),
                            ),
                            height: 147,
                            width: MediaQuery.of(context).size.width,
                            child: Row(
                              children: [
                                Container(
                                  width: MediaQuery.of(context).size.width / 1.75,
                                  color: Colors.transparent,
                                ),
                                Image.asset(
                                  'assets/onaa.png',
                                  height: 114,
                                  width: 114,
                                ),
                              ],
                            )),
                      ),
                    ),
                    const SizedBox(
                      height: 32,
                    ),

                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 12.0),
                      child: Container(

                          decoration: const BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage("assets/onb.png"),
                              fit: BoxFit.fill,
                            ),
                          ),
                          height: 147,
                          width: MediaQuery.of(context).size.width,
                          child: Row(
                            children: [
                              Container(
                                width: MediaQuery.of(context).size.width / 1.75,
                                color: Colors.transparent,
                              ),
                              Image.asset(
                                'assets/onbb.png',
                                height: 114,
                                width: 114,
                              ),
                            ],
                          )),
                    ),
                    const SizedBox(
                      height: 32,
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 12.0),
                      child: Container(

                        decoration: const BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage("assets/onc.png"),
                            fit: BoxFit.fill,
                          ),
                        ),
                        height: 147,
                        width: MediaQuery.of(context).size.width,
                        child: Row(
                          children: [
                            Container(
                              width: MediaQuery.of(context).size.width / 1.75,
                              color: Colors.transparent,
                            ),
                            Image.asset(
                              'assets/oncc.png',
                              height: 114,
                              width: 114,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                             ],
            ),
          ),
        ),
      ),
        bottomNavigationBar: GestureDetector(
            onTap: () {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(
                  builder: (context) => HomePage(),
                ),
              );
            },
            child: Container(
                color: Color(0xff3a99a4),
                child: Container(
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.only(topLeft: Radius.circular(60))),
                    width: double.infinity,
                    height: 57,
                    child: Center(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Padding(
                            padding: const EdgeInsets.fromLTRB(27, 0, 0, 0),
                            child: Container(
                              width: 18,
                              height: 18,
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                            child: Text(
                              'Continue',
                              style: GoogleFonts.poppins(
                                  fontSize: 14,
                                  color: Color(0xFF158998),
                                  fontWeight: FontWeight.w600),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(0, 0, 27, 0),
                            child: const Icon(
                              Icons.arrow_right_alt_outlined,
                              size: 18,
                              color: Color(0xFF158998),
                            ),
                          ),
                        ],
                      ),
                    )))));
  }
}

我创造了这个。

但我想要这样。

【问题讨论】:

  • 所以你还想通过点击卡片来选择单选按钮?
  • @Diwyansh 是的,我想在卡片上设置单选按钮。
  • 我的意思是当用户点击卡片时应该选择单选按钮?
  • @Diwyansh 不,实际上我想要那样的用户界面

标签: flutter flutter-layout flutter-dependencies flutter-animation flutter-test


【解决方案1】:

要实现这一点,您需要将 widgets 包装在 Stack 小部件中,并且还必须将 position 分配给每个,如下面的代码。只需将您的 Column 替换为:

Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      InkWell(
                        onTap: () {
                           Navigator.pushReplacement(
                             context,
                            MaterialPageRoute(
                              builder: (context) => HomePage(),
                            ),
                          );
                        },
                        child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 12.0),
                          child: SizedBox(
                            height: 147,
                            width: MediaQuery.of(context).size.width,
                            child: Stack(
                              alignment: Alignment.center,
                              children: [
                                Positioned(
                                  left: 0,
                                  right: 0,
                                  bottom: 8,
                                  top: 0,
                                  child: Container(
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(10),
                                        image: DecorationImage(
                                          image: NetworkImage(
                                              "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg"),
                                          fit: BoxFit.fill,
                                        ),
                                      ),
                                      height: 147,
                                      width: MediaQuery.of(context).size.width,
                                      child: Row(
                                        children: [
                                          Container(
                                            width: MediaQuery.of(context)
                                                    .size
                                                    .width /
                                                1.75,
                                            color: Colors.transparent,
                                          ),
                                          Image.network(
                                            "https://cdn.pixabay.com/photo/2017/07/08/09/49/red-2483933_960_720.jpg",
                                            height: 114,
                                            width: 114,
                                          ),
                                        ],
                                      )),
                                ),
                                Positioned(
                                  width: 18,
                                  bottom: 0,
                                  child: Center(
                                    child: Container(
                                      height: 15,
                                      width: 15,
                                      decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.circular(50),
                                      ),
                                      child: Radio(
                                        activeColor: Colors.green,
                                        overlayColor: MaterialStateProperty.all(
                                            Colors.red),
                                        value: 1,
                                        groupValue: 1,
                                        onChanged: (int? value) {},
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                      Divider(color: Colors.transparent,),
                      InkWell(
                        onTap: () {
                           Navigator.pushReplacement(
                             context,
                            MaterialPageRoute(
                              builder: (context) => HomePage(),
                            ),
                          );
                        },
                        child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 12.0),
                          child: SizedBox(
                            height: 147,
                            width: MediaQuery.of(context).size.width,
                            child: Stack(
                              alignment: Alignment.center,
                              children: [
                                Positioned(
                                  left: 0,
                                  right: 0,
                                  bottom: 8,
                                  top: 0,
                                  child: Container(
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(10),
                                        image: DecorationImage(
                                          image: NetworkImage(
                                              "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg"),
                                          fit: BoxFit.fill,
                                        ),
                                      ),
                                      height: 147,
                                      width: MediaQuery.of(context).size.width,
                                      child: Row(
                                        children: [
                                          Container(
                                            width: MediaQuery.of(context)
                                                .size
                                                .width /
                                                1.75,
                                            color: Colors.transparent,
                                          ),
                                          Image.network(
                                            "https://cdn.pixabay.com/photo/2017/07/08/09/49/red-2483933_960_720.jpg",
                                            height: 114,
                                            width: 114,
                                          ),
                                        ],
                                      )),
                                ),
                                Positioned(
                                  width: 18,
                                  bottom: 0,
                                  child: Center(
                                    child: Container(
                                      height: 15,
                                      width: 15,
                                      decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.circular(50),
                                      ),
                                      child: Radio(
                                        activeColor: Colors.green,
                                        overlayColor: MaterialStateProperty.all(
                                            Colors.red),
                                        value: 1,
                                        groupValue: 1,
                                        onChanged: (int? value) {},
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                      Divider(color: Colors.transparent,),
                      InkWell(
                        onTap: () {
                           Navigator.pushReplacement(
                             context,
                             MaterialPageRoute(
                               builder: (context) => HomePage(),
                             ),
                           );
                        },
                        child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 12.0),
                          child: SizedBox(
                            height: 147,
                            width: MediaQuery.of(context).size.width,
                            child: Stack(
                              alignment: Alignment.center,
                              children: [
                                Positioned(
                                  left: 0,
                                  right: 0,
                                  bottom: 8,
                                  top: 0,
                                  child: Container(
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(10),
                                        image: DecorationImage(
                                          image: NetworkImage(
                                              "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg"),
                                          fit: BoxFit.fill,
                                        ),
                                      ),
                                      height: 147,
                                      width: MediaQuery.of(context).size.width,
                                      child: Row(
                                        children: [
                                          Container(
                                            width: MediaQuery.of(context)
                                                .size
                                                .width /
                                                1.75,
                                            color: Colors.transparent,
                                          ),
                                          Image.network(
                                            "https://cdn.pixabay.com/photo/2017/07/08/09/49/red-2483933_960_720.jpg",
                                            height: 114,
                                            width: 114,
                                          ),
                                        ],
                                      )),
                                ),
                                Positioned(
                                  width: 18,
                                  bottom: 0,
                                  child: Center(
                                    child: Container(
                                      height: 15,
                                      width: 15,
                                      decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.circular(50),
                                      ),
                                      child: Radio(
                                        activeColor: Colors.green,
                                        overlayColor: MaterialStateProperty.all(
                                            Colors.red),
                                        value: 1,
                                        groupValue: 1,
                                        onChanged: (int? value) {},
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 2021-08-26
    • 2021-09-29
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    相关资源
    最近更新 更多