4

I am using the TextField widget, and I want to hide the left side border, as shown here:

pic

TextField(
  decoration: new InputDecoration(
      border: new OutlineInputBorder(
          borderSide: const BorderSide(width: 2.0, style: BorderStyle.solid),
          borderRadius: BorderRadius.circular(50.0)),
      focusedBorder: OutlineInputBorder(
        borderSide: const BorderSide(color: Colors.grey, width: 2.0),
        borderRadius: BorderRadius.circular(50.0),
      ),
      hintText: 'User Name',
      hintStyle: new TextStyle(color: Colors.grey, fontWeight: FontWeight.bold),
      suffixIcon: const Icon(Icons.person, size: 30.0, color: Colors.grey),
      errorText: snapshot.error),
);

Thanks in advance!

Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
Sangeet
  • 145
  • 3
  • 9

3 Answers3

3

You can change your BoxDecoration

decoration: BoxDecoration(
   border: Border(
    left: BorderSide(width: 16.0, color: Colors.transparent),
    top: BorderSide(width: 16.0, color: Colors.lightBlue.shade900),
    right: BorderSide(width: 16.0, color: Colors.lightBlue.shade900),
    bottom: BorderSide(width: 16.0, color: Colors.lightBlue.shade900),
),
Ash Khachatryan
  • 427
  • 1
  • 5
  • 18
  • 4
    It is not working as BoxDecoration is applied to Container, not to Textfield. – Sangeet Dec 17 '20 at 14:30
  • one bracket was missing at the end, i added and it works for me decoration: BoxDecoration( border: Border( left: BorderSide(width: 16.0, color: Colors.transparent), top: BorderSide(width: 16.0, color: Colors.lightBlue.shade900), right: BorderSide(width: 16.0, color: Colors.lightBlue.shade900), bottom: BorderSide(width: 16.0, color: Colors.lightBlue.shade900), ),) – eko Mar 14 '21 at 10:01
3

As of 2022 I'd like to add a solution using package assorted_layout_widgets:

NonUniformOutlineInputBorder

Can be used to style text-fields and containers.

Similar to Flutter's native OutlineInputBorder, but you can hide some of the sides, by setting hideTopSide, hideBottomSide, hideRightSide and hideLeftSide to true.

Usage for text-fields:

TextField(
   decoration: InputDecoration(
      enabledBorder: NonUniformOutlineInputBorder(hideLeftSide: true, ...),
      ...

Usage for containers:

Container(
   decoration: ShapeDecoration(
      shape: NonUniformOutlineInputBorder(
         hideLeftSide: true,
         borderSide: BorderSide(width: 10),
         borderRadius: BorderRadius.only(
            topRight: Radius.circular(15),
            bottomRight: Radius.circular(35),
         ),          
   ...

NonUniformRoundedRectangleBorder

Can be used to style buttons and containers.

Similar to Flutter's native RoundedRectangleBorder but you can hide some of the sides, by setting hideTopSide, hideBottomSide, hideRightSide and hideLeftSide to true.

Usage for buttons:

ElevatedButton(
   style: ElevatedButton.styleFrom(
      shape: NonUniformRoundedRectangleBorder(
         hideLeftSide: true,
         side: BorderSide(color: Colors.black87, width: 15.0),
         borderRadius: BorderRadius.only(
            topRight: Radius.circular(15),
            bottomRight: Radius.circular(35),
         ), 
      ...

Usage for containers:

Container(
   decoration: ShapeDecoration(
      shape: NonUniformRoundedRectangleBorder(...)),
   ...

Note: I'm the author of that package.

Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
2

borderRadius can be specified only for uniform borders, that is, borders that have the same width and color for each side.

You can achieve a similar effect, by wrapping the TextField into a Container and making use of the BoxShadow property:

example1

Follows a full snippet of the screenshotted example:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: 200,
          decoration: BoxDecoration(
            //borderRadius: BorderRadius.circular(10),
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                offset: Offset(2, 0),
                color: Colors.grey,
                spreadRadius: 0,
                blurRadius: 2,
              ),
            ],
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(20),
              bottomRight: Radius.circular(20),
            ),
          ),
          child: TextField(
            textAlign: TextAlign.center,
            decoration: new InputDecoration(
              border: InputBorder.none,
              hintText: 'User Name',
              hintStyle: new TextStyle(
                  color: Colors.grey, fontWeight: FontWeight.bold),
              suffixIcon: const Icon(
                Icons.person,
                size: 30.0,
                color: Colors.grey,
              ),
            ),
          ),
        ),
      ),
    );
    //
  }
}

A second, hackier, work-around, would be to use a Stack and a Container positioned to the far left to hide the left border. Although, in this case, it might be difficult to use a Colors.transparent background. example2

Follows the full snippet:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: 200,
          height: 50,
          child: Stack(
            overflow: Overflow.visible,
            children: [
              TextField(
                textAlign: TextAlign.center,
                decoration: new InputDecoration(
                  border: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey, width: 1),
                    borderRadius: BorderRadius.circular(20),
                  ),
                  hintText: 'User Name',
                  hintStyle: new TextStyle(
                      color: Colors.grey, fontWeight: FontWeight.bold),
                  suffixIcon: const Icon(
                    Icons.person,
                    size: 30.0,
                    color: Colors.grey,
                  ),
                ),
              ),
              Positioned(
                left: 0,
                bottom: 0,
                child: Container(
                  width: 20,
                  height: 50,
                  color: Theme.of(context).scaffoldBackgroundColor,
                ),
              ),
            ],
          ),
        ),
      ),
    );
    //
  }
}

Stefano Amorelli
  • 4,553
  • 3
  • 14
  • 30
  • It is working but again if there's an error message it is appearing inside the container which shouldn't happen. Can't we do changes in the Textfield? – Sangeet Dec 17 '20 at 14:47
  • I've modified the answer supporting the `TextField` border, example number #2! – Stefano Amorelli Dec 17 '20 at 14:54