android - 如何在 flutter 上制作按钮

标签 android flutter dart

你好,我有一些问题,我需要在我的 flutter 代码上制作按钮 Like this , 但是当我尝试 flutter 时,它变得像 This , 有没有办法让它像第一张图片一样?

这是我的按钮代码

Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                SizedBox(
                  width: 130,
                  height: 50,
                  child: TextButton(
                    style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Colors.blue)),
                    onPressed: () {},
                    child: Text(
                      'Login',
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ),
                Expanded(
                    child: SizedBox(
                  width: 130,
                  height: 50,
                  
                  child: TextButton(
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Colors.white),
                            
                      ),
                      onPressed: () {},
                      child: Text(
                        'Daftar',
                        style: TextStyle(color: Colors.blue),
                      ),),
                ),
                ),
              ],
            ),

最佳答案

enter image description here

var selectedButton = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          margin: EdgeInsets.all(16),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(16),
              shape: BoxShape.rectangle,
              border: Border.all(color: Colors.blue, width: 3)),
          child: Row(
            children: [
              Expanded(
                  child: Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8),
                        shape: BoxShape.rectangle,
                        color: selectedButton == 0 ? Colors.blue : Colors.white),
                    child: TextButton(
                      onPressed: () {
                        setState(() {
                          selectedButton = 0;
                        });
                      },
                      style: TextButton.styleFrom(
                        primary: selectedButton == 0 ? Colors.white : Colors.blue,
                      ),
                      child: Text(
                        'TextButton',
                        style: TextStyle(fontSize: 16),
                      ),
                    ),
                  )),
              Expanded(
                  child: Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8),
                        shape: BoxShape.rectangle,
                        color: selectedButton == 1 ? Colors.blue : Colors.white),
                    child: TextButton(
                      onPressed: () {
                        setState(() {
                          selectedButton = 1;
                        });
                      },
                      style: TextButton.styleFrom(
                        primary: selectedButton == 1 ? Colors.white : Colors.blue,
                      ),
                      child: Text(
                        'TextButton',
                        style: TextStyle(fontSize: 16),
                      ),
                    ),
                  )),
            ],
          ),
        ),
      ),
    );
  }

关于android - 如何在 flutter 上制作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69251550/

相关文章:

flutter - 取消不必要的声明以调用流取消

android - 如何在撰写中使用 BottomSheetScaffold 为 bottomsheet 设置半展开高度?

android - 运行 "flutter run"时出现 Flutter Stripe 错误

ios - DTDeviceKit : deviceType Null

Flutter Go_router,保存路由历史

sqlite - Dart-使用泛型将List <SuperType>转换为List <SubType>

Flutter:DropdownButton不起作用,我加载了数据但它没有出现在界面中

android - Soloud + OpenSLES 后端,非 Activity 音轨

android - 具有正延迟的计时器 IllegalArgumentException

android - NoSuchMethodError : No static method zzb 错误