添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
title: 'Awesome Border', theme: ThemeData(primarySwatch: Colors.blue), home: MyHomePage(title: 'My Awesome Border'), class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); class _MyHomePageState extends State { // 即将渲染的边框组件集合 List _borderWidgets; @override void initState() { // 处理带边框的对象集合 _borderWidgets = []; super.initState(); @override Widget build(BuildContext context) { // 为了美观, 将内容全部居中处理 _borderWidgets.forEach((element) { _borderWidgets[_borderWidgets.indexOf(element)] = Center(child: element); return Scaffold( appBar: AppBar(title: Text(widget.title)), body: Center( child: Container( padding: EdgeInsets.all(20), child: GridView.count( padding: const EdgeInsets.all(8.0), crossAxisCount: 3, crossAxisSpacing: 5, mainAxisSpacing: 5, children: _borderWidgets,

我们首先在 26 行定义了实例变量 _borderWidgets , 它将保存我们所有的用来展示边框的Widget.

然后定义 initState() , 用于初始化 _borderWidgets .

在下方的build内部为其套上一层 Center 实现居中效果.

最后就是使用 GridView 进行渲染了.

💡 代码解析

🟢 运行项目. 应该是空白的, 啥也木有:

padding: EdgeInsets.all(10), decoration: BoxDecoration( border: Border.all(width: 5, color: Colors.purple), child: Text( "hello", style: TextStyle(fontSize: 35), // 黄色边框 Container( padding: EdgeInsets.all(10), decoration: BoxDecoration( border: Border.all(width: 5, color: Colors.yellow), child: Text( "hello", style: TextStyle(fontSize: 35), // 蓝色边框 Container( padding: EdgeInsets.all(10), decoration: BoxDecoration( border: Border.all(width: 5, color: Colors.blue), child: Text( "hello", style: TextStyle(fontSize: 35),

焦点放在Border.all的 color 属性上, 指定不同颜色的色值!

💡 代码解析

🟢 运行项目, 查看效果:

不同颜色的边框
再次搞定边框颜色!

当我们只想在某几个方向添加边框怎么办? 很简单!

继续添加:

// 边框方向(仅底部)
Container(
  padding: EdgeInsets.all(10),
  decoration: BoxDecoration(
    border: Border(bottom: BorderSide(width: 5, color: Colors.black)),
  child: Text(
    "hello",
    style: TextStyle(fontSize: 35),
// 边框方向并加粗(左、底)
Container(
  padding: EdgeInsets.all(10),
  decoration: BoxDecoration(
    border: Border(
        left: BorderSide(width: 8, color: Colors.black),
        bottom: BorderSide(width: 8, color: Colors.black)),
  child: Text(
    "hello",
    style: TextStyle(fontSize: 35),
// 边框方向, 3d效果
Container(
  padding: EdgeInsets.all(10),
  decoration: BoxDecoration(
    border: Border(
        top: BorderSide(width: 10, color: Colors.lightBlue),
        left: BorderSide(width: 15, color: Colors.lightBlueAccent),
        right: BorderSide(width: 5, color: Colors.blue),
        bottom: BorderSide(width: 3, color: Colors.blueAccent)),
  child: Text(
    "hello",
    style: TextStyle(fontSize: 35),

这回我们没有用Border.all进行创建, 而是用了Border()的构造函数, 可以上下左右分别传入BorderSide进行每个边的设置.

💡 代码解析
decoration: BoxDecoration( border: Border.all(width: 5), borderRadius: BorderRadius.all(Radius.circular(15))), child: Text( "hello", style: TextStyle(fontSize: 35), // 边框半径(较大圆角) Container( padding: EdgeInsets.all(10), decoration: BoxDecoration( border: Border.all(width: 5), borderRadius: BorderRadius.all(Radius.circular(30))), child: Text( "hello", style: TextStyle(fontSize: 35), // 边框半径(指定方向圆角) Container( padding: EdgeInsets.all(10), decoration: BoxDecoration( border: Border.all(width: 5), borderRadius: BorderRadius.only( topLeft: Radius.circular(30), bottomRight: Radius.circular(30))), child: Text( "hello", style: TextStyle(fontSize: 35),

我们把焦点放在borderRadius上. 我们分别用到了BorderRadius.all()以及BorderRadius.only()进行初始化边框半径. 和此前的用法类似, all代表所有角, only用来指定某角度.

💡 代码解析

🟢 运行项目, 看看效果(第四行):

边框半径(圆角)的不同效果(第4行)
又一次轻松搞定🤘🤘🤘🤘

这里为大家提供./lib/main.dart的可直接运行的全部源码:

点击展开全部源码

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Awesome Border',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(title: 'My Awesome Border'),
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State {
  // 即将渲染的边框组件集合
  List _borderWidgets;
  @override
  void initState() {
    // 处理带边框的对象集合
    _borderWidgets = [
      // 普通边框
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border.all(),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 细边框
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border.all(width: 3),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 粗边框
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border.all(width: 10),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 紫色边框
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border.all(width: 5, color: Colors.purple),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 黄色边框
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border.all(width: 5, color: Colors.yellow),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 蓝色边框
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border.all(width: 5, color: Colors.blue),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 边框方向(仅底部)
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border(bottom: BorderSide(width: 5, color: Colors.black)),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 边框方向并加粗(左、底)
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border(
              left: BorderSide(width: 8, color: Colors.black),
              bottom: BorderSide(width: 8, color: Colors.black)),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 边框方向, 3d效果
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          border: Border(
              top: BorderSide(width: 10, color: Colors.lightBlue),
              left: BorderSide(width: 15, color: Colors.lightBlueAccent),
              right: BorderSide(width: 5, color: Colors.blue),
              bottom: BorderSide(width: 3, color: Colors.blueAccent)),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 边框半径(圆角)
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            border: Border.all(width: 5),
            borderRadius: BorderRadius.all(Radius.circular(15))),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 边框半径(较大圆角)
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            border: Border.all(width: 5),
            borderRadius: BorderRadius.all(Radius.circular(30))),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
      // 边框半径(指定方向圆角)
      Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            border: Border.all(width: 5),
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(30),
                bottomRight: Radius.circular(30))),
        child: Text(
          "hello",
          style: TextStyle(fontSize: 35),
    super.initState();
  @override
  Widget build(BuildContext context) {
    // 为了美观, 将内容全部居中处理
    _borderWidgets.forEach((element) {
      _borderWidgets[_borderWidgets.indexOf(element)] = Center(child: element);
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
          child: Container(
        padding: EdgeInsets.all(20),
        child: GridView.count(
          padding: const EdgeInsets.all(8.0),
          crossAxisCount: 3,
          crossAxisSpacing: 5,
          mainAxisSpacing: 5,
          children: _borderWidgets,