我是 Flutterflow 的新手,对使用“自定义代码”有点困惑。我想创建一些可以生成随机字符串的东西。我不知道这是否属于 Widget、Action 或 Function 类别。我尝试按照 Youtube 中的步骤操作,但没有成功。我被卡住了,仍然找不到类似的情况
这是我当前的代码。我复制了这里
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:random_string/random_string.dart';
import 'dart:math' show Random;
class GenStringRandom extends StatefulWidget {
const GenStringRandom({
Key? key,
this.width,
this.height,
}) : super(key: key);
final double? width;
final double? height;
@override
_GenStringRandomState createState() => _GenStringRandomState();
}
class _GenStringRandomState extends State<GenStringRandom> {
main() {
print(randomBetween(10, 20)); // some integer between 10 and 20
print(randomNumeric(4)); // sequence of 4 random numbers i.e. 3259
print(randomString(10)); // random sequence of 10 characters i.e. e~f93(4l-
print(randomAlpha(5)); // random sequence of 5 alpha characters i.e. aRztC
print(randomAlphaNumeric(
10)); // random sequence of 10 alpha numeric i.e. aRztC1y32B
var r = Random.secure();
print(randomBetween(10, 20,
provider:
CoreRandomProvider.from(r))); // You can use a provider from Random.
print(randomBetween(10, 20,
provider: _Provider())); // Or you can implement your own.
}
class _Provider with AbstractRandomProvider {
_Provider();
double nextDouble() => 0.5;
}
这是错误消息图像错误
我希望有人可以帮助我修复它并以简单的方式解释以及此处使用的自定义代码。先感谢您。 :)
import 'dart:convert';
import 'dart:math' as math;
List<String> generateRandomString() {
// give me code for generate random list of strings
final random = math.Random();
final List<String> randomStrings = List.generate(
10,
(index) => String.fromCharCodes(
List.generate(
10,
(index) => random.nextInt(26) + 97,
),
),
);
return randomStrings;
}
要在 Flutter 中生成随机字符串列表并使用文本小部件显示它们,您可以创建一个返回字符串列表的自定义函数。然后,在小部件树中使用此列表为每个项目创建文本小部件 以上是生成随机字符串的自定义函数。
这是 flutterflow 生成的文件,您可以从下面查看
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/flutter_flow/flutter_flow_widgets.dart';
import '/flutter_flow/custom_functions.dart' as functions;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'home_page_model.dart';
export 'home_page_model.dart';
class HomePageWidget extends StatefulWidget {
const HomePageWidget({super.key});
@override
State<HomePageWidget> createState() => _HomePageWidgetState();
}
class _HomePageWidgetState extends State<HomePageWidget> {
late HomePageModel _model;
final scaffoldKey = GlobalKey<ScaffoldState>();
@override
void initState() {
super.initState();
_model = createModel(context, () => HomePageModel());
}
@override
void dispose() {
_model.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => _model.unfocusNode.canRequestFocus
? FocusScope.of(context).requestFocus(_model.unfocusNode)
: FocusScope.of(context).unfocus(),
child: Scaffold(
key: scaffoldKey,
backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
appBar: AppBar(
backgroundColor: FlutterFlowTheme.of(context).primary,
automaticallyImplyLeading: false,
title: Text(
'Page Title',
style: FlutterFlowTheme.of(context).headlineMedium.override(
fontFamily: 'Outfit',
color: Colors.white,
fontSize: 22,
letterSpacing: 0,
),
),
actions: [],
centerTitle: false,
elevation: 2,
),
body: SafeArea(
top: true,
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Builder(
builder: (context) {
final title = functions.generateRandomString().toList();
return ListView.builder(
padding: EdgeInsets.zero,
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: title.length,
itemBuilder: (context, titleIndex) {
final titleItem = title[titleIndex];
return Text(
valueOrDefault<String>(
titleItem,
'kekd',
),
style: FlutterFlowTheme.of(context).bodyMedium.override(
fontFamily: 'Readex Pro',
letterSpacing: 0,
),
);
},
);
},
),
],
),
),
),
);
}
}