广播按钮文本对齐左右颤动

发布于 2025-01-27 05:19:05 字数 3021 浏览 1 评论 0原文

我想在VS代码中使用Flutter创建一个广播按钮。我已经完成了功能功能的广播按钮,但是我的项目的要求是将单个按钮文本与左侧对齐。但是我不能那样做。在这里,我附上了示例源代码。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      // Hide the debug banner
      debugShowCheckedModeBanner: false,
      title: '',
      home: HomePage(),
    );
  }
}

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

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

class _HomePageState extends State<HomePage> {
  // The inital group value
  String _selectedGender = 'male';
  String text = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text(
          '',
        ),
      ),
      body: Padding(
          padding: const EdgeInsets.all(25),
          child: Container(
            margin: const EdgeInsets.all(4),
            padding: const EdgeInsets.all(4),
            decoration: BoxDecoration(
                color: const Color(0xffffffff),
                border: Border.all(color: const Color(0xffbcbcbc))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ListTile(
                  leading: Radio(
                    fillColor: MaterialStateColor.resolveWith(
                        (states) => Color(0xfff29257)),
                    value: 'Male',
                    groupValue: _selectedGender,
                    onChanged: (value) {
                      setState(() {
                        _selectedGender = value!.toString();
                      });
                    },
                  ),
                  title: const Text('Male'),
                  textColor: Colors.grey,
                  focusColor: MaterialStateColor.resolveWith(
                      (states) => Color(0xfff29257)),
                ),
                ListTile(
                  leading: Radio(
                    fillColor: MaterialStateColor.resolveWith(
                        (states) => Color(0xfff29257)),
                    value: 'female',
                    groupValue: _selectedGender,
                    onChanged: (value) {
                      setState(() {
                        _selectedGender = value!.toString();
                      });
                    },
                  ),
                  title: const Text('Female'),
                  textColor: Colors.grey,
                  //fillColor: MaterialStateColor.resolveWith((states) => Colors.green),
                  focusColor: MaterialStateColor.resolveWith(
                      (states) => Color(0xfff29257)),
                ),
                
              ],
            ),
          )),
    );
  }
}

有没有办法自定义这些无线电按钮?

I want to create a Radio Button using Flutter in VS Code. I've done the radio button with functionality but the requirement of my project is to align the radio button text to the left side. But I can't to do that. Here I attached my sample source code.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      // Hide the debug banner
      debugShowCheckedModeBanner: false,
      title: '',
      home: HomePage(),
    );
  }
}

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

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

class _HomePageState extends State<HomePage> {
  // The inital group value
  String _selectedGender = 'male';
  String text = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text(
          '',
        ),
      ),
      body: Padding(
          padding: const EdgeInsets.all(25),
          child: Container(
            margin: const EdgeInsets.all(4),
            padding: const EdgeInsets.all(4),
            decoration: BoxDecoration(
                color: const Color(0xffffffff),
                border: Border.all(color: const Color(0xffbcbcbc))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ListTile(
                  leading: Radio(
                    fillColor: MaterialStateColor.resolveWith(
                        (states) => Color(0xfff29257)),
                    value: 'Male',
                    groupValue: _selectedGender,
                    onChanged: (value) {
                      setState(() {
                        _selectedGender = value!.toString();
                      });
                    },
                  ),
                  title: const Text('Male'),
                  textColor: Colors.grey,
                  focusColor: MaterialStateColor.resolveWith(
                      (states) => Color(0xfff29257)),
                ),
                ListTile(
                  leading: Radio(
                    fillColor: MaterialStateColor.resolveWith(
                        (states) => Color(0xfff29257)),
                    value: 'female',
                    groupValue: _selectedGender,
                    onChanged: (value) {
                      setState(() {
                        _selectedGender = value!.toString();
                      });
                    },
                  ),
                  title: const Text('Female'),
                  textColor: Colors.grey,
                  //fillColor: MaterialStateColor.resolveWith((states) => Colors.green),
                  focusColor: MaterialStateColor.resolveWith(
                      (states) => Color(0xfff29257)),
                ),
                
              ],
            ),
          )),
    );
  }
}

Is there a way to customize those radio buttons?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨光如昨 2025-02-03 05:19:07

对于左文本对齐,您可以使用放射性属性属性

controlAffinity: ListTileControlAffinity.trailing,

For left text alignment you can use RadioListTile property

controlAffinity: ListTileControlAffinity.trailing,
鱼窥荷 2025-02-03 05:19:06

一种简单的方法是使用 radiolisttile ListTilecontrolaffinity.Trailing。这会将文本移至左侧,并将单个按钮移至右侧:

           RadioListTile(
              value: true,
              groupValue: groupVal,
              onChanged: (val) {},
              controlAffinity: ListTileControlAffinity.trailing,
            )

A simpler approach would be to use the RadioListTile widget and set controlAffinity = ListTileControlAffinity.trailing. This will move the text to the left and the radio button to the right:

           RadioListTile(
              value: true,
              groupValue: groupVal,
              onChanged: (val) {},
              controlAffinity: ListTileControlAffinity.trailing,
            )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文