为背景图片单独 class BaseLayout

separate class BaseLayout for background image

// 我想为我的 Flutter 应用程序中的每个页面使用背景图片。目前,我正在使用 // 单独classBaseLayout设置背景图片。然而,颤动并不能反映 // 描述设备上的背景图像。 // 有没有更好的方法来反映背景图像?

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter_svg/flutter_svg.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: CarouselSlider(
          options: CarouselOptions(height: 240.0),
          items: [1, 2, 3, 4, 5].map((i) {
            return Builder(
              builder: (BuildContext context) {
                return Container(
                    width: MediaQuery.of(context).size.width,
                    margin: EdgeInsets.symmetric(horizontal: 5.0),
                    decoration: BoxDecoration(color: Colors.amber),
                    child: Text(
                      'text $i',
                      style: TextStyle(fontSize: 16.0),
                    ));
              },
            );
          }).toList(),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class  BaseLayout extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage("assets/images/logo1.svg"), fit: BoxFit.cover),
      ),
      child: Center(child: FlutterLogo(size: 300)),
    );
  }
}````

如果您要使用基本布局作为所有页面的父级,则必须使脚手架背景透明。 您可以使用 Scaffold 小部件的 scaffoldBackgroundColor 属性 使其透明。

或者更好的是,您可以在声明 MaterialApp 时将其定义为您的 Theme 数据。 示例代码:

class MyApp extends StatelessWidget {
// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
     title: 'Flutter Demo',
     theme: ThemeData(
       scaffoldBackgroundColor: Colors.transparent, // To Make scaffold background Transparent
       appBarTheme: AppBarTheme(color:Colors.transparent // To Make appbar background transparent.
       primarySwatch: Colors.blue,
       visualDensity: VisualDensity.adaptivePlatformDensity,
    ),
    home: MyHomePage(title: 'Flutter Demo Home Page'),
  );
 }
}

然后在你的 BaseLayout class:

class  BaseLayout extends StatelessWidget {
  const BaseLayout({this.child});
  final Widget child;
  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage("assets/images/logo1.svg"), fit: BoxFit.cover),
      ),
      child: Center(child: child),
    );
  }
}

现在您可以使用您刚刚创建的 BaseLayout class 包装应​​用程序的任何 Scaffold 小部件,并且它的背景图像在整个应用程序中都是相同的。