简体   繁体   中英

How to add a colored bottom border on a rounded corner Container in flutter?

I'm trying to create a rounded cornered Container with a colored bottom border(One side).

I tried applying border radius and borderSide color to them but I seem to seem to get an error, and the widget fails to render.

Container(
  margin: EdgeInsets.only(
    top:15.0
  ),
  decoration: BoxDecoration(
    color: Colors.white,
    borderRadius: BorderRadius.all(
      Radius.circular(3)
    ),
    border: Border(
      bottom: BorderSide(color: Color.fromRGBO(0, 83, 79, 1),
      width: 3.0
      ))
  )...

I get this Error Message: A border-radius can only be given for uniform borders. This is what I'm trying to achieve

I think you need to use ClipPath like:

ClipPath(
    clipper: ShapeBorderClipper(
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(10))
        )
    ),
    child: Container(
        height: 70.0,
        width: 200.0,
        decoration: BoxDecoration(
            color: Colors.orange,
            border: Border(
                bottom: BorderSide(
                    color: Color.fromRGBO(0, 83, 79, 1),
                    width: 7.0
                )
            )
        )
    )
)

Output for reference:

在此处查看输出

Use InkWell widget with BoxDecoration

  import 'package:flutter/material.dart';

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

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
        title: 'Flutter Demo',
        home: HomePage(),
        );
    }
    }

    class HomePage extends StatefulWidget {
    @override
    _HomePageState createState() => _HomePageState();
    }

    class _HomePageState extends State<HomePage> {
    @override
    Widget build(BuildContext context) {
        return Scaffold(
        appBar: AppBar(),
        body: Center(
            child: Padding(
            padding: EdgeInsets.all(18.0),
            child: Container(
            padding: EdgeInsets.only(
                top: 18.0,
            ),
            margin: EdgeInsets.only(top: 13.0, right: 8.0),
            decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.circular(16.0),
                boxShadow: <BoxShadow>[
                    BoxShadow(
                    color: Colors.black26,
                    blurRadius: 0.0,
                    offset: Offset(0.0, 0.0),
                    ),
                ]),
            child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                SizedBox(
                    height: 20.0,
                ),
                Center(
                    child: Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: new Text("Your Text",
                        style: TextStyle(fontSize: 30.0, color: Colors.black)),
                )),
                SizedBox(height: 24.0),
                InkWell(
                    child: Container(
                    padding: EdgeInsets.only(top: 4.0, bottom: 4.0),
                    decoration: BoxDecoration(
                        color: Colors.green,
                        borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(16.0),
                            bottomRight: Radius.circular(16.0)),
                    ),
                    ),
                    onTap: () {
                    Navigator.pop(context);
                    },
                )
                ],
            ),
            ),
        )),
        );
    }
    }

在此处输入图像描述

Border bottom only with border-radius only can be achieved with ClipPath

Example on DartPad

在此处输入图像描述

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Padding(
          padding: EdgeInsets.all(18.0),
          child: ClipPath(
            clipper: ShapeBorderClipper(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(10)))),
            child: Container(
              height: 70.0,
              width: 200.0,
              decoration: BoxDecoration(
                color: Colors.blueAccent,
                border: Border(
                  bottom: BorderSide(
                      color: Color.fromRGBO(0, 83, 79, 1), width: 7.0),
                ),
              ),
              child: Center(
                    child: Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: new Text("Your Text",
                        style: TextStyle(fontSize: 30.0, color: Colors.black)),
                )),
            ),
          ),
        ),
      ),
    );
  }
}

In case anybody else needs to add rounded box with differently colored sides.

There is a package that was created as a result of flutter issue from 2017 that implements a way to do that: https://pub.dev/packages/custom_rounded_rectangle_border

Check out the readme for usage instructions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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