简体   繁体   English

如何在 flutter 中创建自定义边框

[英]how to create a custom border in flutter

I'm trying to create a custom border with a dynamic size and a gradient color on it.我正在尝试创建一个具有动态大小和渐变颜色的自定义边框。

How can I create a border with the same width as the text above?如何创建与上面文本宽度相同的边框?

@override
Widget build(BuildContext context) {
var selectedItemIndex = 0;

return SingleChildScrollView(
  scrollDirection: Axis.horizontal,
  child: Wrap(
    spacing: 32,
    children: list.asMap().entries.map((entry) {
      var isSelectedItem = (entry.key == selectedItemIndex);
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            entry.value,
            style: TextStyle(
              fontSize: 16,
              color:
                  entry.key == selectedItemIndex ? Colors.white : lightGrey,
            ),
          ),
          const SizedBox(
            height: 4,
          ),
          Flex(
            direction: Axis.horizontal,
            children: [
              Container(
                height: isSelectedItem ? 3 : 2,
                width: 40,  // <- fixed size
                decoration: const BoxDecoration(
                  gradient: purpleGradient,
                ),
              ),
            ],
          ),
        ],
      );
    }).toList(),
  ),
);

} }

在此处输入图像描述

ps.: I tried Flex , Flexible , and double.infinity to expand the border's width. ps.: 我尝试了FlexFlexibledouble.infinity来扩展边框的宽度。

See https://medium.com/vijay-r/neon-light-effect-flutter-23a36c341fe7 will help you, For dynamic size Try setting mainAxisSize: MainAxisSize.min on the column and adding some spacing.请参阅https://medium.com/vijay-r/neon-light-effect-flutter-23a36c341fe7会对您有所帮助,对于动态大小尝试在列上设置 mainAxisSize: MainAxisSize.min 并添加一些间距。

Try this:试试这个:

custom button:自定义按钮:

class UnderlineButton extends StatelessWidget {
  final String label;
  final Function onTap;

  const UnderlineButton({
    Key? key,
    required this.label,
    required this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextButton(
      style: ButtonStyle(
        shape: MaterialStateProperty.all<RoundedRectangleBorder>(
          RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)),
        ),
        overlayColor: MaterialStateProperty.all<Color?>(Colors.transparent),
      ),
      onPressed: onTap,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(label),
          Container(
            height: 7,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.bottomLeft,
                end: Alignment.bottomRight,
                colors: [
                  Colors.blue,
                  Colors.indigo,
                ],
                tileMode: TileMode.repeated,
              ),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(label, style: const TextStyle(color: Colors.transparent),),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

use custom button in screen:在屏幕中使用自定义按钮:

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Row(
          children: [
            UnderlineButton(
              label: 'Recent',
              onTap: () {},
            ),
            UnderlineButton(
              label: 'Top 50',
              onTap: () {},
            ),
            UnderlineButton(
              label: 'Festival',
              onTap: () {},
            ),
          ],
        ),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM