我是flutter的初学者,不太了解Widget的概念。
想把背景图设置为unsplash的随机图,但是想在点击的时候再添加一个按钮请求随机图,但是对Flutter的很多概念感到困惑,不知道怎么完成这个功能。
下面是我目前的代码,不知道怎么改,请大家帮帮我,非常感谢!
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
title: 'Welcome to Flutter',
theme: new ThemeData(
brightness: Brightness.light,
),
home: new Scaffold(
appBar: new AppBar(
title: new Text("Welcome to Flutter"),
),
body: BackgroundImgDemo()
),
);
}
}
class BackgroundImgDemo extends StatelessWidget {
final String imgUrl="https://unsplash.it/1440/3040?random";
const BackgroundImgDemo({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
image: new DecorationImage(
fit: BoxFit.cover,
image: new NetworkImage(imgUrl),
),
),
child: Container(
margin: EdgeInsets.only(top: 500.0),
child: Center(
child: RaisedButton(
color: Colors.white,
child: Text("clicke here!"),
onPressed: () {
},
)
),
),
);
}
}