flutter - 在使用 Flutter 设计修改 UI 时需要帮助
问题描述
我是一个飘飘然的菜鸟。我从github获得了这个示例 UI 。这是默认的主屏幕:
单击 3 个框中的任何一个都会呈现特定的 UI 类型。我希望 3 个 UI 中的任何一个的主屏幕成为我的应用程序的实际主屏幕。例如:
我试图理解代码,但作为菜鸟无法理解需要做什么才能达到要求。
这是 home_screen.dart 文件:
import 'package:best_flutter_ui_templates/app_theme.dart';
import 'package:flutter/material.dart';
import 'model/homelist.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
List<HomeList> homeList = HomeList.homeList;
AnimationController animationController;
bool multiple = true;
@override
void initState() {
animationController = AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
super.initState();
}
Future<bool> getData() async {
await Future<dynamic>.delayed(const Duration(milliseconds: 0));
return true;
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppTheme.white,
body: FutureBuilder<bool>(
future: getData(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
appBar(),
Expanded(
child: FutureBuilder<bool>(
future: getData(),
builder:
(BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return GridView(
padding: const EdgeInsets.only(
top: 0, left: 12, right: 12),
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.vertical,
children: List<Widget>.generate(
homeList.length,
(int index) {
final int count = homeList.length;
final Animation<double> animation =
Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: animationController,
curve: Interval((1 / count) * index, 1.0,
curve: Curves.fastOutSlowIn),
),
);
animationController.forward();
return HomeListView(
animation: animation,
animationController: animationController,
listData: homeList[index],
callBack: () {
Navigator.push<dynamic>(
context,
MaterialPageRoute<dynamic>(
builder: (BuildContext context) =>
homeList[index].navigateScreen,
),
);
},
);
},
),
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: multiple ? 2 : 1,
mainAxisSpacing: 12.0,
crossAxisSpacing: 12.0,
childAspectRatio: 1.5,
),
);
}
},
),
),
],
),
);
}
},
),
);
}
Widget appBar() {
return SizedBox(
height: AppBar().preferredSize.height,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8, left: 8),
child: Container(
width: AppBar().preferredSize.height - 8,
height: AppBar().preferredSize.height - 8,
),
),
Expanded(
child: Center(
child: Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
'Flutter UI',
style: TextStyle(
fontSize: 22,
color: AppTheme.darkText,
fontWeight: FontWeight.w700,
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8, right: 8),
child: Container(
width: AppBar().preferredSize.height - 8,
height: AppBar().preferredSize.height - 8,
color: Colors.white,
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius:
BorderRadius.circular(AppBar().preferredSize.height),
child: Icon(
multiple ? Icons.dashboard : Icons.view_agenda,
color: AppTheme.dark_grey,
),
onTap: () {
setState(() {
multiple = !multiple;
});
},
),
),
),
),
],
),
);
}
}
class HomeListView extends StatelessWidget {
const HomeListView(
{Key key,
this.listData,
this.callBack,
this.animationController,
this.animation})
: super(key: key);
final HomeList listData;
final VoidCallback callBack;
final AnimationController animationController;
final Animation<dynamic> animation;
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animationController,
builder: (BuildContext context, Widget child) {
return FadeTransition(
opacity: animation,
child: Transform(
transform: Matrix4.translationValues(
0.0, 50 * (1.0 - animation.value), 0.0),
child: AspectRatio(
aspectRatio: 1.5,
child: ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(4.0)),
child: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
Image.asset(
listData.imagePath,
fit: BoxFit.cover,
),
Material(
color: Colors.transparent,
child: InkWell(
splashColor: Colors.grey.withOpacity(0.2),
borderRadius:
const BorderRadius.all(Radius.circular(4.0)),
onTap: () {
callBack();
},
),
),
],
),
),
),
),
);
},
);
}
}
该HomeList.homeList
列表通过了 3 种不同的 UI 设计。基本上homeList[index].navigateScreen
是我在不影响其他小部件的情况下作为 Scaffold 的主体所需要的。
任何人都可以帮我弄清楚需要返回什么,MyHomePage
这样我就可以得到一个特定的 UI 作为我的主屏幕,而不是小盒子的 UI。
谢谢你。
解决方案
打开lib项目文件夹中的navigation_home_screen.dart,你会看到
@override
void initState() {
drawerIndex = DrawerIndex.HOME;
screenView = const MyHomePage();
super.initState();
}
这就是魔法发生的地方,简单地说,您只需设置您需要的主页之一,通过按右键导入它们 -> import ...并删除const
关键字:
@override
void initState() {
drawerIndex = DrawerIndex.HOME;
screenView =
HotelHomeScreen(); // or FitnessAppHomeScreen() or DesignCourseHomeScreen()
super.initState();
}
保存并重新启动项目模拟器以查看更改。
推荐阅读
- delphi - 有没有办法退出被卡住的 Parallel.foreach 循环中的线程
- javascript - 来自 SQS 队列的异步回调不会从队列中删除项目
- javascript - 如何在正则表达式中转义双斜杠(javascript)
- ios - Unity 使用 Apple 刷新令牌登录
- java - Web 服务模板:如何在 Web 服务模板中传递标头
- http - MC60 HTTP GET 请求问题
- mysql - 在单个 sql 查询中分组和计数
- excel - 工作表受保护时运行时错误 1004
- firefox - Firefox 标签中带下划线的标题
- .net-core - Autofac 多个注册到单个服务。简单注入器 -> Autofac 翻译