flutter Material示例与MediaQuery.of(context) called with a context that does not contain a MediaQuery.

报错:

flutter MediaQuery.of(context) called with a context that does not contain a MediaQuery.

原因:

Scaffold必须放在MaterialApp里面,否则会报错

正确示例:

import 'package:flutter/material.dart';

main() => runApp(MyApp());

//MaterialApp : 将根布局换成一个 StatelessWidget
//Scaffold : 必须放在MaterialApp里面
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Material",
      home: LearnMaterial(),
    );
  }
}

class LearnMaterial extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LearnMaterial();
  }
}

class _LearnMaterial extends State<LearnMaterial> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("material"),
        ),
        body: Column(
          children: <Widget>[
            Center(
              child: Material(
                color: Colors.blueAccent,
                shape: BeveledRectangleBorder(
                  //斜角矩形边框
                  side: BorderSide(
                    width: 1.0,
                    color: Colors.blueAccent,
                    style: BorderStyle.none,
                  ),
                  borderRadius: BorderRadius.circular(50.0),
                ),
                child: Container(
                  padding: EdgeInsets.all(20.0),
                  child: Text('斜角矩形边框'),
                ),
              ),
            ),
            Center(
              child: Material(
                color: Colors.blueAccent,
                shape: BeveledRectangleBorder(
                  //斜角矩形边框
                  side: BorderSide(
                    width: 1.0,
                    color: Colors.blueAccent,
                    style: BorderStyle.none,
                  ),
                  borderRadius: BorderRadius.circular(10.0),
                ),
                child: Container(
                  padding: EdgeInsets.all(20.0),
                  child: Text('斜角矩形边框'),
                ),
              ),
            ),
            Center(
              child: Material(
                color: Colors.blueAccent,
                shape: BorderDirectional(
                    start: BorderSide(
                      color: Colors.yellow,
                      width: 10.0,
                    ),
                    top: BorderSide(
                      color: Colors.deepOrange,
                      width: 10.0,
                    ),
                    end: BorderSide(
                      color: Colors.black87,
                      width: 10.0,
                    ),
                    bottom: BorderSide(
                      color: Colors.purpleAccent,
                      width: 10.0,
                    )),
                child: Container(
                  padding: EdgeInsets.all(30.0),
                  child: Text('多彩边框'),
                ),
              ),
            ),
            Center(
              child: Material(
                color: Colors.blueAccent,
                shape: CircleBorder(
                    side: BorderSide(
                  color: Colors.yellow,
                  width: 10.0,
                )),
                child: Container(
                  padding: EdgeInsets.all(50.0),
                  child: Text('圆形边框'),
                ),
              ),
            ),
            Center(
              child: Material(
                color: Colors.blueAccent,
                shape: RoundedRectangleBorder(
                  side: BorderSide(
                    color: Colors.purpleAccent,
                    width: 5.0,
                  ),
                  borderRadius: BorderRadius.circular(
                      20.0), //如果[borderRadius]被指定,那么[type]属性不能是 [MaterialType.circle]。
                ),
                child: Container(
                  padding: EdgeInsets.all(30.0),
                  child: Text('圆角矩形'),
                ),
              ),
            ),
            Center(
              child: Material(
                color: Colors.purpleAccent,
                elevation: 10.0,
                shadowColor: Colors.yellow,
                shape: StadiumBorder(
                    side: BorderSide(
                  color: Colors.brown,
                  width: 5.0,
                )),
                child: Container(
                  padding: EdgeInsets.all(30.0),
                  child: Text('体育场形状'),
                ),
              ),
            )
          ],
        ));
  }
}

效果图:

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值